diff options
| author | 2024-07-08 19:58:22 -0400 | |
|---|---|---|
| committer | 2024-07-08 19:58:22 -0400 | |
| commit | 4f7847b2c0219995c4fe2d0858b5030e73581a3c (patch) | |
| tree | 9b84a1fc5698a4f8b97c0580fccf2b26c000d748 /examples/string/uns_string.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 '')
| -rw-r--r-- | examples/string/uns_string.c | 43 |
1 files changed, 22 insertions, 21 deletions
diff --git a/examples/string/uns_string.c b/examples/string/uns_string.c index fe6f06a..26bdc4d 100644 --- a/examples/string/uns_string.c +++ b/examples/string/uns_string.c @@ -34,44 +34,45 @@ enum { NUMBER_OF_IND = 3 }; -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); + /* TODO: record type checking */ + 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)); } #define set_allen(gc,root,val) set_size_t(gc, root, ALLEN_IND, val) #define set_len(gc,root,val) set_size_t(gc, root, LEN_IND, val) -static size_t uns_string_allen(struct uns_gc *gc, struct uns_ctr *root) +static size_t uns_string_allen(Uns_GC gc, struct uns_ctr *root) { return get_size_t(gc, root, ALLEN_IND); } -size_t uns_string_len(struct uns_gc *gc, struct uns_ctr *root) +size_t uns_string_len(Uns_GC gc, struct uns_ctr *root) { return get_size_t(gc, root, LEN_IND); } -char *uns_string_ptr(struct uns_gc *gc, struct uns_ctr *root) +char *uns_string_ptr(Uns_GC gc, struct uns_ctr *root) { - return gc->record_get_ptr(root->p, BYTES_IND); + return uns_get(gc, root->p, BYTES_IND, NULL); } -void uns_string_alloc(struct uns_gc *gc, struct uns_ctr *root, size_t start_len) +void uns_string_alloc(Uns_GC gc, struct uns_ctr *root, size_t start_len) { unsigned char *p; - root->p = gc->alloc_record(gc, NUMBER_OF_IND); + root->p = uns_alloc_rec(gc, NUMBER_OF_IND, 0); /* The following is incorrect: * gc->record_set_ptr(root->p, ALLEN_IND, gc->alloc(gc, sizeof(size_t))); @@ -79,19 +80,19 @@ void uns_string_alloc(struct uns_gc *gc, struct uns_ctr *root, size_t start_len) * but root->p may have already been put on the stack. */ - p = gc->alloc(gc, sizeof(size_t)); - gc->record_set_ptr(root->p, ALLEN_IND, p); + p = uns_alloc(gc, sizeof(size_t), 0); + uns_set(gc, root->p, ALLEN_IND, UNS_POINTER, p); set_allen(gc, root, start_len); - p = gc->alloc(gc, sizeof(size_t)); - gc->record_set_ptr(root->p, LEN_IND, p); + p = uns_alloc(gc, sizeof(size_t), 0); + uns_set(gc, root->p, LEN_IND, UNS_POINTER, p); set_len(gc, root, 0); - p = gc->alloc(gc, start_len); - gc->record_set_ptr(root->p, BYTES_IND, p); + p = uns_alloc(gc, start_len, 0); + uns_set(gc, root->p, BYTES_IND, UNS_POINTER, p); } -void uns_string_resize(struct uns_gc *gc, struct uns_ctr *root, size_t newlen) +void uns_string_resize(Uns_GC gc, struct uns_ctr *root, size_t newlen) { struct uns_ctr tmp_new = {0}; size_t old_len = uns_string_len(gc, root); @@ -112,7 +113,7 @@ void uns_string_resize(struct uns_gc *gc, struct uns_ctr *root, size_t newlen) uns_root_remove(gc, &tmp_new); } -void uns_string_ensure(struct uns_gc *gc, struct uns_ctr *root, size_t extent) +void uns_string_ensure(Uns_GC gc, struct uns_ctr *root, size_t extent) { size_t used = uns_string_len(gc, root); size_t allen = uns_string_allen(gc, root); @@ -121,7 +122,7 @@ void uns_string_ensure(struct uns_gc *gc, struct uns_ctr *root, size_t extent) uns_string_resize(gc, root, used + extent); } -void uns_string_append_bytes(struct uns_gc *gc, struct uns_ctr *to, +void uns_string_append_bytes(Uns_GC gc, struct uns_ctr *to, const void *from, size_t bytes) { size_t len = uns_string_len(gc, to); @@ -130,12 +131,12 @@ void uns_string_append_bytes(struct uns_gc *gc, struct uns_ctr *to, set_len(gc, to, len + bytes); } -void uns_string_append_char(struct uns_gc *gc, struct uns_ctr *root, char c) +void uns_string_append_char(Uns_GC gc, struct uns_ctr *root, char c) { uns_string_append_bytes(gc, root, &c, 1); } -char *uns_string_cstring(struct uns_gc *gc, struct uns_ctr *root) +char *uns_string_cstring(Uns_GC gc, struct uns_ctr *root) { char *p; uns_string_ensure(gc, root, 1); |
