Change to C99 and add weak references

This commit is contained in:
2025-09-05 20:01:36 -07:00
parent 4efdcc97ae
commit 12e3923938
8 changed files with 295 additions and 51 deletions

View File

@ -8,7 +8,7 @@
#include <stdlib.h>
#include <string.h>
#if __has_attribute(unused)
#if defined(__has_attribute) && __has_attribute(unused)
# define UNUSED __attribute__((unused))
#else
# define UNUSED
@ -67,8 +67,8 @@ static UNUSED char *counting_strdup(const char *str) {
}
static UNUSED const RefcountAllocator COUNTING_ALLOCATOR = {
.malloc = counting_malloc,
.free = counting_free,
.malloc.no_data = counting_malloc,
.free.no_data = counting_free,
};
#endif

View File

@ -113,6 +113,42 @@ int main(int argc, const char **argv) {
assert(refcount_context_garbage_collect(c) == 26);
a = make_a(c, 0, "a");
RefcountWeakref *w = refcount_context_make_weakref(c, a);
assert(w);
assert(refcount_context_num_refs(c, a) == 0);
b = refcount_context_strengthen(c, w);
assert(b);
assert(a == b);
assert(refcount_context_num_refs(c, a) == 1);
refcount_context_ref(c, a);
assert(refcount_context_num_refs(c, a) == 2);
RefcountWeakref *x = refcount_context_weaken(c, a);
w = refcount_context_make_weakref(c, a);
assert(refcount_context_num_refs(c, a) == 1);
assert(w != x);
refcount_context_destroy_weakref(c, x);
refcount_context_ref(c, a);
assert(refcount_context_num_refs(c, a) == 2);
x = refcount_context_weaken(c, a);
assert(refcount_context_num_refs(c, a) == 1);
assert(w != x);
assert(refcount_context_weakref_is_valid(c, w));
assert(refcount_context_weakref_is_valid(c, x));
assert(!refcount_context_unref(c, a));
assert(!refcount_context_weakref_is_valid(c, w));
assert(!refcount_context_weakref_is_valid(c, x));
refcount_context_destroy_weakref(c, w);
refcount_context_destroy_weakref(c, x);
refcount_context_destroy(c);
check_allocator_status();