require consumers to supply their own instance of the collector, initialize it

This commit is contained in:
Jeremy Tregunna 2016-04-26 22:07:37 -06:00
parent 002caadb81
commit 6a7d90145c
No known key found for this signature in database
GPG Key ID: 1A042D7269D00255
3 changed files with 39 additions and 35 deletions

View File

@ -8,13 +8,10 @@ static inline refhdr_t* _refuse_header(void* ptr)
return (refhdr_t*)((char*)ptr - sizeof(refhdr_t));
}
refuse_t* refuse_new(void)
void refuse_init(refuse_t* refuse)
{
refuse_t* refuse = malloc(sizeof(refuse_t));
refuse->dec = refdeque_alloc();
refuse->mod = refdeque_alloc();
return refuse;
refuse->dec = refdeque_alloc();
refuse->mod = refdeque_alloc();
}
void refuse_destroy(refuse_t* refuse)

View File

@ -18,12 +18,12 @@ typedef struct
refdeque_t* mod;
} refuse_t;
// Create a new garbage collector. It should be noted, that refuse is
// Initialize the garbage collector. It should be noted, that refuse is
// not yet thread safe. Therefore it's recommended that refuse run in
// its own thread. Objects may be shared across thread boundaries for
// the purposes of your use case, however, all operations, save for
// refuse_alloc, must be run on the thread that owns refuse.
extern refuse_t* refuse_new(void);
extern void refuse_init(refuse_t*);
// Allocate a chunk of memory and place it under control of an instance
// of refuse. The pointer returned points at the start of memory for

61
tests.c
View File

@ -11,17 +11,20 @@ struct mock
int a;
};
START_TEST(test_refuse_new)
START_TEST(test_refuse_init)
{
refuse_t* refuse = refuse_new();
ck_assert(refuse != NULL);
refuse_t refuse = {0};
refuse_init(&refuse);
ck_assert(refuse.mod != NULL);
ck_assert(refuse.dec != NULL);
}
END_TEST
START_TEST(test_refuse_alloc_1)
{
refuse_t* refuse = refuse_new();
struct mock* t = refuse_alloc(refuse, sizeof(struct mock));
refuse_t refuse = {0};
refuse_init(&refuse);
struct mock* t = refuse_alloc(&refuse, sizeof(struct mock));
ck_assert(t != NULL);
refhdr_t* hdr = (refhdr_t*)((char*)t - sizeof(refhdr_t));
ck_assert_int_eq(hdr->retainCount, 0);
@ -32,15 +35,16 @@ END_TEST
START_TEST(test_refuse_retain_1)
{
refuse_t* refuse = refuse_new();
struct mock* t = refuse_alloc(refuse, sizeof(struct mock));
refuse_t refuse = {0};
refuse_init(&refuse);
struct mock* t = refuse_alloc(&refuse, sizeof(struct mock));
t->a = 42;
refuse_retain(refuse, t);
refuse_retain(&refuse, t);
refhdr_t* hdr = (refhdr_t*)((char*)t - sizeof(refhdr_t));
ck_assert_int_eq(hdr->retainCount, 0);
ck_assert_int_eq(hdr->new, 1);
ck_assert_int_eq(hdr->dirty, 0);
refhdr_t* popped = refdeque_pop_back(refuse->mod);
refhdr_t* popped = refdeque_pop_back(refuse.mod);
struct mock* s = (struct mock*)((char*)popped + sizeof(refhdr_t));
ck_assert_int_eq(s->a, 42);
}
@ -48,14 +52,15 @@ END_TEST
START_TEST(test_refuse_retain_2)
{
refuse_t* refuse = refuse_new();
struct mock* t = refuse_alloc(refuse, sizeof(struct mock));
refuse_retain(refuse, t);
refuse_t refuse = {0};
refuse_init(&refuse);
struct mock* t = refuse_alloc(&refuse, sizeof(struct mock));
refuse_retain(&refuse, t);
refhdr_t* hdr = (refhdr_t*)((char*)t - sizeof(refhdr_t));
ck_assert_int_eq(hdr->retainCount, 0);
ck_assert_int_eq(hdr->new, 1);
ck_assert_int_eq(hdr->dirty, 0);
refuse_reconcile(refuse);
refuse_reconcile(&refuse);
ck_assert_int_eq(hdr->new, 0);
ck_assert_int_eq(hdr->retainCount, 1);
}
@ -63,28 +68,30 @@ END_TEST
START_TEST(test_refuse_retain_3)
{
refuse_t* refuse = refuse_new();
struct mock* t = refuse_alloc(refuse, sizeof(struct mock));
refuse_set_dirty(refuse, t);
refuse_retain(refuse, t);
refuse_t refuse = {0};
refuse_init(&refuse);
struct mock* t = refuse_alloc(&refuse, sizeof(struct mock));
refuse_set_dirty(&refuse, t);
refuse_retain(&refuse, t);
refhdr_t* hdr = (refhdr_t*)((char*)t - sizeof(refhdr_t));
ck_assert_int_eq(hdr->retainCount, 0);
refuse_reconcile(refuse);
refuse_reconcile(&refuse);
ck_assert_int_eq(hdr->retainCount, 0);
}
END_TEST
START_TEST(test_refuse_release_1)
{
refuse_t* refuse = refuse_new();
struct mock* t = refuse_alloc(refuse, sizeof(struct mock));
refuse_retain(refuse, t);
refuse_reconcile(refuse);
refuse_t refuse = {0};
refuse_init(&refuse);
struct mock* t = refuse_alloc(&refuse, sizeof(struct mock));
refuse_retain(&refuse, t);
refuse_reconcile(&refuse);
refhdr_t* hdr = (refhdr_t*)((char*)t - sizeof(refhdr_t));
ck_assert_int_eq(hdr->retainCount, 1);
refuse_release(refuse, t);
refuse_release(&refuse, t);
ck_assert_int_eq(hdr->retainCount, 1);
refuse_reconcile(refuse);
refuse_reconcile(&refuse);
ck_assert_int_eq(hdr->retainCount, 0);
}
END_TEST
@ -93,9 +100,9 @@ static Suite* refuse_suite(void)
{
Suite *s = suite_create("refuse");
TCase* tc_refuse_new = tcase_create("refuse_new");
tcase_add_test(tc_refuse_new, test_refuse_new);
suite_add_tcase(s, tc_refuse_new);
TCase* tc_refuse_init = tcase_create("refuse_init");
tcase_add_test(tc_refuse_init, test_refuse_init);
suite_add_tcase(s, tc_refuse_init);
TCase* tc_refuse_alloc = tcase_create("refuse_alloc");
tcase_add_test(tc_refuse_alloc, test_refuse_alloc_1);