From 503b08caadb9be5b54d8542ad57973612d061cfc Mon Sep 17 00:00:00 2001 From: Peter McGoron Date: Wed, 12 Jun 2024 21:41:35 -0400 Subject: [PATCH] change uns_root_ptr to uns_ctr --- cheney_c89.c | 2 +- examples/string/test_inefficient.c | 2 +- examples/string/test_large.c | 2 +- examples/string/test_small.c | 2 +- examples/string/uns_string.c | 27 +++++++++++++++------------ examples/string/uns_string.h | 16 ++++++++-------- include/uns.h | 12 ++++++------ uns.c | 4 ++-- 8 files changed, 35 insertions(+), 32 deletions(-) diff --git a/cheney_c89.c b/cheney_c89.c index e6aee3a..25b248f 100644 --- a/cheney_c89.c +++ b/cheney_c89.c @@ -103,7 +103,7 @@ int uns_cheney_c89_collect(struct uns_gc *gc) unsigned char *scanptr; size_t newlen = gc->next_alloc; - struct uns_root_list *root; + struct uns_ctr *root; assert(gc->next_alloc >= fromspace_lim - fromspace); diff --git a/examples/string/test_inefficient.c b/examples/string/test_inefficient.c index fb96684..46533ac 100644 --- a/examples/string/test_inefficient.c +++ b/examples/string/test_inefficient.c @@ -29,7 +29,7 @@ int test(struct uns_gc *gc) { - struct uns_root_list r = {0}; + struct uns_ctr r = {0}; int i; /* MistralAI 8x7B 0.1: diff --git a/examples/string/test_large.c b/examples/string/test_large.c index 68964ef..2ba7a8a 100644 --- a/examples/string/test_large.c +++ b/examples/string/test_large.c @@ -29,7 +29,7 @@ int test(struct uns_gc *gc) { - struct uns_root_list r = {0}; + struct uns_ctr r = {0}; int i; /* MistralAI 8x7B 0.1: diff --git a/examples/string/test_small.c b/examples/string/test_small.c index 08b2ed2..458c318 100644 --- a/examples/string/test_small.c +++ b/examples/string/test_small.c @@ -29,7 +29,7 @@ int test(struct uns_gc *gc) { - struct uns_root_list r = {0}; + struct uns_ctr r = {0}; const char s[] = "hello, world"; uns_string_alloc(gc, &r, 32); diff --git a/examples/string/uns_string.c b/examples/string/uns_string.c index 219df89..fe6f06a 100644 --- a/examples/string/uns_string.c +++ b/examples/string/uns_string.c @@ -34,7 +34,7 @@ enum { NUMBER_OF_IND = 3 }; -static size_t get_size_t(struct uns_gc *gc, struct uns_root_list *root, size_t i) +static size_t get_size_t(struct uns_gc *gc, struct uns_ctr *root, size_t i) { size_t r; @@ -44,7 +44,7 @@ static size_t get_size_t(struct uns_gc *gc, struct uns_root_list *root, size_t i return r; } -static void set_size_t(struct uns_gc *gc, struct uns_root_list *root, size_t i, size_t val) +static void set_size_t(struct uns_gc *gc, struct uns_ctr *root, size_t i, size_t val) { void *p = gc->record_get_ptr(root->p, i); memcpy(p, &val, sizeof(val)); @@ -53,22 +53,22 @@ static void set_size_t(struct uns_gc *gc, struct uns_root_list *root, size_t i, #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_root_list *root) +static size_t uns_string_allen(struct 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_root_list *root) +size_t uns_string_len(struct 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_root_list *root) +char *uns_string_ptr(struct uns_gc *gc, struct uns_ctr *root) { return gc->record_get_ptr(root->p, BYTES_IND); } -void uns_string_alloc(struct uns_gc *gc, struct uns_root_list *root, size_t start_len) +void uns_string_alloc(struct uns_gc *gc, struct uns_ctr *root, size_t start_len) { unsigned char *p; root->p = gc->alloc_record(gc, NUMBER_OF_IND); @@ -91,11 +91,14 @@ void uns_string_alloc(struct uns_gc *gc, struct uns_root_list *root, size_t star gc->record_set_ptr(root->p, BYTES_IND, p); } -void uns_string_resize(struct uns_gc *gc, struct uns_root_list *root, size_t newlen) +void uns_string_resize(struct uns_gc *gc, struct uns_ctr *root, size_t newlen) { - struct uns_root_list tmp_new = {0}; + struct uns_ctr tmp_new = {0}; size_t old_len = uns_string_len(gc, root); + /* The temporary string needs to be added to the roots list because + * there are multiple allocations that happen in the body of the string. + */ uns_root_add(gc, &tmp_new); uns_string_alloc(gc, &tmp_new, newlen); @@ -109,7 +112,7 @@ void uns_string_resize(struct uns_gc *gc, struct uns_root_list *root, size_t new uns_root_remove(gc, &tmp_new); } -void uns_string_ensure(struct uns_gc *gc, struct uns_root_list *root, size_t extent) +void uns_string_ensure(struct 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); @@ -118,7 +121,7 @@ void uns_string_ensure(struct uns_gc *gc, struct uns_root_list *root, size_t ext uns_string_resize(gc, root, used + extent); } -void uns_string_append_bytes(struct uns_gc *gc, struct uns_root_list *to, +void uns_string_append_bytes(struct uns_gc *gc, struct uns_ctr *to, const void *from, size_t bytes) { size_t len = uns_string_len(gc, to); @@ -127,12 +130,12 @@ void uns_string_append_bytes(struct uns_gc *gc, struct uns_root_list *to, set_len(gc, to, len + bytes); } -void uns_string_append_char(struct uns_gc *gc, struct uns_root_list *root, char c) +void uns_string_append_char(struct 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_root_list *root) +char *uns_string_cstring(struct uns_gc *gc, struct uns_ctr *root) { char *p; uns_string_ensure(gc, root, 1); diff --git a/examples/string/uns_string.h b/examples/string/uns_string.h index 14ae812..b583992 100644 --- a/examples/string/uns_string.h +++ b/examples/string/uns_string.h @@ -39,15 +39,15 @@ * (allocated length) (used) (size of used) */ -size_t uns_string_len(struct uns_gc *gc, struct uns_root_list *root); -char *uns_string_ptr(struct uns_gc *gc, struct uns_root_list *root); +size_t uns_string_len(struct uns_gc *gc, struct uns_ctr *root); +char *uns_string_ptr(struct uns_gc *gc, struct uns_ctr *root); /* The following functions may cause a collection. */ -void uns_string_alloc(struct uns_gc *gc, struct uns_root_list *root, size_t start_len); -void uns_string_resize(struct uns_gc *gc, struct uns_root_list *root, size_t newlen); -void uns_string_ensure(struct uns_gc *gc, struct uns_root_list *root, size_t extent); -void uns_string_append_bytes(struct uns_gc *gc, struct uns_root_list *to, +void uns_string_alloc(struct uns_gc *gc, struct uns_ctr *root, size_t start_len); +void uns_string_resize(struct uns_gc *gc, struct uns_ctr *root, size_t newlen); +void uns_string_ensure(struct uns_gc *gc, struct uns_ctr *root, size_t extent); +void uns_string_append_bytes(struct uns_gc *gc, struct uns_ctr *to, const void *from, size_t bytes); -void uns_string_append_char(struct uns_gc *gc, struct uns_root_list *root, char c); -char *uns_string_cstring(struct uns_gc *gc, struct uns_root_list *root); +void uns_string_append_char(struct uns_gc *gc, struct uns_ctr *root, char c); +char *uns_string_cstring(struct uns_gc *gc, struct uns_ctr *root); #endif diff --git a/include/uns.h b/include/uns.h index 1277b32..93a859d 100644 --- a/include/uns.h +++ b/include/uns.h @@ -48,10 +48,10 @@ typedef UNS_SIGNED_WORD uns_sword; * A root that is not a part of any GC MUST have ``prev`` and ``next`` * set to NULL. */ -struct uns_root_list { - struct uns_root_list *prev; +struct uns_ctr { + struct uns_ctr *prev; void *p; - struct uns_root_list *next; + struct uns_ctr *next; }; struct uns_gc { @@ -64,7 +64,7 @@ struct uns_gc { * * The roots list can have repeated values. */ - struct uns_root_list *roots; + struct uns_ctr *roots; size_t next_alloc; void (*oom)(struct uns_gc *); void (*after_gc)(struct uns_gc *); @@ -94,11 +94,11 @@ struct uns_gc { }; /** Add a root to the GC. The root must point to valid memory, or NULL. */ -void uns_root_add(struct uns_gc *gc, struct uns_root_list *root); +void uns_root_add(struct uns_gc *gc, struct uns_ctr *root); /** Remove a root from the GC. It is OK to remove a root that is not a * part of any GC using this function (the call will have no effect). */ -void uns_root_remove(struct uns_gc *gc, struct uns_root_list *root); +void uns_root_remove(struct uns_gc *gc, struct uns_ctr *root); #endif diff --git a/uns.c b/uns.c index 4d6eaad..6ae6894 100644 --- a/uns.c +++ b/uns.c @@ -17,14 +17,14 @@ #include "uns.h" -void uns_root_add(struct uns_gc *gc, struct uns_root_list *root) +void uns_root_add(struct uns_gc *gc, struct uns_ctr *root) { root->prev = NULL; root->next = gc->roots; gc->roots = root; } -void uns_root_remove(struct uns_gc *gc, struct uns_root_list *root) +void uns_root_remove(struct uns_gc *gc, struct uns_ctr *root) { if (root->prev) { root->prev->next = root->next;