Sun, 07 Feb 2021 16:24:41 +0100
high level allocator functions should be real functions, not macros
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 15:35:52 2021 +0100 1.2 +++ b/src/allocator.c Sun Feb 07 16:24:41 2021 +0100 1.3 @@ -31,10 +31,12 @@ 1.4 #include <stdlib.h> 1.5 1.6 struct cx_allocator_s cx_default_allocator = { 1.7 - cx_malloc_stdlib, 1.8 - cx_realloc_stdlib, 1.9 - cx_calloc_stdlib, 1.10 - cx_free_stdlib, 1.11 + { 1.12 + cx_malloc_stdlib, 1.13 + cx_realloc_stdlib, 1.14 + cx_calloc_stdlib, 1.15 + cx_free_stdlib 1.16 + }, 1.17 NULL 1.18 }; 1.19 CxAllocator cxDefaultAllocator = &cx_default_allocator; 1.20 @@ -54,3 +56,19 @@ 1.21 void cx_free_stdlib(cx_allocator a, void* mem) { 1.22 free(mem); 1.23 } 1.24 + 1.25 +void* cxMalloc(CxAllocator allocator, size_t n) { 1.26 + return allocator->allocatorClass.malloc(allocator, n); 1.27 +} 1.28 + 1.29 +void* cxRealloc(CxAllocator allocator, void* mem, size_t n) { 1.30 + return allocator->allocatorClass.realloc(allocator, mem, n); 1.31 +} 1.32 + 1.33 +void* cxCalloc(CxAllocator allocator, size_t nelem, size_t n) { 1.34 + return allocator->allocatorClass.calloc(allocator, nelem, n); 1.35 +} 1.36 + 1.37 +void cxFree(CxAllocator allocator, void* mem) { 1.38 + allocator->allocatorClass.free(allocator, mem); 1.39 +} 1.40 \ No newline at end of file
2.1 --- a/src/cx/allocator.h Sun Feb 07 15:35:52 2021 +0100 2.2 +++ b/src/cx/allocator.h Sun Feb 07 16:24:41 2021 +0100 2.3 @@ -31,6 +31,8 @@ 2.4 2.5 #include <stdlib.h> 2.6 2.7 +/* LOW LEVEL API */ 2.8 + 2.9 typedef void* cx_allocator; 2.10 typedef void*(*cx_malloc_func)(cx_allocator a, size_t n); 2.11 typedef void*(*cx_realloc_func)(cx_allocator a, void* mem, size_t n); 2.12 @@ -42,20 +44,26 @@ 2.13 void* cx_calloc_stdlib(cx_allocator a, size_t nelem, size_t n); 2.14 void cx_free_stdlib(cx_allocator a, void* mem); 2.15 2.16 -struct cx_allocator_s { 2.17 +struct cx_allocator_class { 2.18 cx_malloc_func malloc; 2.19 cx_realloc_func realloc; 2.20 cx_calloc_func calloc; 2.21 cx_free_func free; 2.22 +}; 2.23 + 2.24 +/* HIGH LEVEL API */ 2.25 + 2.26 +struct cx_allocator_s { 2.27 + struct cx_allocator_class allocatorClass; 2.28 void* data; 2.29 }; 2.30 typedef struct cx_allocator_s* CxAllocator; 2.31 2.32 extern CxAllocator cxDefaultAllocator; 2.33 2.34 -#define cxMalloc(a, n) (a)->malloc((a), n) 2.35 -#define cxRealloc(a, mem, n) (a)->realloc((a), mem, n) 2.36 -#define cxCalloc(a, nelem, n) (a)->calloc((a), nelem, n) 2.37 -#define cxFree(a, mem) (a)->free((a), mem) 2.38 +void* cxMalloc(CxAllocator allocator, size_t n); 2.39 +void* cxRealloc(CxAllocator allocator, void* mem, size_t n); 2.40 +void* cxCalloc(CxAllocator allocator, size_t nelem, size_t n); 2.41 +void cxFree(CxAllocator allocator, void* mem); 2.42 2.43 #endif /* UCX_ALLOCATOR_H */
3.1 --- a/test/test_allocator.c Sun Feb 07 15:35:52 2021 +0100 3.2 +++ b/test/test_allocator.c Sun Feb 07 16:24:41 2021 +0100 3.3 @@ -31,10 +31,11 @@ 3.4 #include <CUnit/Basic.h> 3.5 3.6 void test_default_allocator_available(void) { 3.7 - CU_ASSERT_PTR_EQUAL(cxDefaultAllocator->malloc, cx_malloc_stdlib) 3.8 - CU_ASSERT_PTR_EQUAL(cxDefaultAllocator->realloc, cx_realloc_stdlib) 3.9 - CU_ASSERT_PTR_EQUAL(cxDefaultAllocator->calloc, cx_calloc_stdlib) 3.10 - CU_ASSERT_PTR_EQUAL(cxDefaultAllocator->free, cx_free_stdlib) 3.11 + struct cx_allocator_class clazz = cxDefaultAllocator->allocatorClass; 3.12 + CU_ASSERT_PTR_EQUAL(clazz.malloc, cx_malloc_stdlib) 3.13 + CU_ASSERT_PTR_EQUAL(clazz.realloc, cx_realloc_stdlib) 3.14 + CU_ASSERT_PTR_EQUAL(clazz.calloc, cx_calloc_stdlib) 3.15 + CU_ASSERT_PTR_EQUAL(clazz.free, cx_free_stdlib) 3.16 } 3.17 3.18 void test_default_malloc(void) {