From 62f299dcf7eaa1821cd3ea736bc9f40b93d2bac8 Mon Sep 17 00:00:00 2001 From: Peter McGoron Date: Thu, 20 Jun 2024 23:14:25 -0400 Subject: [PATCH] fix root push and pop issue; make shared library for examples --- README.rst | 6 ++++++ c89_relo.c | 4 ++-- cheney_c89.c | 15 ++++++++++++--- examples/string/Makefile | 3 +++ include/internal/c89_relo.h | 4 ++-- uns.c | 9 ++++++++- 6 files changed, 33 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index 8745af5..8988150 100644 --- a/README.rst +++ b/README.rst @@ -30,3 +30,9 @@ You can statically link any LGPL 3.0 code to code of permissive licenses (like MIT), and even to source-available license (like the SSPL or the Commons Clause) as long as the end user can recompile the program to use their own version of the library. + +---- +Todo +---- + +call before gc and after gc diff --git a/c89_relo.c b/c89_relo.c index f215c8f..6150781 100644 --- a/c89_relo.c +++ b/c89_relo.c @@ -56,13 +56,13 @@ static void check_len(void *rec, size_t i) assert(i * sizeof(void *) < uns_c89_relo_get_len(rec)); } -void uns_c89_relo_record_set(void *rec, size_t i, void *v) +void uns_c89_relo_record_set(Uns_ptr rec, size_t i, void *v) { check_len(rec, i); memcpy((unsigned char *)rec + i*sizeof(void*), &v, sizeof(v)); } -void *uns_c89_relo_record_get(void *rec, size_t i) +void *uns_c89_relo_record_get(Uns_ptr rec, size_t i) { void *v; diff --git a/cheney_c89.c b/cheney_c89.c index 25b248f..abdbb76 100644 --- a/cheney_c89.c +++ b/cheney_c89.c @@ -14,6 +14,7 @@ * License along with this program. If not, see * . */ +#include #include #include @@ -53,6 +54,11 @@ static void *raw_alloc(struct ctx *ctx, size_t len, int is_record) { unsigned char *p; +/* + printf("%lu (%ld)\n", (unsigned long)(len + sizeof(struct uns_c89_relo_hdr)), + (long)(ctx->tospace_end - ctx->tospace_alloc)); +*/ + assert(ctx->tospace_alloc + sizeof(struct uns_c89_relo_hdr) + len <= ctx->tospace_end); p = uns_c89_relo_init(ctx->tospace_alloc, len, is_record); ctx->tospace_alloc = p + len; @@ -89,8 +95,8 @@ static void scan_record(struct uns_gc *gc, unsigned char *p) void *newp; for (i = 0; i < l; i++) { - newp = relocate(gc, uns_c89_relo_record_get(p, i)); - uns_c89_relo_record_set(p, i, newp); + newp = relocate(gc, uns_c89_relo_record_get((void *)p, i)); + uns_c89_relo_record_set((void *)p, i, newp); } } @@ -118,6 +124,9 @@ int uns_cheney_c89_collect(struct uns_gc *gc) /* Setup statistics */ gc->before_collection = fromspace_lim - fromspace; +/* + printf("before: %ld\n", gc->before_collection); +*/ gc->after_collection = 0; gc->collection_number += 1; @@ -170,7 +179,7 @@ void *uns_cheney_c89_alloc_record(struct uns_gc *gc, size_t records) unsigned char *res = alloc(gc, records*sizeof(void *), 1); for (i = 0; i < records; i++) - uns_c89_relo_record_set(res, i, NULL); + uns_c89_relo_record_set((void *)res, i, NULL); return res; } diff --git a/examples/string/Makefile b/examples/string/Makefile index bf70827..6ec8882 100644 --- a/examples/string/Makefile +++ b/examples/string/Makefile @@ -4,6 +4,9 @@ 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; \ diff --git a/include/internal/c89_relo.h b/include/internal/c89_relo.h index 5347aa8..1226ce2 100644 --- a/include/internal/c89_relo.h +++ b/include/internal/c89_relo.h @@ -46,7 +46,7 @@ size_t uns_c89_relo_get_record_len(void *p); void *uns_c89_relo_init(void *p, size_t len_size, int is_record); -void uns_c89_relo_record_set(void *rec, size_t i, void *v); -void *uns_c89_relo_record_get(void *rec, size_t i); +void uns_c89_relo_record_set(Uns_ptr rec, size_t i, void *v); +void *uns_c89_relo_record_get(Uns_ptr rec, size_t i); #endif diff --git a/uns.c b/uns.c index 6ae6894..556a6aa 100644 --- a/uns.c +++ b/uns.c @@ -15,12 +15,16 @@ * . */ +#include #include "uns.h" void uns_root_add(struct uns_gc *gc, struct uns_ctr *root) { root->prev = NULL; root->next = gc->roots; + + if (gc->roots) + gc->roots->prev = root; gc->roots = root; } @@ -33,7 +37,10 @@ void uns_root_remove(struct uns_gc *gc, struct uns_ctr *root) root->next->prev = root->prev; } - if (gc->roots == root) + if (gc->roots == root) { + assert(!root->prev); gc->roots = root->next; + } + root->prev = root->next = NULL; }