Add base headers

This commit is contained in:
Gregory Lirent 2022-06-02 14:20:55 +03:00
parent 147bda0a2b
commit 3c4ff8587e
8 changed files with 425 additions and 0 deletions

19
include/__attributes.h Normal file
View File

@ -0,0 +1,19 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#ifndef LIBCDSB_CORE_ATTRIBUTES_H
#define LIBCDSB_CORE_ATTRIBUTES_H
#define LIBCDSB_nt__ __attribute__ ((nothrow))
#define LIBCDSB_nn1__ __attribute__ ((nonnull (1)))
#define LIBCDSB_nn12__ __attribute__ ((nonnull (1,2)))
#define LIBCDSB_nn123__ __attribute__ ((nonnull (1,2,3)))
#define LIBCDSB_nn13__ __attribute__ ((nonnull (1,3)))
#define LIBCDSB_pure__ LIBCDSB_nt__ __attribute__ ((pure))
#define LIBCDSB_wur__ __attribute__ ((warn_unused_result))
#define LIBCDSB_cmpattr__ LIBCDSB_pure__ LIBCDSB_nn12__
#define LIBCDSB_cpyattr__ LIBCDSB_pure__ LIBCDSB_wur__ LIBCDSB_nn1__
#define LIBCDSB_dupattr__ LIBCDSB_wur__ LIBCDSB_nn1__
#endif /* LIBCDSB_CORE_ATTRIBUTES_H */

24
include/extra/cstring.h Normal file
View File

@ -0,0 +1,24 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include <string.h>
#include "../__attributes.h"
#ifndef LIBCDSB_EXTRA_CSTRING_H
#define LIBCDSB_EXTRA_CSTRING_H
extern size_t libcdsb_strlen (const char* s) LIBCDSB_pure__ LIBCDSB_nn1__;
extern size_t libcdsb_strasciilen(const char* s) LIBCDSB_pure__ LIBCDSB_nn1__;
extern char* libcdsb_strdup (const char* s) LIBCDSB_nt__ LIBCDSB_wur__ LIBCDSB_nn1__;
extern char* libcdsb_strndup(const char* s, size_t n) LIBCDSB_nt__ LIBCDSB_wur__ LIBCDSB_nn1__;
extern void* libcdsb_memndup(const void* m, size_t n) LIBCDSB_nt__ LIBCDSB_wur__ LIBCDSB_nn1__;
#define strlen libcdsb_strlen
#define strasciilen libcdsb_strasciilen
#define strdup libcdsb_strdup
#define strndup libcdsb_strndup
#define memndup libcdsb_memndup
#endif /* LIBCDSB_EXTRA_CSTRING_H */

20
include/extra/memory.h Normal file
View File

@ -0,0 +1,20 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include <stddef.h>
#include "../__attributes.h"
#ifndef LIBCDSB_EXTRA_MEMORY_H
#define LIBCDSB_EXTRA_MEMORY_H
extern void* libcdsb_aalloc (size_t a, size_t n) LIBCDSB_nt__ LIBCDSB_wur__;
extern void* libcdsb_malloc (size_t n) LIBCDSB_nt__ LIBCDSB_wur__;
extern void* libcdsb_calloc (size_t n, size_t c) LIBCDSB_nt__ LIBCDSB_wur__;
extern void* libcdsb_realloc(void *p, size_t n) LIBCDSB_nt__ LIBCDSB_wur__;
#define aligned_alloc libcdsb_aalloc
#define malloc libcdsb_malloc
#define calloc libcdsb_calloc
#define realloc libcdsb_realloc
#endif /* LIBCDSB_EXTRA_MEMORY_H */

179
include/vtype.h Normal file
View File

@ -0,0 +1,179 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include <stdint.h>
#include <unistd.h>
#include <stdbool.h>
#include "__attributes.h"
#ifndef LIBCDSB_VTYPE_H
#define LIBCDSB_VTYPE_H
typedef enum libcdsb_value_types {
VTYPE_POINTER = 0,
VTYPE_BOOLEAN = 1,
VTYPE_UINT8 = 2,
VTYPE_UINT16 = 3,
VTYPE_UINT32 = 4,
VTYPE_UINT64 = 5,
VTYPE_INT8 = 6,
VTYPE_INT16 = 7,
VTYPE_INT32 = 8,
VTYPE_INT64 = 9,
VTYPE_FLOAT = 10,
VTYPE_DOUBLE = 11,
VTYPE_LDOUBLE = 12,
VTYPE_STRING = 13,
VTYPE_MAP = 14,
VTYPE_ARRAY = 15,
VTYPE_LIST = 16,
VTYPE_SET = 17,
LIBCDSB_VTYPE_TERMINATION, /* It must be at the end of the enum. Never be used */
} vtype;
struct libcdsb_string { char* buffer; };
struct libcdsb_array { void* mem; size_t size; vtype type; };
struct libcdsb_value { void* mem; int flags; vtype type; };
struct libcdsb_map { struct libcdsb_map_node* root; vtype type; };
struct libcdsb_set { struct libcdsb_rbtree_node* root; vtype type; };
struct libcdsb_list { struct libcdsb_list_node* last; struct libcdsb_list_node* first; };
typedef void* vtype_pointer;
typedef _Bool vtype_bool;
typedef uint_least8_t vtype_uint8;
typedef uint_least16_t vtype_uint16;
typedef uint_least32_t vtype_uint32;
typedef uint_least64_t vtype_uint64;
typedef int_least8_t vtype_int8;
typedef int_least16_t vtype_int16;
typedef int_least32_t vtype_int32;
typedef int_least64_t vtype_int64;
typedef float vtype_float;
typedef double vtype_double;
typedef long double vtype_ldouble;
typedef struct libcdsb_value_pair vtype_kvpair;
typedef struct libcdsb_value vtype_value;
typedef struct libcdsb_array vtype_array;
typedef struct libcdsb_map vtype_map;
typedef struct libcdsb_set vtype_set;
typedef struct libcdsb_list vtype_list;
typedef struct libcdsb_string vtype_string;
typedef struct libcdsb_iterator vtype_iterator;
/* Get utf8 chars count in string */
extern size_t string_size(const vtype_string* x) LIBCDSB_pure__ LIBCDSB_nn1__;
/* Get string size in bytes */
extern size_t string_nmemb(const vtype_string* x) LIBCDSB_pure__ LIBCDSB_nn1__;
/* Get count of the array elements */
extern size_t array_size(const vtype_array* x) LIBCDSB_pure__ LIBCDSB_nn1__;
/* Get array size in bytes */
extern size_t array_nmemb(const vtype_array* x) LIBCDSB_pure__ LIBCDSB_nn1__;
/* Get count of the list elements */
extern size_t list_size(const vtype_list* x) LIBCDSB_pure__ LIBCDSB_nn1__;
/* Get count of the map elements */
extern size_t map_size(const vtype_map* x) LIBCDSB_pure__ LIBCDSB_nn1__;
/* Get count of the set elements */
extern size_t vset_size(const vtype_set* x) LIBCDSB_pure__ LIBCDSB_nn1__;
/* Cpmpare 2 strings */
extern int string_compare(const vtype_string* s0, const vtype_string* s1) LIBCDSB_cmpattr__;
/* Compare 2 arrays */
extern int array_compare(const vtype_array* s0, const vtype_array* s1) LIBCDSB_cmpattr__;
/* Compare 2 lists */
extern int list_compare(const vtype_list* s0, const vtype_list* s1) LIBCDSB_cmpattr__;
/* Compare 2 maps */
extern int map_compare(const vtype_map* s0, const vtype_map* s1) LIBCDSB_cmpattr__;
/* Compare 2 sets */
extern int vset_compare(const vtype_set* s0, const vtype_set* s1) LIBCDSB_cmpattr__;
/* Copy string to another */
extern vtype_string string_copy(const vtype_string* x) LIBCDSB_cpyattr__;
/* Copy array to another */
extern vtype_array array_copy(const vtype_array* x) LIBCDSB_cpyattr__;
/* Copy list to another */
extern vtype_list list_copy(const vtype_list* x) LIBCDSB_cpyattr__;
/* Copy map to another */
extern vtype_map map_copy(const vtype_map* x) LIBCDSB_cpyattr__;
/* Copy set to another */
extern vtype_set vset_copy(const vtype_set* x) LIBCDSB_cpyattr__;
/* Duplicate string memory block */
extern vtype_string* string_duplicate(const vtype_string* x) LIBCDSB_dupattr__;
/* Duplicate array memory block */
extern vtype_array* array_duplicate(const vtype_array* x) LIBCDSB_dupattr__;
/* Duplicate list memory block */
extern vtype_list* list_duplicate(const vtype_list* x) LIBCDSB_dupattr__;
/* Duplicate map memory block */
extern vtype_map* map_duplicate(const vtype_map* x) LIBCDSB_dupattr__;
/* Duplicate set memory block */
extern vtype_set* vset_duplicate(const vtype_set* x) LIBCDSB_dupattr__;
/* Copy string and store result to the memory block */
extern void string_copy_init(vtype_string* x, const vtype_string* s) LIBCDSB_nn12__;
/* Copy array and store result to the memory block */
extern void array_copy_init(vtype_array* x, const vtype_array* s) LIBCDSB_nn12__;
/* Copy list and store result to the memory block */
extern void list_copy_init(vtype_list* x, const vtype_list* s) LIBCDSB_nn12__;
/* Copy map and store result to the memory block */
extern void map_copy_init(vtype_map* x, const vtype_map* s) LIBCDSB_nn12__;
/* Copy set and store result to the memory block */
extern void vset_copy_init(vtype_set* x, const vtype_set* s) LIBCDSB_nn12__;
/* Free string resources */
extern void string_free(vtype_string* x);
/* Free array resources */
extern void array_free(vtype_array* x);
/* Free list resources */
extern void list_free(vtype_list* x);
/* Free map resources */
extern void map_free(vtype_map* x);
/* Free set resources */
extern void vset_free(vtype_set* x);
extern void value_init(vtype_value* dest);
extern void value_free(vtype_value* dest);
extern _Bool value_is_readonly (const vtype_value* node);
extern _Bool value_is_strict_type(const vtype_value* node);
extern void value_is_null (const vtype_value* dest);
extern void value_nmemb (const vtype_value* dest);
extern void* value_get_pointer (const vtype_value* src);
extern _Bool value_set_pointer(vtype_value* node, const void* src);
extern _Bool value_set_cstring(vtype_value* node, const char* src);
extern _Bool value_set_string (vtype_value* node, const vtype_string* src);
extern _Bool value_set_array (vtype_value* node, const vtype_array* src);
extern _Bool value_set_list (vtype_value* node, const vtype_list* src);
extern _Bool value_set_map (vtype_value* node, const vtype_map* src);
extern _Bool value_set_vset (vtype_value* node, const vtype_set* src);
extern _Bool value_set_boolean(vtype_value* node, vtype_bool src);
extern _Bool value_set_uint8 (vtype_value* node, vtype_uint8 src);
extern _Bool value_set_uint16 (vtype_value* node, vtype_uint16 src);
extern _Bool value_set_uint32 (vtype_value* node, vtype_uint32 src);
extern _Bool value_set_uint64 (vtype_value* node, vtype_uint64 src);
extern _Bool value_set_int8 (vtype_value* node, vtype_int8 src);
extern _Bool value_set_int16 (vtype_value* node, vtype_int16 src);
extern _Bool value_set_int32 (vtype_value* node, vtype_int32 src);
extern _Bool value_set_int64 (vtype_value* node, vtype_int64 src);
extern _Bool value_set_float (vtype_value* node, vtype_float src);
extern _Bool value_set_double (vtype_value* node, vtype_double src);
extern _Bool value_set_ldouble(vtype_value* node, vtype_ldouble src);
#endif /* LIBCDSB_VTYPE_H */

View File

@ -0,0 +1,13 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../../include/__attributes.h"
#define throw__ HHTTPC_nt__
#define pure__ HHTTPC_pure__
#define const__ __attribute__((const)) HHTTPC_nt__
#define leaf__ __attribute__((leaf)) HHTTPC_nt__
#define wur__ HHTTPC_wur__
#define inline__ __attribute__((always_inline))
#define ainline(...) inline __VA_ARGS__ inline__; inline __VA_ARGS__

25
src/__internal/assert.h Normal file
View File

@ -0,0 +1,25 @@
/* This software is licensed by the Apache License 2.0, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include <stdlib.h>
#include <assert.h>
#include "../../include/vtype.h"
#define is_integer(t) ((t) <= VTYPE_INT64)
#define is_float(t) ((t) >= VTYPE_FLOAT && (t) <= VTYPE_LDOUBLE)
#define is_iterable(t) ((t) >= VTYPE_MAP)
#ifndef NDEBUG
# define type_assert(t0, t1) assert((t0) == (t1))
# define tvalue_assert(t) assert(is_integer(t))
# define tfloat_assert(t) assert(is_float (t))
# define expr_assert(expr) assert(expr)
#else
# define type_assert(t0, t1) if ( (t0) != (t1)) abort()
# define tvalue_assert(t) if (!is_integer(t)) abort()
# define tfloat_assert(t) if (!is_float (t)) abort()
# define expr_assert(expr) expr
#endif

103
src/__internal/include.h Normal file
View File

@ -0,0 +1,103 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include <stddef.h>
#include <stdlib.h>
#include "../../include/vtype.h"
#include "../../include/extra/memory.h"
#include "../../include/extra/cstring.h"
#include "../../include/extra/vtype.h"
#ifndef LIBCDSB_SRC_INTERNAL_INCLUDE
#define LIBCDSB_SRC_INTERNAL_INCLUDE
#define is_x64 (sizeof(void*) == sizeof(vtype_uint64))
#define is_big_endian (*((unsigned int*)"\0\0\0\1") < (unsigned int)0xffff)
#define is_little_endian (!is_big_endian)
#define is_null(x) (((void*)(x)) == nullptr)
#ifdef true
# undef true
#endif
#ifdef false
# undef false
#endif
#ifndef nullptr
# define nullptr ((void*)0)
#endif
#define true ((_Bool)1)
#define false ((_Bool)0)
#include "__attributes.h"
typedef vtype_uint8 u8_t;
typedef vtype_uint16 u16_t;
typedef vtype_uint32 u32_t;
typedef vtype_uint64 u64_t;
typedef vtype_int8 s8_t;
typedef vtype_int16 s16_t;
typedef vtype_int32 s32_t;
typedef vtype_int64 s64_t;
typedef vtype_float fl_t;
typedef vtype_double dbl_t;
typedef vtype_ldouble ldbl_t;
typedef vtype_string str_t;
typedef vtype_array arr_t;
typedef vtype_list list_t;
typedef vtype_map map_t;
typedef vtype_set set_t;
typedef vtype_value val_t;
typedef vtype_kvpair kvp_t;
typedef vtype_iterator iter_t;
typedef enum {
VF_WRITEABLE = 0x01,
VF_CHANGEABLE = 0x02,
VF_REMOVABLE = 0x0f
} vtype_value_flags;
ainline(val_t* value_set(val_t* d, void* ptr, vtype type, int flags)) {
d->mem = ptr;
d->type = type;
d->flags = flags;
return d;
}
extern int libcdsb_vtype_compare_values(const void* s0, vtype t0, const void* s1, vtype t1);
extern int libcdsb_vtype_compare_values_eq(const void* s0, const void* s1, vtype t);
#define vtype_compare libcdsb_vtype_compare_values
#define vtype_compare_eq libcdsb_vtype_compare_values_eq
#define vtype_size(type) (LIBCDSB_VTYPE_SIZES[type])
#define vtypeof(x) (vtype)(_Generic((x),\
const void**: VTYPE_POINTER, void**: VTYPE_POINTER, const void*: VTYPE_POINTER, void*: VTYPE_POINTER,\
const char**: VTYPE_STRING, char**: VTYPE_STRING, const char*: VTYPE_STRING, char*: VTYPE_STRING,\
const str_t*: VTYPE_STRING, str_t*: VTYPE_STRING, str_t: VTYPE_STRING,\
const arr_t*: VTYPE_ARRAY, arr_t*: VTYPE_ARRAY, arr_t: VTYPE_ARRAY,\
const list_t*: VTYPE_LIST, list_t*: VTYPE_LIST, list_t: VTYPE_LIST,\
const map_t*: VTYPE_MAP, map_t*: VTYPE_MAP, map_t: VTYPE_MAP,\
const set_t*: VTYPE_SET, set_t*: VTYPE_SET, set_t: VTYPE_SET,\
const vtype_bool*: VTYPE_BOOLEAN, vtype_bool*: VTYPE_BOOLEAN, vtype_bool: VTYPE_BOOLEAN,\
const vtype_uint8*: VTYPE_UINT8, vtype_uint8*: VTYPE_UINT8, vtype_uint8: VTYPE_UINT8,\
const vtype_uint16*: VTYPE_UINT16, vtype_uint16*: VTYPE_UINT16, vtype_uint16: VTYPE_UINT16,\
const vtype_uint32*: VTYPE_UINT32, vtype_uint32*: VTYPE_UINT32, vtype_uint32: VTYPE_UINT32,\
const vtype_uint64*: VTYPE_UINT64, vtype_uint64*: VTYPE_UINT64, vtype_uint64: VTYPE_UINT64,\
const vtype_int8*: VTYPE_INT8, vtype_int8*: VTYPE_INT8, vtype_int8: VTYPE_INT8,\
const vtype_int16*: VTYPE_INT16, vtype_int16*: VTYPE_INT16, vtype_int16: VTYPE_INT16,\
const vtype_int32*: VTYPE_INT32, vtype_int32*: VTYPE_INT32, vtype_int32: VTYPE_INT32,\
const vtype_int64*: VTYPE_INT64, vtype_int64*: VTYPE_INT64, vtype_int64: VTYPE_INT64,\
const vtype_float*: VTYPE_FLOAT, vtype_float*: VTYPE_FLOAT, vtype_float: VTYPE_FLOAT,\
const vtype_double*: VTYPE_DOUBLE, vtype_double*: VTYPE_DOUBLE, vtype_double: VTYPE_DOUBLE,\
const vtype_ldouble*: VTYPE_LDOUBLE, vtype_ldouble*: VTYPE_LDOUBLE, vtype_ldouble: VTYPE_LDOUBLE))
#endif /* LIBCDSB_SRC_INTERNAL_INCLUDE */

42
src/__internal/vnode.h Normal file
View File

@ -0,0 +1,42 @@
/* This software is licensed by the Apache License 2.0, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "include.h"
#ifndef LIBCDSB_SRC_INTERNAL_VNODE_H
#define LIBCDSB_SRC_INTERNAL_VNODE_H
#define mark_ptr(x, m) ((void*)((uintptr_t)(x)|(uintptr_t)(m)))
#define unmark_ptr(x) ((void*)((uintptr_t)(x)&~(uintptr_t)(_Alignof(max_align_t)-1)))
#define get_mark(x) ((int)((uintptr_t)(x)&(uintptr_t)(_Alignof(max_align_t)-1)))
#define unmark_ptr16(x) ((void*)((uintptr_t)(x)&~(uintptr_t)(15)))
#define get_mark16(x) ((int)((uintptr_t)(x)&(uintptr_t)(15)))
#define is_permissible(T) (sizeof(void*) >= sizeof(T) && _Alignof(void*) >= _Alignof(T))
typedef union {
void* ptr; _Bool b;
str_t s; arr_t a; list_t l;
map_t m; set_t vs;
u8_t u8; u16_t u16; u32_t u32; u64_t u64;
fl_t f; dbl_t d; ldbl_t ld;
} var_t;
typedef void* vnode_t;
extern vnode_t libcdsb_vnode_create(const void* value, vtype type);
extern vnode_t libcdsb_vnode_create_target(vtype target_type, const void* value, vtype type);
extern void libcdsb_vnode_free(vnode_t* x, vtype type);
extern void* libcdsb_vnode_peek(const vnode_t* x, vtype type);
#define vnode_create libcdsb_vnode_create
#define vnode_tcreate libcdsb_vnode_create_target
#define vnode_peek libcdsb_vnode_peek
#define vnode_free libcdsb_vnode_free
#define vnode_compare(s0, t0, s1, t1) vtype_compare(vnode_peek(s0, t0), t0, vnode_peek(s1, t1), t1)
#define vnode_duplicate(vnode, type) libcdsb_vnode_create(vnode_peek(vnode, type), type)
#define vnode_tduplicate(target_type, vnode, type) libcdsb_vnode_create_target(target_type, vnode_peek(vnode, type), type)
#endif /* LIBCDSB_SRC_INTERNAL_VNODE_H */