This commit is contained in:
Gregory Lirent 2022-06-02 14:22:19 +03:00
parent 3c4ff8587e
commit 6a359e7f4b
3 changed files with 675 additions and 0 deletions

134
src/extra.c Normal file
View File

@ -0,0 +1,134 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include <stdlib.h>
#include "../include/extra/cstring.h"
#include "__internal/include.h"
#undef aligned_alloc
#undef malloc
#undef realloc
#undef calloc
/*#####################################################################################################################*/
void* libcdsb_aalloc(size_t a, size_t n) {
void* x;
if ((x = aligned_alloc(a, n)))
return x;
abort();
}
void* libcdsb_calloc(size_t n, size_t c) {
void* x;
if ((x = calloc(n, c)))
return x;
abort();
}
void* libcdsb_malloc(size_t n) {
void* x;
if ((x = malloc(n)))
return x;
abort();
}
void* libcdsb_realloc(void* x, size_t n) {
if ((x = realloc(x, n)))
return x;
abort();
}
/*#####################################################################################################################*/
void* libcdsb_memndup(const void* m, size_t n) {
void* x;
if ((x = malloc(n)))
return memcpy(x, m, n);
abort();
}
char* libcdsb_strdup(const char* s) {
void* x;
size_t n;
if ((x = malloc(n = strlen(s) + 1)))
return memcpy(x, s, n);
abort();
}
char* libcdsb_strndup(const char* s, size_t n) {
void* x;
if ((x = malloc(n + 1))) {
((char*)memcpy(x, s, n))[n] = 0;
return x;
}
abort();
}
/*#####################################################################################################################*/
size_t libcdsb_strlen(const char* s) {
static const size_t m = (sizeof(size_t) == 8) ? 0x8080808080808080UL : 0x80808080UL;
static const size_t d = (sizeof(size_t) == 8) ? 0x0101010101010101UL : 0x01010101UL;
const u8_t* p;
const size_t* w;
p = (void*)s;
while((uintptr_t)p%sizeof(size_t)) {
if (!*p) return (void*)p - (void*)s;
++p;
}
w = (void*)p;
while (!((*w-d)&(~*w&m))) { ++w; }
p = (void*)w;
while (*p) { ++p; }
return (void*)p - (void*)s;
}
size_t libcdsb_strasciilen(const char* s) {
static const size_t m = (sizeof(size_t) == 8) ? 0x8080808080808080UL : 0x80808080UL;
static const size_t d = (sizeof(size_t) == 8) ? 0x0101010101010101UL : 0x01010101UL;
const u8_t* p;
const size_t* w;
p = (void*)s;
while((uintptr_t)p%sizeof(size_t)) {
if (!*p || (*p&0x80))
return (void*)p - (void*)s;
++p;
}
w = (void*)p;
while (!((*w&m)||((*w-d)&m))) { ++w; }
p = (void*)w;
while (*p && !(*p&0x80)) { ++p; }
return (void*)p - (void*)s;
}

273
src/vnode.c Normal file
View File

@ -0,0 +1,273 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "__internal/assert.h"
#include "__internal/vnode.h"
/*#####################################################################################################################*/
static vnode_t create_number(vtype xt, const void* v, vtype t) {
var_t _ = { .ptr = 0 };
if (t == VTYPE_POINTER) t = (sizeof(void*) == 8) ? VTYPE_UINT64 : VTYPE_UINT32;
if (t == VTYPE_BOOLEAN) t = VTYPE_UINT8;
if (xt == VTYPE_POINTER) xt = (sizeof(void*) == 8) ? VTYPE_UINT64 : VTYPE_UINT32;
if (xt == VTYPE_BOOLEAN) xt = VTYPE_UINT8;
switch (xt) { default: default_: abort();
case VTYPE_INT8:
case VTYPE_UINT8: switch (t) { default: goto default_;
case VTYPE_INT8:
case VTYPE_UINT8: _.u8 = *(u8_t*)v; break;
case VTYPE_INT16:
case VTYPE_UINT16: _.u8 = *(u16_t*)v; break;
case VTYPE_INT32:
case VTYPE_UINT32: _.u8 = *(u32_t*)v; break;
case VTYPE_INT64:
case VTYPE_UINT64: _.u8 = *(u64_t*)v; break;
}
break;
case VTYPE_INT16:
case VTYPE_UINT16: switch (t) { default: goto default_;
case VTYPE_INT8:
case VTYPE_UINT8: _.u16 = *(u8_t*)v; break;
case VTYPE_INT16:
case VTYPE_UINT16: _.u16 = *(u16_t*)v; break;
case VTYPE_INT32:
case VTYPE_UINT32: _.u16 = *(u32_t*)v; break;
case VTYPE_INT64:
case VTYPE_UINT64: _.u16 = *(u64_t*)v; break;
}
break;
case VTYPE_INT32:
case VTYPE_UINT32: switch (t) { default: goto default_;
case VTYPE_INT8:
case VTYPE_UINT8: _.u32 = *(u8_t*)v; break;
case VTYPE_INT16:
case VTYPE_UINT16: _.u32 = *(u16_t*)v; break;
case VTYPE_INT32:
case VTYPE_UINT32: _.u32 = *(u32_t*)v; break;
case VTYPE_INT64:
case VTYPE_UINT64: _.u32 = *(u64_t*)v; break;
}
break;
case VTYPE_INT64:
case VTYPE_UINT64: switch (t) { default: goto default_;
case VTYPE_INT8:
case VTYPE_UINT8: _.u64 = *(u8_t*)v; goto u64_;
case VTYPE_INT16:
case VTYPE_UINT16: _.u64 = *(u16_t*)v; goto u64_;
case VTYPE_INT32:
case VTYPE_UINT32: _.u64 = *(u32_t*)v; goto u64_;
case VTYPE_INT64:
case VTYPE_UINT64: _.u64 = *(u64_t*)v;
u64_: if (!is_x64) _.ptr = memndup(&_.u64, sizeof(_.u64));
break;
}
break;
case VTYPE_FLOAT: switch (t) { default: goto default_;
case VTYPE_DOUBLE: _.f = *(dbl_t*)v; goto f_;
case VTYPE_LDOUBLE: _.f = *(ldbl_t*)v; goto f_;
case VTYPE_FLOAT: _.f = *(fl_t*)v;
f_: if (!is_permissible(fl_t)) _.ptr = memndup(&_.f, sizeof(_.f));
break;
}
break;
case VTYPE_DOUBLE: switch (t) { default: goto default_;
case VTYPE_FLOAT: _.d = *(fl_t*)v; goto d_;
case VTYPE_LDOUBLE: _.d = *(ldbl_t*)v; goto d_;
case VTYPE_DOUBLE: _.d = *(dbl_t*)v;
d_: if (!is_permissible(dbl_t)) _.ptr = memndup(&_.d, sizeof(_.d));
break;
}
break;
case VTYPE_LDOUBLE: switch (t) { default: goto default_;
case VTYPE_FLOAT: _.ld = *(fl_t*)v; goto ld_;
case VTYPE_DOUBLE: _.ld = *(dbl_t*)v; goto ld_;
case VTYPE_LDOUBLE: _.ld = *(ldbl_t*)v;
ld_: if (!is_permissible(ldbl_t)) _.ptr = memndup(&_.ld, sizeof(_.ld));
break;
}
break;
}
return _.ptr;
}
/*#####################################################################################################################*/
vnode_t hhttpc_vnode_create(const void* v, vtype t) {
var_t _ = { .ptr = 0 };
switch (t) { default: abort();
case VTYPE_INT8:
case VTYPE_UINT8: _.u8 = *(u8_t*)v;
break;
case VTYPE_INT16:
case VTYPE_UINT16: _.u16 = *(u16_t*)v;
break;
case VTYPE_INT32:
case VTYPE_UINT32: _.u32 = *(u32_t*)v;
break;
case VTYPE_INT64:
case VTYPE_UINT64:
u64_: if (is_x64) _.u64 = *(u64_t*)v;
else _.ptr = memndup(v, sizeof(u64_t));
break;
case VTYPE_FLOAT: if (is_permissible(fl_t)) _.ld = *(fl_t*)v;
else _.ptr = memndup(v, sizeof(fl_t));
break;
case VTYPE_DOUBLE: if (is_permissible(dbl_t)) _.ld = *(dbl_t*)v;
else _.ptr = memndup(v, sizeof(dbl_t));
break;
case VTYPE_LDOUBLE: if (is_permissible(ldbl_t)) _.ld = *(ldbl_t*)v;
else _.ptr = memndup(v, sizeof(ldbl_t));
break;
case VTYPE_BOOLEAN: _.b = *(_Bool*)v;
break;
case VTYPE_POINTER: _.ptr = *(void**)v;
break;
case VTYPE_STRING: _.ptr = string_duplicate(v);
break;
case VTYPE_MAP: _.ptr = map_duplicate(v);
break;
case VTYPE_ARRAY: _.ptr = array_duplicate(v);
break;
case VTYPE_LIST: _.ptr = list_duplicate(v);
break;
case VTYPE_SET: _.ptr = vset_duplicate(v);
break;
}
return _.ptr;
}
/*#####################################################################################################################*/
void* libcdsb_vnode_peek(const vnode_t* x, vtype t) {
switch (t) { default: abort();
case VTYPE_FLOAT: if (is_permissible(fl_t)) goto vt_; else goto pt_;
case VTYPE_DOUBLE: if (is_permissible(dbl_t)) goto vt_; else goto pt_;
case VTYPE_LDOUBLE: if (is_permissible(ldbl_t)) goto vt_; else goto pt_;
case VTYPE_INT64:
case VTYPE_UINT64: if (!is_x64) goto pt_;
case VTYPE_BOOLEAN:
case VTYPE_POINTER:
case VTYPE_INT8:
case VTYPE_UINT8:
case VTYPE_INT16:
case VTYPE_UINT16:
case VTYPE_INT32:
case VTYPE_UINT32:
vt_: return (void*)x;
case VTYPE_STRING:
case VTYPE_MAP:
case VTYPE_ARRAY:
case VTYPE_LIST:
case VTYPE_SET:
pt_: return *x;
}
}
void libcdsb_vnode_free(vnode_t* x, vtype t) {
switch (t) { default: abort();
case VTYPE_FLOAT: if (is_permissible(fl_t)) goto vt_; else goto pt_;
case VTYPE_DOUBLE: if (is_permissible(dbl_t)) goto vt_; else goto pt_;
case VTYPE_LDOUBLE: if (is_permissible(ldbl_t)) goto vt_; else goto pt_;
case VTYPE_INT64:
case VTYPE_UINT64: if (!is_x64) goto pt_;
case VTYPE_BOOLEAN:
case VTYPE_POINTER:
case VTYPE_INT8:
case VTYPE_UINT8:
case VTYPE_INT16:
case VTYPE_UINT16:
case VTYPE_INT32:
case VTYPE_UINT32:
vt_:
break;
case VTYPE_STRING: string_free(*x); goto pt_;
case VTYPE_MAP: map_free(*x); goto pt_;
case VTYPE_ARRAY: array_free(*x); goto pt_;
case VTYPE_LIST: list_free(*x); goto pt_;
case VTYPE_SET: vset_free(*x);
pt_: free(*x);
break;
}
*x = 0;
}
/*#####################################################################################################################*/
vnode_t libcdsb_vnode_create_target(vtype xt, const void* v, vtype t) {
var_t _ = { .ptr = 0 };
if (is_integer(xt)) {
tvalue_assert(t);
return create_number(xt, v, t);
} else if (is_float(xt)) {
tfloat_assert(t);
return create_number(xt, v, t);
}
type_assert(xt, t);
switch (xt) { default: abort();
case VTYPE_STRING: _.ptr = string_duplicate(v);
break;
case VTYPE_MAP: _.ptr = map_duplicate(v);
break;
case VTYPE_ARRAY: _.ptr = array_duplicate(v);
break;
case VTYPE_LIST: _.ptr = list_duplicate(v);
break;
case VTYPE_SET: _.ptr = vset_duplicate(v);
break;
}
return _.ptr;
}

268
src/vtype.c Normal file
View File

@ -0,0 +1,268 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "__internal/include.h"
#define max_align _Alignof(max_align_t)
#define max_size ((sizeof(ldbl_t) >= 8) ? sizeof(ldbl_t) : 8)
/*#####################################################################################################################*/
ainline(vtype expandu8(u32_t* v)) {
if (is_little_endian) {
*v &= 0x000000ff;
} else {
*v >>= 24;
}
return VTYPE_UINT32;
}
ainline(vtype expandu16(u32_t* v)) {
if (is_little_endian) {
*v &= 0x0000ffff;
} else {
*v >>= 16;
}
return VTYPE_UINT32;
}
ainline(vtype expands8(u32_t* v)) {
if (is_big_endian)
*v >>= 24;
*v |= 0xffffff00;
return VTYPE_INT32;
}
ainline(vtype expands16(u32_t* v)) {
if (is_big_endian)
*v >>= 16;
*v |= 0xffff0000;
return VTYPE_INT32;
}
/*#####################################################################################################################*/
const size_t LIBCDSB_VTYPE_SIZES[18] = {
sizeof(void*), sizeof(_Bool),
sizeof(u8_t), sizeof(u16_t), sizeof(u32_t), sizeof(u64_t),
sizeof(s8_t), sizeof(s16_t), sizeof(s32_t), sizeof(s64_t),
sizeof(fl_t), sizeof(dbl_t), sizeof(ldbl_t),
sizeof(str_t), sizeof(map_t), sizeof(arr_t),
sizeof(list_t), sizeof(set_t)
};
/*#####################################################################################################################*/
int libcdsb_vtype_compare_values(const void* s0, vtype t0, const void* s1, vtype t1) {
int c;
size_t _Alignas(max_align) v0[max_size/sizeof(size_t)];
size_t _Alignas(max_align) v1[max_size/sizeof(size_t)];
c = t0 - t1;
if (t0 >= VTYPE_STRING || t1 >= VTYPE_STRING) {
static int(*compare[5])(const void*,const void*) = {
(void*)string_compare,
(void*) map_compare,
(void*) array_compare,
(void*) list_compare,
(void*) vset_compare
};
if (c) return c;
t0 -= VTYPE_STRING;
return compare[t0](s0, s1);
}
if (t0 == VTYPE_POINTER) t0 = (sizeof(void*) == 8) ? VTYPE_UINT64 : VTYPE_UINT32;
if (t1 == VTYPE_POINTER) t1 = (sizeof(void*) == 8) ? VTYPE_UINT64 : VTYPE_UINT32;
if (t0 == VTYPE_BOOLEAN) t0 = VTYPE_UINT8;
if (t1 == VTYPE_BOOLEAN) t1 = VTYPE_UINT8;
if (!c) {
if (s0 == s1)
return 0;
if (t0 == VTYPE_LDOUBLE) {
*(ldbl_t*)v0 = *(ldbl_t*)s0 - *(ldbl_t*)s1;
ldouble_:
if (!*(ldbl_t*)v0)
return 0;
return *(ldbl_t*)v0 < 0 ? -1 : 1;
} else if (t0 == VTYPE_DOUBLE) {
*(ldbl_t*)v0 = *(dbl_t*)s0 - *(dbl_t*)s1;
goto ldouble_;
} else if (t0 == VTYPE_FLOAT) {
*(ldbl_t*)v0 = *(fl_t*)s0 - *(fl_t*)s1;
goto ldouble_;
}
if (t0 == VTYPE_UINT8)
return *(u8_t*)s0 - *(u8_t*)s1;
if (t0 == VTYPE_UINT16)
return *(u16_t*)s0 - *(u16_t*)s1;
if (t0 == VTYPE_UINT32)
return *(u32_t*)s0 - *(u32_t*)s1;
return *(u64_t*)s0 - *(u64_t*)s1;
} else if (t0 >= VTYPE_FLOAT && t1 >= VTYPE_FLOAT) {
if (t0 == VTYPE_FLOAT) {
*(ldbl_t*)v0 = *(fl_t*)s0;
} else if (t0 == VTYPE_DOUBLE) {
*(ldbl_t*)v0 = *(dbl_t*)s0;
} else if (t0 == VTYPE_LDOUBLE) {
*(ldbl_t*)v0 = *(ldbl_t*)s0;
}
if (t1 == VTYPE_FLOAT) {
*(ldbl_t*)v0 -= *(fl_t*)s1;
} else if (t1 == VTYPE_DOUBLE) {
*(ldbl_t*)v0 -= *(dbl_t*)s1;
} else {
*(ldbl_t*)v0 -= *(ldbl_t*)s1;
}
goto ldouble_;
}
memcpy(v0, s0, 8);
memcpy(v1, s1, 8);
if (t0 <= VTYPE_UINT64 && t1 <= VTYPE_UINT64) {
if (t0 == VTYPE_UINT8) {
t0 = expandu8((void*)v0);
} else if (t0 == VTYPE_UINT16) {
t0 = expandu16((void*)v0);
} else if (t0 == VTYPE_UINT64 && *(u64_t*)v0 <= UINT32_MAX) {
t0 = VTYPE_UINT32;
}
if (t1 == VTYPE_UINT8) {
t1 = expandu8((void*)v1);
} else if (t1 == VTYPE_UINT16) {
t1 = expandu16((void*)v1);
} else if (t1 == VTYPE_UINT64 && *(u64_t*)v1 <= UINT32_MAX) {
t0 = VTYPE_UINT32;
}
unsigned_: c = t0 - t1;
if (!c) return *(u32_t*)v0 - *(u32_t*)v1;
} else if (t0 <= VTYPE_INT64 && t1 <= VTYPE_INT64) {
if (t0 == VTYPE_INT8) {
t0 = (*(u8_t*)v0 <= INT8_MAX) ? expandu8((void*)v0) : expands8((void*)v0);
} else if (t0 == VTYPE_INT16) {
t0 = (*(u16_t*)v0 <= INT16_MAX) ? expandu16((void*)v0) : expands16((void*)v0);
} else if (t0 == VTYPE_INT32) {
if (*(u32_t*)v0 <= INT32_MAX) {
t0 = VTYPE_UINT32;
}
} else if (t0 == VTYPE_INT64) {
if (*(u64_t*)v0 <= UINT32_MAX) {
t0 = VTYPE_UINT32;
} else if (*(u64_t*)v0 <= INT64_MAX) {
t0 = VTYPE_UINT64;
}
}
if (t1 == VTYPE_INT8) {
t1 = (*(u8_t*)v1 <= INT8_MAX) ? expandu8((void*)v1) : expands8((void*)v1);
} else if (t1 == VTYPE_INT16) {
t1 = (*(u16_t*)v1 <= INT16_MAX) ? expandu16((void*)v1) : expands16((void*)v1);
} else if (t1 == VTYPE_INT32) {
if (*(u32_t*)v1 <= INT32_MAX) {
t1 = VTYPE_UINT32;
}
} else if (t1 == VTYPE_INT64) {
if (*(u64_t*)v1 <= UINT32_MAX) {
t1 = VTYPE_UINT32;
} else if (*(u64_t*)v1 <= INT64_MAX) {
t1 = VTYPE_UINT64;
}
}
if (t0 <= VTYPE_UINT64 && t1 <= VTYPE_UINT64)
goto unsigned_;
if (t0 <= VTYPE_UINT64) return 1;
if (t1 <= VTYPE_UINT64) return -1;
c = t0 - t1;
if (!c) return *(u32_t*)v1 - *(u32_t*)v0;
}
return c;
}
/*#####################################################################################################################*/
int libcdsb_vtype_compare_values_eq(const void* s0, const void* s1, vtype t) {
ldbl_t v[1];
if (s0 == s1)
return 0;
switch (t) { default: abort();
case VTYPE_INT8: return *(s8_t*)s0 - *(s8_t*)s1;
case VTYPE_INT16: return *(s16_t*)s0 - *(s16_t*)s1;
case VTYPE_INT32: return *(s32_t*)s0 - *(s32_t*)s1;
case VTYPE_INT64: return *(s64_t*)s0 - *(s64_t*)s1;
case VTYPE_BOOLEAN:
case VTYPE_UINT8: return *(u8_t*)s0 - *(u8_t*)s1;
case VTYPE_UINT16: return *(u16_t*)s0 - *(u16_t*)s1;
case VTYPE_UINT32:
u32_: return *(u32_t*)s0 - *(u32_t*)s1;
case VTYPE_POINTER: if (sizeof(void*) != 8) goto u32_;
case VTYPE_UINT64: return *(u64_t*)s0 - *(u64_t*)s1;
case VTYPE_LDOUBLE: *(ldbl_t*)v = *(ldbl_t*)s0 - *(ldbl_t*)s1;
if (!*(ldbl_t*)v)
return 0;
return *(ldbl_t*)v < 0 ? -1 : 1;
case VTYPE_DOUBLE: *(dbl_t*)v = *(dbl_t*)s0 - *(dbl_t*)s1;
if (!*(dbl_t*)v)
return 0;
return *(dbl_t*)v < 0 ? -1 : 1;
case VTYPE_FLOAT: *(fl_t*)v = *(fl_t*)s0 - *(fl_t*)s1;
if (!*(fl_t*)v)
return 0;
return *(fl_t*)v < 0 ? -1 : 1;
case VTYPE_STRING: return string_compare(s0, s1);
case VTYPE_ARRAY: return array_compare(s0, s1);
case VTYPE_LIST: return list_compare(s0, s1);
case VTYPE_MAP: return map_compare(s0, s1);
case VTYPE_SET: return vset_compare(s0, s1);
}
}