# HG changeset patch # User Mike Becker # Date 1612714666 -3600 # Node ID 3539dd99ab926a5a8bc7bb751cdb87d6ec4d01e4 # Parent dc1bfe5ffd3898fb27b960fb4080a93283bfc4c1 make cx_allocator_class shared diff -r dc1bfe5ffd38 -r 3539dd99ab92 src/allocator.c --- a/src/allocator.c Sun Feb 07 16:52:21 2021 +0100 +++ b/src/allocator.c Sun Feb 07 17:17:46 2021 +0100 @@ -30,13 +30,15 @@ #include +cx_allocator_class cx_default_allocator_class = { + cx_malloc_stdlib, + cx_realloc_stdlib, + cx_calloc_stdlib, + cx_free_stdlib +}; + struct cx_allocator_s cx_default_allocator = { - { - cx_malloc_stdlib, - cx_realloc_stdlib, - cx_calloc_stdlib, - cx_free_stdlib - }, + &cx_default_allocator_class, NULL }; CxAllocator cxDefaultAllocator = &cx_default_allocator; @@ -58,17 +60,17 @@ } void* cxMalloc(CxAllocator allocator, size_t n) { - return allocator->allocatorClass.malloc(allocator, n); + return allocator->cl->malloc(allocator->data, n); } void* cxRealloc(CxAllocator allocator, void* mem, size_t n) { - return allocator->allocatorClass.realloc(allocator, mem, n); + return allocator->cl->realloc(allocator->data, mem, n); } void* cxCalloc(CxAllocator allocator, size_t nelem, size_t n) { - return allocator->allocatorClass.calloc(allocator, nelem, n); + return allocator->cl->calloc(allocator->data, nelem, n); } void cxFree(CxAllocator allocator, void* mem) { - allocator->allocatorClass.free(allocator, mem); + allocator->cl->free(allocator->data, mem); } \ No newline at end of file diff -r dc1bfe5ffd38 -r 3539dd99ab92 src/cx/allocator.h --- a/src/cx/allocator.h Sun Feb 07 16:52:21 2021 +0100 +++ b/src/cx/allocator.h Sun Feb 07 17:17:46 2021 +0100 @@ -44,18 +44,20 @@ void* cx_calloc_stdlib(cx_allocator a, size_t nelem, size_t n); void cx_free_stdlib(cx_allocator a, void* mem); -struct cx_allocator_class { +typedef struct { cx_malloc_func malloc; cx_realloc_func realloc; cx_calloc_func calloc; cx_free_func free; -}; +} cx_allocator_class; + +extern cx_allocator_class cx_default_allocator_class; /* HIGH LEVEL API */ struct cx_allocator_s { - struct cx_allocator_class allocatorClass; - void* data; + cx_allocator_class *cl; + cx_allocator data; }; typedef struct cx_allocator_s* CxAllocator; diff -r dc1bfe5ffd38 -r 3539dd99ab92 test/test_allocator.c --- a/test/test_allocator.c Sun Feb 07 16:52:21 2021 +0100 +++ b/test/test_allocator.c Sun Feb 07 17:17:46 2021 +0100 @@ -31,11 +31,11 @@ #include void test_default_allocator_available(void) { - struct cx_allocator_class clazz = cxDefaultAllocator->allocatorClass; - CU_ASSERT_PTR_EQUAL(clazz.malloc, cx_malloc_stdlib) - CU_ASSERT_PTR_EQUAL(clazz.realloc, cx_realloc_stdlib) - CU_ASSERT_PTR_EQUAL(clazz.calloc, cx_calloc_stdlib) - CU_ASSERT_PTR_EQUAL(clazz.free, cx_free_stdlib) + cx_allocator_class* clazz = cxDefaultAllocator->cl; + CU_ASSERT_PTR_EQUAL(clazz->malloc, cx_malloc_stdlib) + CU_ASSERT_PTR_EQUAL(clazz->realloc, cx_realloc_stdlib) + CU_ASSERT_PTR_EQUAL(clazz->calloc, cx_calloc_stdlib) + CU_ASSERT_PTR_EQUAL(clazz->free, cx_free_stdlib) } void test_default_malloc(void) {