Sun, 07 Feb 2021 17:17:46 +0100
make cx_allocator_class shared
src/allocator.c | file | annotate | diff | comparison | revisions | |
src/cx/allocator.h | file | annotate | diff | comparison | revisions | |
test/test_allocator.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/allocator.c Sun Feb 07 16:52:21 2021 +0100 1.2 +++ b/src/allocator.c Sun Feb 07 17:17:46 2021 +0100 1.3 @@ -30,13 +30,15 @@ 1.4 1.5 #include <stdlib.h> 1.6 1.7 +cx_allocator_class cx_default_allocator_class = { 1.8 + cx_malloc_stdlib, 1.9 + cx_realloc_stdlib, 1.10 + cx_calloc_stdlib, 1.11 + cx_free_stdlib 1.12 +}; 1.13 + 1.14 struct cx_allocator_s cx_default_allocator = { 1.15 - { 1.16 - cx_malloc_stdlib, 1.17 - cx_realloc_stdlib, 1.18 - cx_calloc_stdlib, 1.19 - cx_free_stdlib 1.20 - }, 1.21 + &cx_default_allocator_class, 1.22 NULL 1.23 }; 1.24 CxAllocator cxDefaultAllocator = &cx_default_allocator; 1.25 @@ -58,17 +60,17 @@ 1.26 } 1.27 1.28 void* cxMalloc(CxAllocator allocator, size_t n) { 1.29 - return allocator->allocatorClass.malloc(allocator, n); 1.30 + return allocator->cl->malloc(allocator->data, n); 1.31 } 1.32 1.33 void* cxRealloc(CxAllocator allocator, void* mem, size_t n) { 1.34 - return allocator->allocatorClass.realloc(allocator, mem, n); 1.35 + return allocator->cl->realloc(allocator->data, mem, n); 1.36 } 1.37 1.38 void* cxCalloc(CxAllocator allocator, size_t nelem, size_t n) { 1.39 - return allocator->allocatorClass.calloc(allocator, nelem, n); 1.40 + return allocator->cl->calloc(allocator->data, nelem, n); 1.41 } 1.42 1.43 void cxFree(CxAllocator allocator, void* mem) { 1.44 - allocator->allocatorClass.free(allocator, mem); 1.45 + allocator->cl->free(allocator->data, mem); 1.46 } 1.47 \ No newline at end of file
2.1 --- a/src/cx/allocator.h Sun Feb 07 16:52:21 2021 +0100 2.2 +++ b/src/cx/allocator.h Sun Feb 07 17:17:46 2021 +0100 2.3 @@ -44,18 +44,20 @@ 2.4 void* cx_calloc_stdlib(cx_allocator a, size_t nelem, size_t n); 2.5 void cx_free_stdlib(cx_allocator a, void* mem); 2.6 2.7 -struct cx_allocator_class { 2.8 +typedef struct { 2.9 cx_malloc_func malloc; 2.10 cx_realloc_func realloc; 2.11 cx_calloc_func calloc; 2.12 cx_free_func free; 2.13 -}; 2.14 +} cx_allocator_class; 2.15 + 2.16 +extern cx_allocator_class cx_default_allocator_class; 2.17 2.18 /* HIGH LEVEL API */ 2.19 2.20 struct cx_allocator_s { 2.21 - struct cx_allocator_class allocatorClass; 2.22 - void* data; 2.23 + cx_allocator_class *cl; 2.24 + cx_allocator data; 2.25 }; 2.26 typedef struct cx_allocator_s* CxAllocator; 2.27
3.1 --- a/test/test_allocator.c Sun Feb 07 16:52:21 2021 +0100 3.2 +++ b/test/test_allocator.c Sun Feb 07 17:17:46 2021 +0100 3.3 @@ -31,11 +31,11 @@ 3.4 #include <CUnit/Basic.h> 3.5 3.6 void test_default_allocator_available(void) { 3.7 - struct cx_allocator_class clazz = cxDefaultAllocator->allocatorClass; 3.8 - CU_ASSERT_PTR_EQUAL(clazz.malloc, cx_malloc_stdlib) 3.9 - CU_ASSERT_PTR_EQUAL(clazz.realloc, cx_realloc_stdlib) 3.10 - CU_ASSERT_PTR_EQUAL(clazz.calloc, cx_calloc_stdlib) 3.11 - CU_ASSERT_PTR_EQUAL(clazz.free, cx_free_stdlib) 3.12 + cx_allocator_class* clazz = cxDefaultAllocator->cl; 3.13 + CU_ASSERT_PTR_EQUAL(clazz->malloc, cx_malloc_stdlib) 3.14 + CU_ASSERT_PTR_EQUAL(clazz->realloc, cx_realloc_stdlib) 3.15 + CU_ASSERT_PTR_EQUAL(clazz->calloc, cx_calloc_stdlib) 3.16 + CU_ASSERT_PTR_EQUAL(clazz->free, cx_free_stdlib) 3.17 } 3.18 3.19 void test_default_malloc(void) {