Initial garbage colletor implementation

This commit is contained in:
2025-08-30 21:21:02 -07:00
parent 80e3f1a916
commit d78cf29765
11 changed files with 709 additions and 214 deletions

View File

@ -1,5 +1,5 @@
link_libraries(refcount)
add_compile_options(-Wall)
add_compile_options(-Wall -Wpedantic)
add_executable(test_list test_list.c)
add_test(NAME list COMMAND test_list)

View File

@ -68,7 +68,6 @@ static UNUSED char *counting_strdup(const char *str) {
static UNUSED const RefcountAllocator COUNTING_ALLOCATOR = {
.malloc = counting_malloc,
.realloc = counting_realloc,
.free = counting_free,
};

View File

@ -4,7 +4,11 @@
#include <refcount/list.h>
#include <string.h>
int main() {
void *counting_strdup_callback(const void *str, void *ignored) {
return counting_strdup(str);
}
int main(int argc, const char **argv) {
refcount_global_allocator = &COUNTING_ALLOCATOR;
assert(refcount_list_length(NULL) == 0);
@ -77,7 +81,7 @@ int main() {
assert(strcmp(refcount_list_nth(l, 3), "str4") == 0);
assert(strcmp(refcount_list_nth(l, 4), "str5") == 0);
RefcountList *l2 = refcount_list_copy(l, (void *) counting_strdup);
RefcountList *l2 = refcount_list_copy(l, counting_strdup_callback, NULL);
refcount_list_free(l, counting_free);

View File

@ -7,49 +7,78 @@ typedef struct A {
int num;
RefcountEntry refcount;
char *str;
struct A *next;
} A;
A *make_a(RefcountContext *c, int n, const char *s) {
A *a = counting_malloc(sizeof(A));
a->num = n;
a->str = counting_strdup(s);
a->next = NULL;
refcount_context_init_obj(c, a);
return a;
}
void destroy_callback(void *a_raw) {
bool held_refs_callback(void *a_raw, RefcountList **out, void *ignored) {
A *a = a_raw;
if (a->next) {
*out = refcount_list_push_full(*out, a->next, &COUNTING_ALLOCATOR);
}
return true;
}
void destroy_callback(void *a_raw, void *ignored) {
A *a = a_raw;
counting_free(a->str);
counting_free(a);
}
int main() {
refcount_global_allocator = &COUNTING_ALLOCATOR;
int main(int argc, const char **argv) {
RefcountContext *c =
refcount_make_context(offsetof(A, refcount), NULL, destroy_callback);
refcount_make_context(offsetof(A, refcount), held_refs_callback,
destroy_callback, NULL, &COUNTING_ALLOCATOR);
A *a = make_a(c, 10, "Hello world\n");
assert(!refcount_context_is_static(c, a));
assert(refcount_context_num_refs(c, a) == 0);
refcount_context_ref(c, a);
a = refcount_context_ref(c, a);
assert(refcount_context_num_refs(c, a) == 1);
refcount_context_ref(c, a);
a = refcount_context_ref(c, a);
assert(refcount_context_num_refs(c, a) == 2);
refcount_context_unref(c, a);
a = refcount_context_unref(c, a);
assert(refcount_context_num_refs(c, a) == 1);
refcount_context_float(c, a);
a = refcount_context_float(c, a);
assert(a);
assert(refcount_context_num_refs(c, a) == 0);
refcount_context_ref(c, a);
assert(refcount_context_num_refs(c, a) == 1);
a = refcount_context_unref(c, a);
assert(!a);
a = make_a(c, 10, "Hello World\n");
A *b = make_a(c, 42, "The answer!");
a->next = refcount_context_ref(c, b);
assert(refcount_context_num_refs(c, a->next) == 1);
assert(refcount_context_num_refs(c, a) == 0);
refcount_context_unref(c, a);
a = make_a(c, 'a', "a");
a->next = refcount_context_ref(c, a);
assert(refcount_context_num_refs(c, a) == 1);
refcount_context_ref(c, a);
refcount_context_unref(c, a);
assert(refcount_context_num_refs(c, a) == 1);
refcount_context_garbage_collect(c);
refcount_context_destroy(c);
check_allocator_status();