58 lines
1.3 KiB
C
58 lines
1.3 KiB
C
#include "alloc.h"
|
|
|
|
#include <assert.h>
|
|
#include <refcount/refcount.h>
|
|
|
|
typedef struct A {
|
|
int num;
|
|
RefcountEntry refcount;
|
|
char *str;
|
|
} 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);
|
|
refcount_context_init_obj(c, a);
|
|
return a;
|
|
}
|
|
|
|
void destroy_callback(void *a_raw) {
|
|
A *a = a_raw;
|
|
counting_free(a->str);
|
|
counting_free(a);
|
|
}
|
|
|
|
int main() {
|
|
refcount_global_allocator = &COUNTING_ALLOCATOR;
|
|
|
|
RefcountContext *c =
|
|
refcount_make_context(offsetof(A, refcount), NULL, destroy_callback);
|
|
|
|
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);
|
|
assert(refcount_context_num_refs(c, a) == 1);
|
|
|
|
refcount_context_ref(c, a);
|
|
assert(refcount_context_num_refs(c, a) == 2);
|
|
|
|
refcount_context_unref(c, a);
|
|
assert(refcount_context_num_refs(c, a) == 1);
|
|
|
|
refcount_context_float(c, a);
|
|
assert(refcount_context_num_refs(c, a) == 0);
|
|
|
|
refcount_context_ref(c, a);
|
|
assert(refcount_context_num_refs(c, a) == 1);
|
|
|
|
refcount_context_unref(c, a);
|
|
|
|
refcount_context_destroy(c);
|
|
|
|
check_allocator_status();
|
|
return 0;
|
|
}
|