Skip to content

Commit

Permalink
add CT_DEF_MPOOL macro, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jul 23, 2016
1 parent c41912e commit 636c068
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 29 deletions.
7 changes: 7 additions & 0 deletions src/mem/mpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
#define CT_MP_FREE(pool, ptr) \
((pool) != NULL ? ct_mpool_free_block(pool, (ptr)) : free(ptr))

#define CT_DEF_MPOOL(name, num, type) \
CT_MPool name; \
if (ct_mpool_init(&(name), (num), sizeof(type))) { \
CT_ERROR("out of memory (mpool)"); \
goto fail; \
}

CT_BEGIN_DECLS

typedef struct CT_MPoolFreeList CT_MPoolFreeList;
Expand Down
5 changes: 3 additions & 2 deletions test/cons.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ int test_cons() {
ct_cons_iterate(c, trace_cons, NULL);
ct_cons_free_all(c, NULL, NULL, NULL);

CT_MPool pool;
ct_mpool_init(&pool, 256, sizeof(CT_Cons));
CT_DEF_MPOOL(pool, 256, CT_Cons);
c = ct_cons("CCC", ct_cons("BBB", ct_cons("AAA", NULL, &pool), &pool), &pool);

ct_cons_iterate(c, count_cons, reset_state(&cs, NULL));
Expand Down Expand Up @@ -95,4 +94,6 @@ int test_cons() {

ct_mpool_free(&pool);
return 0;
fail:
return 1;
}
10 changes: 6 additions & 4 deletions test/hashset.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ int test_hashset_char() {
}

int test_hashset_vec() {
CT_MPool vpool;
CT_IS(!ct_mpool_init(&vpool, 16, sizeof(CT_Vec3f)), "init vpool");
CT_DEF_MPOOL(vpool, 16, CT_Vec3f);
CT_Hashset s, s2;
CT_HSOps ops = {.hash = ct_murmur3_32,
.alloc_key = hs_alloc_mpool,
Expand All @@ -130,6 +129,8 @@ int test_hashset_vec() {
ct_hs_free(&s2);
ct_mpool_free(&vpool);
return 0;
fail:
return 1;
}

int test_hashset_edge() {
Expand Down Expand Up @@ -160,11 +161,10 @@ int bench_hashset() {
for (size_t k = 0; k < 20; k++) {
clock_t begin = clock();
CT_Hashset s;
CT_MPool vpool;
CT_HSOps ops = {.hash = ct_murmur3_32};
uint32_t num = 1e6;
CT_IS(!ct_hs_init(&s, &ops, num, 0x10000, CT_HS_CONST_KEYS), "init hs");
CT_IS(!ct_mpool_init(&vpool, num, sizeof(CT_Vec3f)), "init vpool");
CT_DEF_MPOOL(vpool, num, CT_Vec3f);
for (size_t i = 0; i < num; i++) {
ct_hs_assoc(&s, ct_vec3f(ct_rand_norm() * 1000, ct_rand_norm() * 1000,
ct_rand_norm() * 1000, &vpool),
Expand All @@ -182,4 +182,6 @@ int bench_hashset() {
(double)(t2 - begin) * tscale, (double)(t3 - begin) * tscale);
}
return 0;
fail:
return 1;
}
10 changes: 6 additions & 4 deletions test/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ int test_hashtable_char() {
}

int test_hashtable_vec() {
CT_MPool vpool;
CT_IS(!ct_mpool_init(&vpool, 16, sizeof(CT_Vec3f)), "init vpool");
CT_DEF_MPOOL(vpool, 16, CT_Vec3f);
CT_Hashtable t, t2;
CT_HTOps ops = {.hash = ct_murmur3_32,
.alloc_key = ht_alloc_mpool,
Expand All @@ -145,6 +144,8 @@ int test_hashtable_vec() {
ct_ht_free(&t2);
ct_mpool_free(&vpool);
return 0;
fail:
return 1;
}

int test_hashtable_edge() {
Expand Down Expand Up @@ -176,12 +177,11 @@ ct_export int bench_hashtable() {
for (size_t k = 0; k < 20; k++) {
clock_t begin = clock();
CT_Hashtable t;
CT_MPool vpool;
CT_HTOps ops = {.hash = ct_murmur3_32};
uint32_t num = 1e6;
char *a = "a";
CT_IS(!ct_ht_init(&t, &ops, num, 0x10000, CT_HT_CONST_ALL), "init ht");
CT_IS(!ct_mpool_init(&vpool, num, sizeof(CT_Vec3f)), "init vpool");
CT_DEF_MPOOL(vpool, num, CT_Vec3f);
for (size_t i = 0; i < num; i++) {
ct_ht_assoc(&t, ct_vec3f(ct_rand_norm() * 1000, ct_rand_norm() * 1000,
ct_rand_norm() * 1000, &vpool),
Expand All @@ -199,4 +199,6 @@ ct_export int bench_hashtable() {
(double)(t2 - begin) * tscale, (double)(t3 - begin) * tscale);
}
return 0;
fail:
return 1;
}
3 changes: 3 additions & 0 deletions test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ int all_tests() {
#ifdef CT_FEATURE_CHECKS
CT_INFO("using checks...");
#endif
#ifdef CT_FEATURE_CHECK_MEM
CT_INFO("using mem checks...");
#endif
#ifdef CT_FEATURE_TRACE_MPOOL
CT_INFO("tracing mpool...");
#endif
Expand Down
10 changes: 6 additions & 4 deletions test/octree.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ int ct_octree_bounds(const CT_OTNode *node, void *state) {
int test_octree() {
CT_INFO("CT_Octree size: %zu", sizeof(CT_Octree));
CT_INFO("CT_OTNode size: %zu", sizeof(CT_OTNode));

CT_Octree t;
CT_MPool vpool;
ct_octree_init(&t, 0, 0, 0, 100, 100, 100, 0x10000);
ct_mpool_init(&vpool, 0x10000, sizeof(CT_Vec3f));

CT_DEF_MPOOL(vpool, 0x10000, CT_Vec3f);

CT_Vec3f *a = ct_vec3f(10, 10, 10, &vpool);
CT_Vec3f *b = ct_vec3f(10, 11, 10, &vpool);
CT_Vec3f *b2 = ct_vec3f(10.1, 11, 10, &vpool);
Expand Down Expand Up @@ -57,9 +59,9 @@ int test_octree() {
CT_IS(num == bounds.num, "wrong leaf count: %zu", bounds.num);
CT_INFO("%f,%f,%f -> %f,%f,%f %zu", bounds.min.x, bounds.min.y, bounds.min.z,
bounds.max.x, bounds.max.y, bounds.max.z, bounds.num);
//ct_octree_trace(&t);
ct_mpool_free(&vpool);
//ct_mpool_trace(&t.pool);
ct_octree_free(&t);
return 0;
fail:
return 1;
}
19 changes: 13 additions & 6 deletions test/poisson.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
CT_TEST_DECLS

int test_poisson() {
srand(0);
CT_DEF_MPOOL(vpool, 0x1000, CT_Vec2f);
int w = 600;
int h = 600;
float r = 10;
size_t count = 0;

CT_Quadtree t;
CT_MPool vpool;
int w = 600, h = 600;
CT_IS(!ct_qtree_init(&t, 0, 0, w, h, 0x1000), "init");
CT_IS(!ct_mpool_init(&vpool, 0x1000, sizeof(CT_Vec2f)), "init pool");
srand(0);
float r = 10;
size_t count = 0;

size_t failed = 0;
CT_Vec2f *p = (CT_Vec2f *)ct_mpool_alloc(&vpool);

while (1) {
if (!ct_poisson_sample2f(&t, r, 20, p)) {
p = (CT_Vec2f *)ct_mpool_alloc(&vpool);
Expand All @@ -25,7 +28,11 @@ int test_poisson() {
}
}
CT_IS(2000 < count, "count: %zu", count);

ct_mpool_free(&vpool);
ct_qtree_free(&t);
return 0;

fail:
return 1;
}
9 changes: 6 additions & 3 deletions test/quadtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ static void print_isec_results(struct isec_t *isec) {
int test_quadtree() {
CT_INFO("CT_Quadtree size: %zu", sizeof(CT_Quadtree));
CT_INFO("CT_QTNode size: %zu", sizeof(CT_QTNode));

CT_Quadtree t;
CT_MPool vpool;
ct_qtree_init(&t, 0, 0, 100, 100, 0x10000);
ct_mpool_init(&vpool, 0x10000, sizeof(CT_Vec2f));

CT_DEF_MPOOL(vpool, 0x10000, CT_Vec2f);

CT_Vec2f *a = ct_vec2f(10, 10, &vpool);
CT_Vec2f *b = ct_vec2f(10, 11, &vpool);
CT_Vec2f *b2 = ct_vec2f(10.1, 11, &vpool);
Expand Down Expand Up @@ -98,7 +100,8 @@ int test_quadtree() {
CT_INFO("%f,%f -> %f, %f, %zu", bounds.min.x, bounds.min.y, bounds.max.x,
bounds.max.y, bounds.num);
ct_mpool_free(&vpool);
//ct_mpool_trace(&t.pool);
ct_qtree_free(&t);
return 0;
fail:
return 1;
}
15 changes: 9 additions & 6 deletions test/vec.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ CT_TEST_DECLS
(float)(yy), (float)(zz));

int test_vec2f() {
CT_MPool pool;
CT_IS(!ct_mpool_init(&pool, 16, sizeof(CT_Vec2f)), "can't init mpool");
CT_DEF_MPOOL(pool, 16, CT_Vec2f);
CT_Vec2f *a = ct_vec2f(1, 2, &pool);
CT_Vec2f *b = ct_add2fn(a, 10, CT_MP_ALLOC(&pool, CT_Vec2f));
ASSERT_VEC2F(b, 11, 12);
Expand Down Expand Up @@ -52,11 +51,12 @@ int test_vec2f() {
CT_IS(1 == ct_compare2fv_xy(c, a), "c > a");
ct_mpool_free(&pool);
return 0;
fail:
return 1;
}

int test_vec3f() {
CT_MPool pool;
CT_IS(!ct_mpool_init(&pool, 16, sizeof(CT_Vec3f)), "can't init mpool");
CT_DEF_MPOOL(pool, 16, CT_Vec3f);
CT_Vec3f *a = ct_vec3f(1, 2, 3, &pool);
CT_Vec3f *b = ct_add3fn(a, 10, CT_MP_ALLOC(&pool, CT_Vec3f));
ASSERT_VEC3F(b, 11, 12, 13);
Expand Down Expand Up @@ -101,6 +101,8 @@ int test_vec3f() {
//bench_sse(a, b);
ct_mpool_free(&pool);
return 0;
fail:
return 1;
}

int test_swizzle() {
Expand Down Expand Up @@ -211,8 +213,7 @@ ct_export void bench_sse(CT_Vec3f *a, CT_Vec3f *b) {

int bench_vec3_create() {
uint32_t num = 1e6;
CT_MPool vpool;
CT_IS(!ct_mpool_init(&vpool, num, sizeof(CT_Vec3f)), "init vpool");
CT_DEF_MPOOL(vpool, num, CT_Vec3f);
for (size_t i = 0; i < num; i++) {
//ct_vec3f(ct_rand_norm() * 1000, ct_rand_norm() * 1000,
// ct_rand_norm() * 1000, &vpool);
Expand All @@ -221,4 +222,6 @@ int bench_vec3_create() {
}
ct_mpool_free(&vpool);
return 0;
fail:
return 1;
}

0 comments on commit 636c068

Please sign in to comment.