fix root push and pop issue; make shared library for examples

This commit is contained in:
Peter McGoron 2024-06-20 23:14:25 -04:00
parent 5b8775dab6
commit 62f299dcf7
6 changed files with 33 additions and 8 deletions

View File

@ -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

View File

@ -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;

View File

@ -14,6 +14,7 @@
* License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
@ -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;
}

View File

@ -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; \

View File

@ -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

9
uns.c
View File

@ -15,12 +15,16 @@
* <https://www.gnu.org/licenses/>.
*/
#include <assert.h>
#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;
}