diff options
| author | 2024-07-08 19:58:22 -0400 | |
|---|---|---|
| committer | 2024-07-08 19:58:22 -0400 | |
| commit | 4f7847b2c0219995c4fe2d0858b5030e73581a3c (patch) | |
| tree | 9b84a1fc5698a4f8b97c0580fccf2b26c000d748 /examples/hashtable/uns_hashtable.c | |
| parent | delete for hashtables (diff) | |
Major API reorganization, test infrastructure
The API is now hidden behind functions. The GC struct can have an
unstable layout without affecting any compiled binaries (users of
Universal Service or collectors hidden behind the API). The
collectors can be called directly if desired.
The API now allows for different allocation flags. These will be
used in future extensions (like weak pointers). For now none are
used.
Tests are compiled for each collector. I can't think of a good
solution that will encompass everything I want to write, but for
now this will work on POSIX systems.
Diffstat (limited to 'examples/hashtable/uns_hashtable.c')
| -rw-r--r-- | examples/hashtable/uns_hashtable.c | 100 |
1 files changed, 49 insertions, 51 deletions
diff --git a/examples/hashtable/uns_hashtable.c b/examples/hashtable/uns_hashtable.c index 8dd36e7..100b2ca 100644 --- a/examples/hashtable/uns_hashtable.c +++ b/examples/hashtable/uns_hashtable.c @@ -35,52 +35,52 @@ enum { HTBL_HDR_LEN }; -static size_t get_size_t(struct uns_gc *gc, struct uns_ctr *root, size_t i) +static size_t get_size_t(Uns_GC gc, struct uns_ctr *root, size_t i) { size_t r; - void *p = gc->record_get_ptr(root->p, i); + void *p = uns_get(gc, root->p, i, NULL); memcpy(&r, p, sizeof(r)); return r; } -static void set_size_t(struct uns_gc *gc, struct uns_ctr *root, size_t i, size_t val) +static void set_size_t(Uns_GC gc, struct uns_ctr *root, size_t i, size_t val) { - void *p = gc->record_get_ptr(root->p, i); + void *p = uns_get(gc, root->p, i, NULL); memcpy(p, &val, sizeof(val)); } -static void *index(struct uns_gc *gc, struct uns_ctr *htbl, size_t i) +static void *index(Uns_GC gc, struct uns_ctr *htbl, size_t i) { - return gc->record_get_ptr(htbl->p, i + HTBL_HDR_LEN); + return uns_get(gc, htbl->p, i + HTBL_HDR_LEN, NULL); } -void uns_hashtable_alloc_into(struct uns_gc *gc, struct uns_ctr *r, size_t size) +void uns_hashtable_alloc_into(Uns_GC gc, struct uns_ctr *r, size_t size) { unsigned char *p; size_t i; - r->p = gc->alloc_record(gc, size + HTBL_HDR_LEN); + r->p = uns_alloc_rec(gc, size + HTBL_HDR_LEN, 0); - p = gc->alloc(gc, sizeof(size_t)); - gc->record_set_ptr(r->p, HTBL_LEN_PTR, p); + p = uns_alloc(gc, sizeof(size_t), 0); + uns_set(gc, r->p, HTBL_LEN_PTR, UNS_POINTER, p); set_size_t(gc, r, HTBL_LEN_PTR, size); - p = gc->alloc(gc, sizeof(size_t)); - gc->record_set_ptr(r->p, HTBL_USED_PTR, p); + p = uns_alloc(gc, sizeof(size_t), 0); + uns_set(gc, r->p, HTBL_USED_PTR, UNS_POINTER, p); set_size_t(gc, r, HTBL_USED_PTR, 0); /* Set entries to zero. */ for (i = HTBL_HDR_LEN; i <= size; i++) - gc->record_set_ptr(r->p, i, NULL); + uns_set(gc, r->p, i, UNS_POINTER, NULL); } -size_t uns_hashtable_len(struct uns_gc *gc, struct uns_ctr *htbl) +size_t uns_hashtable_len(Uns_GC gc, struct uns_ctr *htbl) { return get_size_t(gc, htbl, HTBL_LEN_PTR); } -size_t uns_hashtable_used(struct uns_gc *gc, struct uns_ctr *htbl) +size_t uns_hashtable_used(Uns_GC gc, struct uns_ctr *htbl) { return get_size_t(gc, htbl, HTBL_USED_PTR); } @@ -93,19 +93,19 @@ enum { HTBL_ENTRY_LEN }; -static void alloc_holder(struct uns_gc *gc, struct uns_ctr *r, struct uns_ctr *string, +static void alloc_holder(Uns_GC gc, struct uns_ctr *r, struct uns_ctr *string, struct uns_ctr *value, size_t len) { unsigned char *alloc_ptr; - r->p = gc->alloc_record(gc, HTBL_ENTRY_LEN); + r->p = uns_alloc_rec(gc, HTBL_ENTRY_LEN, 0); - alloc_ptr = gc->alloc(gc, sizeof(size_t)); + alloc_ptr = uns_alloc(gc, sizeof(size_t), 0); memcpy(alloc_ptr, &len, sizeof(size_t)); - gc->record_set_ptr(r->p, LENGTH, alloc_ptr); - gc->record_set_ptr(r->p, STRING, string->p); - gc->record_set_ptr(r->p, VALUE, value->p); - gc->record_set_ptr(r->p, NEXT, NULL); + uns_set(gc, r->p, LENGTH, UNS_POINTER, alloc_ptr); + uns_set(gc, r->p, STRING, UNS_POINTER, string->p); + uns_set(gc, r->p, VALUE, UNS_POINTER, value->p); + uns_set(gc, r->p, NEXT, UNS_POINTER, NULL); } static unsigned long fnv(unsigned char *p, size_t len) @@ -121,16 +121,16 @@ static unsigned long fnv(unsigned char *p, size_t len) return hash; } -static int search_container(struct uns_gc *gc, struct uns_ctr *cur_ctr, +static int search_container(Uns_GC gc, struct uns_ctr *cur_ctr, unsigned char *str, size_t len) { - unsigned char *ctr_str = gc->record_get_ptr(cur_ctr->p, STRING); + unsigned char *ctr_str = uns_get(gc, cur_ctr->p, STRING, NULL); size_t ctr_str_len = get_size_t(gc, cur_ctr, LENGTH); void *next; if (ctr_str_len != len || memcmp(ctr_str, str, len) != 0) { /* strings do not match */ - next = gc->record_get_ptr(cur_ctr->p, NEXT); + next = uns_get(gc, cur_ctr->p, NEXT, NULL); if (!next) return 0; cur_ctr->p = next; @@ -140,7 +140,7 @@ static int search_container(struct uns_gc *gc, struct uns_ctr *cur_ctr, } } -static int search_for_container(struct uns_gc *gc, struct uns_ctr *htbl, +static int search_for_container(Uns_GC gc, struct uns_ctr *htbl, struct uns_ctr *container, unsigned char *str, size_t len, size_t *hash_out) { @@ -158,7 +158,7 @@ static int search_for_container(struct uns_gc *gc, struct uns_ctr *htbl, /* This could be made better with internal pointers, but that is not * portable. */ -void uns_hashtable_del(struct uns_gc *gc, struct uns_ctr *htbl, +void uns_hashtable_del(Uns_GC gc, struct uns_ctr *htbl, unsigned char *str, size_t len, struct uns_ctr *old_value) { @@ -174,26 +174,26 @@ void uns_hashtable_del(struct uns_gc *gc, struct uns_ctr *htbl, /* Special case: first pointer */ if (len == get_size_t(gc, &rec, LENGTH) && - memcmp(str, gc->record_get_ptr(rec.p, STRING), len) == 0) { - old_value->p = gc->record_get_ptr(rec.p, VALUE); - gc->record_set_ptr(htbl->p, HTBL_HDR_LEN + hash, - gc->record_get_ptr(rec.p, NEXT)); + memcmp(str, uns_get(gc, rec.p, STRING, NULL), len) == 0) { + old_value->p = uns_get(gc, rec.p, VALUE, NULL); + uns_set(gc, htbl->p, HTBL_HDR_LEN + hash, UNS_POINTER, + uns_get(gc, rec.p, NEXT, NULL)); } prevptr = rec; - for (rec.p = gc->record_get_ptr(rec.p, NEXT); rec.p; - rec.p = gc->record_get_ptr(rec.p, NEXT)) { + for (rec.p = uns_get(gc, rec.p, NEXT, NULL); rec.p; + rec.p = uns_get(gc, rec.p, NEXT, NULL)) { if (len == get_size_t(gc, &rec, LENGTH) - && memcmp(str, gc->record_get_ptr(rec.p, VALUE), len) == 0) { - old_value->p = gc->record_get_ptr(rec.p, VALUE); - gc->record_set_ptr(prevptr.p, NEXT, - gc->record_get_ptr(rec.p, NEXT)); + && memcmp(str, uns_get(gc, rec.p, VALUE, NULL), len) == 0) { + old_value->p = uns_get(gc, rec.p, VALUE, NULL); + uns_set(gc, prevptr.p, UNS_POINTER, NEXT, + uns_get(gc, rec.p, NEXT, NULL)); return; } } } -void uns_hashtable_search(struct uns_gc *gc, struct uns_ctr *htbl, +void uns_hashtable_search(Uns_GC gc, struct uns_ctr *htbl, unsigned char *str, size_t string_len, struct uns_ctr *found) { @@ -201,12 +201,12 @@ void uns_hashtable_search(struct uns_gc *gc, struct uns_ctr *htbl, r = search_for_container(gc, htbl, found, str, string_len, NULL); if (r) - found->p = gc->record_get_ptr(found->p, VALUE); + found->p = uns_get(gc, found->p, VALUE, NULL); else found->p = NULL; } -void uns_hashtable_add(struct uns_gc *gc, struct uns_ctr *htbl, +void uns_hashtable_add(Uns_GC gc, struct uns_ctr *htbl, struct uns_ctr *string, size_t string_len, struct uns_ctr *value, struct uns_ctr *old_value) { @@ -218,8 +218,8 @@ void uns_hashtable_add(struct uns_gc *gc, struct uns_ctr *htbl, uns_hashtable_resize(gc, htbl, uns_hashtable_len(gc, htbl)*2); if (search_for_container(gc, htbl, &container, (unsigned char *)string->p, string_len, &hash)) { - old_value->p = gc->record_get_ptr(container.p, VALUE); - gc->record_set_ptr(container.p, VALUE, value->p); + old_value->p = uns_get(gc, container.p, VALUE, NULL); + uns_set(gc, container.p, VALUE, UNS_POINTER, value->p); return; } @@ -232,18 +232,16 @@ void uns_hashtable_add(struct uns_gc *gc, struct uns_ctr *htbl, alloc_holder(gc, &next_container, string, value, string_len); if (container.p) { - gc->record_set_ptr(container.p, NEXT, next_container.p); + uns_set(gc, container.p, NEXT, UNS_POINTER, next_container.p); } else { - gc->record_set_ptr(htbl->p, hash + HTBL_HDR_LEN, next_container.p); + uns_set(gc, htbl->p, hash + HTBL_HDR_LEN, UNS_POINTER, next_container.p); } uns_root_remove(gc, &next_container); uns_root_remove(gc, &container); } -/* TODO: add delete */ - -static void add_for_ent(struct uns_gc *gc, struct uns_ctr *htbl, void *ctrp) +static void add_for_ent(Uns_GC gc, struct uns_ctr *htbl, void *ctrp) { struct uns_ctr ctr = {0}; struct uns_ctr str = {0}; @@ -260,8 +258,8 @@ static void add_for_ent(struct uns_gc *gc, struct uns_ctr *htbl, void *ctrp) uns_root_add(gc, &value); uns_root_add(gc, &old_value_should_never_happen); - str.p = gc->record_get_ptr(ctr.p, STRING); - value.p = gc->record_get_ptr(ctr.p, VALUE); + str.p = uns_get(gc, ctr.p, STRING, NULL); + value.p = uns_get(gc, ctr.p, VALUE, NULL); str_len = get_size_t(gc, &ctr, LENGTH); uns_hashtable_add(gc, htbl, &str, str_len, &value, @@ -273,11 +271,11 @@ static void add_for_ent(struct uns_gc *gc, struct uns_ctr *htbl, void *ctrp) uns_root_remove(gc, &str); uns_root_remove(gc, &ctr); - ctrp = gc->record_get_ptr(ctr.p, NEXT); + ctrp = uns_get(gc, ctr.p, NEXT, NULL); add_for_ent(gc, htbl, ctrp); } -void uns_hashtable_resize(struct uns_gc *gc, struct uns_ctr *htbl, size_t newsize) +void uns_hashtable_resize(Uns_GC gc, struct uns_ctr *htbl, size_t newsize) { struct uns_ctr newtbl = {0}; size_t i; |
