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 |
--- a/src/allocator.c Sun Feb 07 15:35:52 2021 +0100 +++ b/src/allocator.c Sun Feb 07 16:24:41 2021 +0100 @@ -31,10 +31,12 @@ #include <stdlib.h> struct cx_allocator_s cx_default_allocator = { - cx_malloc_stdlib, - cx_realloc_stdlib, - cx_calloc_stdlib, - cx_free_stdlib, + { + cx_malloc_stdlib, + cx_realloc_stdlib, + cx_calloc_stdlib, + cx_free_stdlib + }, NULL }; CxAllocator cxDefaultAllocator = &cx_default_allocator; @@ -54,3 +56,19 @@ void cx_free_stdlib(cx_allocator a, void* mem) { free(mem); } + +void* cxMalloc(CxAllocator allocator, size_t n) { + return allocator->allocatorClass.malloc(allocator, n); +} + +void* cxRealloc(CxAllocator allocator, void* mem, size_t n) { + return allocator->allocatorClass.realloc(allocator, mem, n); +} + +void* cxCalloc(CxAllocator allocator, size_t nelem, size_t n) { + return allocator->allocatorClass.calloc(allocator, nelem, n); +} + +void cxFree(CxAllocator allocator, void* mem) { + allocator->allocatorClass.free(allocator, mem); +} \ No newline at end of file
--- a/src/cx/allocator.h Sun Feb 07 15:35:52 2021 +0100 +++ b/src/cx/allocator.h Sun Feb 07 16:24:41 2021 +0100 @@ -31,6 +31,8 @@ #include <stdlib.h> +/* LOW LEVEL API */ + typedef void* cx_allocator; typedef void*(*cx_malloc_func)(cx_allocator a, size_t n); typedef void*(*cx_realloc_func)(cx_allocator a, void* mem, size_t n); @@ -42,20 +44,26 @@ 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_s { +struct cx_allocator_class { cx_malloc_func malloc; cx_realloc_func realloc; cx_calloc_func calloc; cx_free_func free; +}; + +/* HIGH LEVEL API */ + +struct cx_allocator_s { + struct cx_allocator_class allocatorClass; void* data; }; typedef struct cx_allocator_s* CxAllocator; extern CxAllocator cxDefaultAllocator; -#define cxMalloc(a, n) (a)->malloc((a), n) -#define cxRealloc(a, mem, n) (a)->realloc((a), mem, n) -#define cxCalloc(a, nelem, n) (a)->calloc((a), nelem, n) -#define cxFree(a, mem) (a)->free((a), mem) +void* cxMalloc(CxAllocator allocator, size_t n); +void* cxRealloc(CxAllocator allocator, void* mem, size_t n); +void* cxCalloc(CxAllocator allocator, size_t nelem, size_t n); +void cxFree(CxAllocator allocator, void* mem); #endif /* UCX_ALLOCATOR_H */
--- a/test/test_allocator.c Sun Feb 07 15:35:52 2021 +0100 +++ b/test/test_allocator.c Sun Feb 07 16:24:41 2021 +0100 @@ -31,10 +31,11 @@ #include <CUnit/Basic.h> void test_default_allocator_available(void) { - CU_ASSERT_PTR_EQUAL(cxDefaultAllocator->malloc, cx_malloc_stdlib) - CU_ASSERT_PTR_EQUAL(cxDefaultAllocator->realloc, cx_realloc_stdlib) - CU_ASSERT_PTR_EQUAL(cxDefaultAllocator->calloc, cx_calloc_stdlib) - CU_ASSERT_PTR_EQUAL(cxDefaultAllocator->free, cx_free_stdlib) + 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) } void test_default_malloc(void) {