/* This software is licensed by the Apache License 2.0, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ #include "include.h" #include "assert.h" #ifndef LIBCDSB_SRC_INTERNAL_VNODE_H #define LIBCDSB_SRC_INTERNAL_VNODE_H #define is_permissible(T) (sizeof(void*) >= sizeof(T) && _Alignof(void*) >= _Alignof(T)) typedef void* vnode_t; extern vnode_t libcdsb_builtin_vnode_create ( var_t) wur__; extern vnode_t libcdsb_builtin_vnode_create_ex(vtype xt, var_t) wur__; extern var_t libcdsb_builtin_vnode_peek(const vnode_t* x, vtype type) pure__ wur__ Nonnull__(1); extern void libcdsb_builtin_vnode_free( vnode_t* x, vtype type) Nonnull__(1); ainline(void libcdsb_builtin_vnode_attach(vnode_t* node, var_t value)) { if (value.type < VTYPE_STRING) { *node = libcdsb_builtin_vnode_create(value); } else if (sizeof(str_t) == sizeof(void*) && value.type == VTYPE_STRING) { *node = *(char**)value.pointer; } else { *node = malloc(vtype_size(value.type)); memcpy(*node, value.pointer, vtype_size(value.type)); } } ainline(void libcdsb_builtin_vnode_attach_ex(vnode_t* node, vtype target, var_t value)) { if (value.type < VTYPE_STRING) { *node = libcdsb_builtin_vnode_create_ex(target, value); } else { type_assert(target, value.type); if (sizeof(str_t) == sizeof(void*) && value.type == VTYPE_STRING) { *node = *(char**)value.pointer; } else { *node = malloc(vtype_size(value.type)); memcpy(*node, value.pointer, vtype_size(value.type)); } } } #define vnode_create libcdsb_builtin_vnode_create #define vnode_attach libcdsb_builtin_vnode_attach #define vnode_create_ex libcdsb_builtin_vnode_create_ex #define vnode_attach_ex libcdsb_builtin_vnode_attach_ex #define vnode_peek libcdsb_builtin_vnode_peek #define vnode_free libcdsb_builtin_vnode_free #define vnode_hash(n, t) variable_hash(vnode_peek(n, t)) #define vnode_compare(s0, t0, s1, t1) variable_compare(vnode_peek(s0, t0), vnode_peek(s1, t1)) #define vnode_stringify(n, t) variable_stringify(vnode_peek(n, t)) #define vnode_duplicate(n, t) vnode_create(vnode_peek(n, t)) #define vnode_duplicate_ex(xt, n, t) vnode_create_ex(xt, vnode_peek(n, t)) #endif /* LIBCDSB_SRC_INTERNAL_VNODE_H */