# HG changeset patch # User Mike Becker # Date 1612711481 -3600 # Node ID 80c31ebd66c1243b505a6f018ba5b3bbba201f3e # Parent 8c0421ccbb586b717d464b1e0b839db5c7530bc4 high level allocator functions should be real functions, not macros diff -r 8c0421ccbb58 -r 80c31ebd66c1 src/allocator.c --- 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 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 diff -r 8c0421ccbb58 -r 80c31ebd66c1 src/cx/allocator.h --- 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 +/* 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 */ diff -r 8c0421ccbb58 -r 80c31ebd66c1 test/test_allocator.c --- 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 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) {