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 | |
| 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/string')
| -rw-r--r-- | examples/string/Makefile | 25 | ||||
| -rw-r--r-- | examples/string/test_inefficient.c | 50 | ||||
| -rw-r--r-- | examples/string/test_large.c | 45 | ||||
| -rw-r--r-- | examples/string/test_small.c | 2 | ||||
| -rw-r--r-- | examples/string/uns_string.c | 43 | ||||
| -rw-r--r-- | examples/string/uns_string.h | 16 |
6 files changed, 79 insertions, 102 deletions
diff --git a/examples/string/Makefile b/examples/string/Makefile deleted file mode 100644 index 9693d5e..0000000 --- a/examples/string/Makefile +++ /dev/null @@ -1,25 +0,0 @@ -.PHONY: test clean -TESTS=test_small.test test_inefficient.test test_large.test -COMMON_OBJS=../test_common.o uns_string.o -.SUFFIXES: .test -CFLAGS=-Wall -Wno-overlength-strings -std=c89 -Werror -pedantic -fPIC -g -Iinclude - -libunsstring.so: uns_string.o - $(CC) -shared -I../../include $(CFLAGS) $< -o libunsstring.so - -test: $(TESTS) $(COMMON_OBJS) - for i in $(TESTS); do \ - LD_LIBRARY_PATH=$$(pwd)/../../ valgrind ./$$i || exit 1; \ - done - -test_inefficient.test: $(COMMON_OBJS) -test_small.test: $(COMMON_OBJS) -test_large.test: $(COMMON_OBJS) - -.c.test: - $(CC) -I../../include $(CFLAGS) $< $(COMMON_OBJS) -L../../ -luniversalservice -o $@ -.c.o: - $(CC) -I../../include $(CFLAGS) $< -c -o $@ - -clean: - rm -f $(TESTS) $(COMMON_OBJS) libunsstring.so diff --git a/examples/string/test_inefficient.c b/examples/string/test_inefficient.c index 46533ac..7db180f 100644 --- a/examples/string/test_inefficient.c +++ b/examples/string/test_inefficient.c @@ -24,49 +24,51 @@ */ #include <stdio.h> +#include <string.h> #include "uns.h" #include "uns_string.h" -int test(struct uns_gc *gc) +int test(Uns_GC gc) { struct uns_ctr r = {0}; - int i; + size_t i, j; + size_t len; /* MistralAI 8x7B 0.1: * Prompt: "write a sonnet about garbage collectors (the memory management kind)" - * - * This string constant is larger than the standard's minimum. If - * this test doesn't compile, get a better compiler. */ - const char s[] = - "To the humble garbagemen of code's dark night,\n" - "Who tread through bits and bytes with steady hand.\n" - "They roam the heap, in dimly lit sight,\n" - "And rescue order from chaos unplanned.\n" + const char *s[] = { + "To the humble garbagemen of code's dark night,\n", + "Who tread through bits and bytes with steady hand.\n", + "They roam the heap, in dimly lit sight,\n", + "And rescue order from chaos unplanned.\n", - "With algorithms precise and swift they glide,\n" - "Through malloc'd arrays and pointers vast.\n" - "Their work essential to keep programs wide,\n" - "From crashing down upon us too soon passed.\n" + "With algorithms precise and swift they glide,\n", + "Through malloc'd arrays and pointers vast.\n", + "Their work essential to keep programs wide,\n", + "From crashing down upon us too soon passed.\n", - "Yet often left unsung are these brave knights,\n" - "Whose vigilance keeps our software clean.\n" - "In shadows cast by CPUs might,\n" - "Unseen forces that make sense of unseen.\n" + "Yet often left unsung are these brave knights,\n", + "Whose vigilance keeps our software clean.\n", + "In shadows cast by CPUs might,\n", + "Unseen forces that make sense of unseen.\n", - "So here's to you, dear Garbage Collectors all,\n" + "So here's to you, dear Garbage Collectors all,\n", "Thank you for keeping coding dreams enthralled.\n" - ; + }; /* Generate lots of garbage by being inefficient. */ uns_string_alloc(gc, &r, 32); uns_root_add(gc, &r); - for (i = 0; i < sizeof(s) - 1; i++) { - uns_string_append_char(gc, &r, s[i]); + for (i = 0; i < sizeof(s)/sizeof(s[0]); i++) { + len = strlen(s[i]); + for (j = 0; j < len; j++) + uns_string_append_char(gc, &r, s[i][j]); } - gc->collect(gc); + + uns_collect(gc); uns_root_remove(gc, &r); - gc->collect(gc); + uns_collect(gc); return 0; } diff --git a/examples/string/test_large.c b/examples/string/test_large.c index 2ba7a8a..58e6b8c 100644 --- a/examples/string/test_large.c +++ b/examples/string/test_large.c @@ -24,49 +24,48 @@ */ #include <stdio.h> +#include <string.h> #include "uns.h" #include "uns_string.h" -int test(struct uns_gc *gc) +int test(Uns_GC gc) { struct uns_ctr r = {0}; - int i; + int i, j; /* MistralAI 8x7B 0.1: * Prompt: "write a sonnet about garbage collectors (the memory management kind)" - * - * This string constant is larger than the standard's minimum. If - * this test doesn't compile, get a better compiler. */ - const char s[] = - "To the humble garbagemen of code's dark night,\n" - "Who tread through bits and bytes with steady hand.\n" - "They roam the heap, in dimly lit sight,\n" - "And rescue order from chaos unplanned.\n" + const char *s[] = { + "To the humble garbagemen of code's dark night,\n", + "Who tread through bits and bytes with steady hand.\n", + "They roam the heap, in dimly lit sight,\n", + "And rescue order from chaos unplanned.\n", - "With algorithms precise and swift they glide,\n" - "Through malloc'd arrays and pointers vast.\n" - "Their work essential to keep programs wide,\n" - "From crashing down upon us too soon passed.\n" + "With algorithms precise and swift they glide,\n", + "Through malloc'd arrays and pointers vast.\n", + "Their work essential to keep programs wide,\n", + "From crashing down upon us too soon passed.\n", - "Yet often left unsung are these brave knights,\n" - "Whose vigilance keeps our software clean.\n" - "In shadows cast by CPUs might,\n" - "Unseen forces that make sense of unseen.\n" + "Yet often left unsung are these brave knights,\n", + "Whose vigilance keeps our software clean.\n", + "In shadows cast by CPUs might,\n", + "Unseen forces that make sense of unseen.\n", - "So here's to you, dear Garbage Collectors all,\n" + "So here's to you, dear Garbage Collectors all,\n", "Thank you for keeping coding dreams enthralled.\n" - ; + }; /* generate lots of garbage */ uns_string_alloc(gc, &r, 32); uns_root_add(gc, &r); for (i = 0; i < 100; i++) { - uns_string_append_bytes(gc, &r, s, sizeof(s) - 1); + for (j = 0; j < sizeof(s)/sizeof(s[0]); j++) + uns_string_append_bytes(gc, &r, s[j], strlen(s[j])); } - gc->collect(gc); + uns_collect(gc); uns_root_remove(gc, &r); - gc->collect(gc); + uns_collect(gc); return 0; } diff --git a/examples/string/test_small.c b/examples/string/test_small.c index 458c318..f275ae2 100644 --- a/examples/string/test_small.c +++ b/examples/string/test_small.c @@ -27,7 +27,7 @@ #include "uns.h" #include "uns_string.h" -int test(struct uns_gc *gc) +int test(Uns_GC gc) { struct uns_ctr r = {0}; const char s[] = "hello, world"; 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); diff --git a/examples/string/uns_string.h b/examples/string/uns_string.h index b583992..c56b5ea 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_ctr *root); -char *uns_string_ptr(struct uns_gc *gc, struct uns_ctr *root); +size_t uns_string_len(Uns_GC gc, struct uns_ctr *root); +char *uns_string_ptr(Uns_GC gc, struct uns_ctr *root); /* The following functions may cause a collection. */ -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, +void uns_string_alloc(Uns_GC gc, struct uns_ctr *root, size_t start_len); +void uns_string_resize(Uns_GC gc, struct uns_ctr *root, size_t newlen); +void uns_string_ensure(Uns_GC gc, struct uns_ctr *root, size_t extent); +void uns_string_append_bytes(Uns_GC gc, struct uns_ctr *to, const void *from, size_t bytes); -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); +void uns_string_append_char(Uns_GC gc, struct uns_ctr *root, char c); +char *uns_string_cstring(Uns_GC gc, struct uns_ctr *root); #endif |
