change uns_root_ptr to uns_ctr

This commit is contained in:
Peter McGoron 2024-06-12 21:41:35 -04:00
parent 26afacd565
commit 503b08caad
8 changed files with 35 additions and 32 deletions

View File

@ -103,7 +103,7 @@ int uns_cheney_c89_collect(struct uns_gc *gc)
unsigned char *scanptr; unsigned char *scanptr;
size_t newlen = gc->next_alloc; size_t newlen = gc->next_alloc;
struct uns_root_list *root; struct uns_ctr *root;
assert(gc->next_alloc >= fromspace_lim - fromspace); assert(gc->next_alloc >= fromspace_lim - fromspace);

View File

@ -29,7 +29,7 @@
int test(struct uns_gc *gc) int test(struct uns_gc *gc)
{ {
struct uns_root_list r = {0}; struct uns_ctr r = {0};
int i; int i;
/* MistralAI 8x7B 0.1: /* MistralAI 8x7B 0.1:

View File

@ -29,7 +29,7 @@
int test(struct uns_gc *gc) int test(struct uns_gc *gc)
{ {
struct uns_root_list r = {0}; struct uns_ctr r = {0};
int i; int i;
/* MistralAI 8x7B 0.1: /* MistralAI 8x7B 0.1:

View File

@ -29,7 +29,7 @@
int test(struct uns_gc *gc) int test(struct uns_gc *gc)
{ {
struct uns_root_list r = {0}; struct uns_ctr r = {0};
const char s[] = "hello, world"; const char s[] = "hello, world";
uns_string_alloc(gc, &r, 32); uns_string_alloc(gc, &r, 32);

View File

@ -34,7 +34,7 @@ enum {
NUMBER_OF_IND = 3 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; 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; 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); void *p = gc->record_get_ptr(root->p, i);
memcpy(p, &val, sizeof(val)); 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_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) #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); 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); 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); 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; unsigned char *p;
root->p = gc->alloc_record(gc, NUMBER_OF_IND); 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); 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); 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_root_add(gc, &tmp_new);
uns_string_alloc(gc, &tmp_new, newlen); 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); 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 used = uns_string_len(gc, root);
size_t allen = uns_string_allen(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); 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) const void *from, size_t bytes)
{ {
size_t len = uns_string_len(gc, to); 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); 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); 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; char *p;
uns_string_ensure(gc, root, 1); uns_string_ensure(gc, root, 1);

View File

@ -39,15 +39,15 @@
* (allocated length) (used) (size of used) * (allocated length) (used) (size of used)
*/ */
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);
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);
/* The following functions may cause a collection. */ /* 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_alloc(struct uns_gc *gc, struct uns_ctr *root, size_t start_len);
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);
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);
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); const void *from, size_t 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);
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);
#endif #endif

View File

@ -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`` * A root that is not a part of any GC MUST have ``prev`` and ``next``
* set to NULL. * set to NULL.
*/ */
struct uns_root_list { struct uns_ctr {
struct uns_root_list *prev; struct uns_ctr *prev;
void *p; void *p;
struct uns_root_list *next; struct uns_ctr *next;
}; };
struct uns_gc { struct uns_gc {
@ -64,7 +64,7 @@ struct uns_gc {
* *
* The roots list can have repeated values. * The roots list can have repeated values.
*/ */
struct uns_root_list *roots; struct uns_ctr *roots;
size_t next_alloc; size_t next_alloc;
void (*oom)(struct uns_gc *); void (*oom)(struct uns_gc *);
void (*after_gc)(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. */ /** 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 /** 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). * 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 #endif

4
uns.c
View File

@ -17,14 +17,14 @@
#include "uns.h" #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->prev = NULL;
root->next = gc->roots; root->next = gc->roots;
gc->roots = root; 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) { if (root->prev) {
root->prev->next = root->next; root->prev->next = root->next;