From f60047d4b013eb7f75ad4f5c63eda63153a4bf8e Mon Sep 17 00:00:00 2001 From: kartofen Date: Sun, 8 Sep 2024 20:02:06 +0300 Subject: value is now opaque --- src/value.c | 64 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 31 deletions(-) (limited to 'src/value.c') diff --git a/src/value.c b/src/value.c index c72c4a2..94a7acd 100644 --- a/src/value.c +++ b/src/value.c @@ -19,26 +19,26 @@ const char * const value_type_string[] = { #define FN(fn, ...) return fn(buf, buf_sz, __VA_ARGS__) #define VALUE_STRING_TABLE(X, v, buf, buf_sz) \ X(VALUE_NIL, FN(snprintf, "(nil)")) \ - X(VALUE_ATOM, FN(snprintf, "%s", VALUE(v).atom)) \ - X(VALUE_STR, FN(snprintf, "%s", VALUE(v).str)) \ - X(VALUE_INT, FN(snprintf, "%d", VALUE(v).num)) \ - X(VALUE_CONS, FN(cons_print, &VALUE(v).cons)) \ - X(VALUE_PROC, FN(proc_print, &VALUE(v).proc)) \ - X(VALUE_MACRO, FN(proc_print, &VALUE(v).proc)) \ + X(VALUE_ATOM, FN(snprintf, "%s", vvalue_atom(v))) \ + X(VALUE_STR, FN(snprintf, "%s", vvalue_str(v))) \ + X(VALUE_INT, FN(snprintf, "%d", vvalue_num(v))) \ + X(VALUE_CONS, FN(cons_print, &vvalue_cons(v))) \ + X(VALUE_PROC, FN(proc_print, &vvalue_proc(v))) \ + X(VALUE_MACRO, FN(proc_print, &vvalue_proc(v))) \ X(VALUE_PROC_BUILTIN, \ - FN(snprintf, "%p", *(void **)&VALUE(v).proc_builtin.proc)) + FN(snprintf, "%p", *(void **)&vvalue_proc_builtin(v).proc)) -#define NOREFS(value) (--(value)->refs == 0) +#define NOREFS(value) (value_dec_refs(value) == 0) #define VALUE_DESTROY_TABLE(X, v) \ X(VALUE_NIL, (void)NOREFS(v)) \ - X(VALUE_ATOM, if(NOREFS(v)) free(VALUE(v).atom)) \ - X(VALUE_STR, if(NOREFS(v)) free(VALUE(v).str)) \ + X(VALUE_ATOM, if(NOREFS(v)) free(vvalue_atom(v))) \ + X(VALUE_STR, if(NOREFS(v)) free(vvalue_str(v))) \ X(VALUE_INT, (void)NOREFS(v)) \ X(VALUE_CONS, (void)NOREFS(v); \ - value_destroy(VALUE(v).cons.left); \ - value_destroy(VALUE(v).cons.right)) \ - X(VALUE_PROC, if(NOREFS(v)) proc_destroy(&VALUE(v).proc)) \ - X(VALUE_MACRO, if(NOREFS(v)) proc_destroy(&VALUE(v).proc)) \ + value_destroy(vvalue_cons(v).left); \ + value_destroy(vvalue_cons(v).right)) \ + X(VALUE_PROC, if(NOREFS(v)) proc_destroy(&vvalue_proc(v))) \ + X(VALUE_MACRO, if(NOREFS(v)) proc_destroy(&vvalue_proc(v))) \ X(VALUE_PROC_BUILTIN, (void)NOREFS(v)) #define CREATE(vtype, value) return value_create(vtype, value) @@ -63,32 +63,35 @@ const char * const value_type_string[] = { X(VALUE_MACRO, proc) \ X(VALUE_PROC_BUILTIN, proc_builtin) +// #define value_alloc() malloc(sizeof(struct value)) +// #define value_dealloc(v) free(v) +#define value_alloc() value_mempool_allocate() +#define value_free(v) value_mempool_free(v) + static char *str_alloc_copy(char *src); static int cons_print(char *buf, size_t buf_sz, struct cons *cons); static int proc_print(char *buf, size_t buf_sz, struct proc *proc); - static void proc_destroy(struct proc *proc); -#define MEMBER_COPY(vtype, member) \ - case vtype: memcpy(&_value->value.member, value, \ - sizeof(_value->value.member)); \ +#define MEMBER_COPY(vtype, member) \ + case vtype: memcpy(&vvalue_##member(_value), value, \ + sizeof(vvalue_##member(_value))); \ break; value_t value_create(enum value_type type, void *value) { - // value_t _value = malloc(sizeof(*_value)); - value_t _value = value_mempool_allocate(); - _value->type = type; + value_t _value = value_alloc(); + value_set_type(_value, type); switch(type) { VALUE_MEMBER_TABLE(MEMBER_COPY); default: break; } - _value->refs = 1; + value_set_refs(_value, 1); return _value; } @@ -101,9 +104,8 @@ void value_destroy(value_t value) VALUE_DESTROY_TABLE(CASE_APPLY, value); } - if(value->refs == 0) - value_mempool_free(value); - // free(value); + if(vvalue_refs(value) == 0) + value_free(value); } value_t value_from_token(struct token *token) @@ -122,11 +124,11 @@ value_t value_copy(value_t value) { if(!value) return value; - value->refs++; + value_inc_refs(value); - if(value->type == VALUE_CONS) { - value_copy(value->value.cons.left); - value_copy(value->value.cons.right); + if(vvalue_type(value) == VALUE_CONS) { + value_copy(vvalue_cons(value).left); + value_copy(vvalue_cons(value).right); } return value; @@ -168,13 +170,13 @@ static int cons_print(char *buf, size_t buf_sz, struct cons *cons) SET_VALUE_STRING(cons->left); value_t right = cons->right; - while(right->type == VALUE_CONS) { + while(vvalue_type(right) == VALUE_CONS) { SET_CHAR(' '); SET_VALUE_STRING(right->value.cons.left); right = right->value.cons.right; } - if(right->type != VALUE_NIL) { + if(vvalue_type(right) != VALUE_NIL) { SET_CHAR(' '); SET_CHAR('.'); SET_CHAR(' '); -- cgit v1.2.3