121 lines
3.0 KiB
C
121 lines
3.0 KiB
C
#include "alloc.h"
|
|
|
|
#include <assert.h>
|
|
#include <refcount/refcount.h>
|
|
|
|
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;
|
|
}
|
|
|
|
bool held_refs_callback(void *a_raw, RefcountList **out, void *ignored) {
|
|
A *a = a_raw;
|
|
*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(int argc, const char **argv) {
|
|
RefcountContext *c =
|
|
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);
|
|
|
|
a = refcount_context_ref(c, a);
|
|
assert(refcount_context_num_refs(c, a) == 1);
|
|
|
|
a = refcount_context_ref(c, a);
|
|
assert(refcount_context_num_refs(c, a) == 2);
|
|
|
|
a = refcount_context_unref(c, a);
|
|
assert(refcount_context_num_refs(c, a) == 1);
|
|
|
|
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_unref(c, a);
|
|
|
|
a = NULL;
|
|
for (char i = 'a'; i <= 'z'; ++i) {
|
|
A *na = make_a(c, i, "");
|
|
na->next = refcount_context_ref(c, a);
|
|
a = na;
|
|
}
|
|
assert(refcount_context_num_refs(c, a) == 0);
|
|
|
|
refcount_context_unref(c, a);
|
|
|
|
A *first = NULL;
|
|
a = NULL;
|
|
for (char i = 'a'; i <= 'z'; ++i) {
|
|
A *na = make_a(c, i, "");
|
|
if (!first) {
|
|
first = na;
|
|
}
|
|
na->next = refcount_context_ref(c, a);
|
|
a = na;
|
|
}
|
|
assert(refcount_context_num_refs(c, a) == 0);
|
|
first->next = refcount_context_ref(c, a);
|
|
a = first;
|
|
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);
|
|
|
|
a = NULL;
|
|
for (char i = 'a'; i <= 'z'; ++i) {
|
|
A *na = make_a(c, i, "");
|
|
na->next = refcount_context_ref(c, a);
|
|
a = na;
|
|
}
|
|
assert(refcount_context_num_refs(c, a) == 0);
|
|
refcount_context_unref(c, a);
|
|
|
|
assert(refcount_context_garbage_collect(c) == 26);
|
|
|
|
refcount_context_destroy(c);
|
|
|
|
check_allocator_status();
|
|
return 0;
|
|
}
|