Sun, 07 Feb 2021 18:08:21 +0100
removes unnecessary cx_allocator typedef
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 17:17:46 2021 +0100 +++ b/src/allocator.c Sun Feb 07 18:08:21 2021 +0100 @@ -30,6 +30,22 @@ #include <stdlib.h> +void *cx_malloc_stdlib(void *unused, size_t n) { + return malloc(n); +} + +void *cx_realloc_stdlib(void *unused, void *mem, size_t n) { + return realloc(mem, n); +} + +void *cx_calloc_stdlib(void *unused, size_t nelem, size_t n) { + return calloc(nelem, n); +} + +void cx_free_stdlib(void *unused, void *mem) { + free(mem); +} + cx_allocator_class cx_default_allocator_class = { cx_malloc_stdlib, cx_realloc_stdlib, @@ -43,34 +59,18 @@ }; CxAllocator cxDefaultAllocator = &cx_default_allocator; -void* cx_malloc_stdlib(cx_allocator a, size_t n) { - return malloc(n); -} - -void* cx_realloc_stdlib(cx_allocator a, void* mem, size_t n) { - return realloc(mem, n); -} - -void* cx_calloc_stdlib(cx_allocator a, size_t nelem, size_t n) { - return calloc(nelem, n); -} - -void cx_free_stdlib(cx_allocator a, void* mem) { - free(mem); -} - -void* cxMalloc(CxAllocator allocator, size_t n) { +void *cxMalloc(CxAllocator allocator, size_t n) { return allocator->cl->malloc(allocator->data, n); } -void* cxRealloc(CxAllocator allocator, void* mem, size_t n) { +void *cxRealloc(CxAllocator allocator, void *mem, size_t n) { return allocator->cl->realloc(allocator->data, mem, n); } -void* cxCalloc(CxAllocator allocator, size_t nelem, size_t n) { +void *cxCalloc(CxAllocator allocator, size_t nelem, size_t n) { return allocator->cl->calloc(allocator->data, nelem, n); } -void cxFree(CxAllocator allocator, void* mem) { +void cxFree(CxAllocator allocator, void *mem) { allocator->cl->free(allocator->data, mem); -} \ No newline at end of file +}
--- a/src/cx/allocator.h Sun Feb 07 17:17:46 2021 +0100 +++ b/src/cx/allocator.h Sun Feb 07 18:08:21 2021 +0100 @@ -31,18 +31,13 @@ #include <stdlib.h> -/* LOW LEVEL API */ +typedef void *(*cx_malloc_func)(void *data, size_t n); + +typedef void *(*cx_realloc_func)(void *data, void *mem, size_t n); -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); -typedef void*(*cx_calloc_func)(cx_allocator a, size_t nelem, size_t n); -typedef void(*cx_free_func)(cx_allocator a, void* mem); +typedef void *(*cx_calloc_func)(void *data, size_t nelem, size_t n); -void* cx_malloc_stdlib(cx_allocator a, size_t n); -void* cx_realloc_stdlib(cx_allocator a, void* mem, size_t n); -void* cx_calloc_stdlib(cx_allocator a, size_t nelem, size_t n); -void cx_free_stdlib(cx_allocator a, void* mem); +typedef void(*cx_free_func)(void *data, void *mem); typedef struct { cx_malloc_func malloc; @@ -53,19 +48,20 @@ extern cx_allocator_class cx_default_allocator_class; -/* HIGH LEVEL API */ - struct cx_allocator_s { cx_allocator_class *cl; - cx_allocator data; + void *data; }; -typedef struct cx_allocator_s* CxAllocator; +typedef struct cx_allocator_s *CxAllocator; extern CxAllocator cxDefaultAllocator; -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); +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 17:17:46 2021 +0100 +++ b/test/test_allocator.c Sun Feb 07 18:08:21 2021 +0100 @@ -31,36 +31,33 @@ #include <CUnit/Basic.h> void test_default_allocator_available(void) { - 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) + cx_allocator_class *clazz = cxDefaultAllocator->cl; + CU_ASSERT_PTR_EQUAL(clazz, &cx_default_allocator_class) } void test_default_malloc(void) { - void* test = cxMalloc(cxDefaultAllocator, 16); + void *test = cxMalloc(cxDefaultAllocator, 16); CU_ASSERT_PTR_NOT_NULL(test); free(test); } void test_default_realloc(void) { - void* test = calloc(8, 1); + void *test = calloc(8, 1); memcpy(test, "Test", 4); test = cxRealloc(cxDefaultAllocator, test, 16); - CU_ASSERT_PTR_NOT_NULL(test); + CU_ASSERT_PTR_NOT_NULL(test) CU_ASSERT_STRING_EQUAL("Test", test) free(test); } void test_default_calloc(void) { - void* test = cxCalloc(cxDefaultAllocator, 8, 2); - CU_ASSERT_PTR_NOT_NULL(test); + void *test = cxCalloc(cxDefaultAllocator, 8, 2); + CU_ASSERT_PTR_NOT_NULL(test) free(test); } void test_default_free(void) { - void* test = malloc(16); + void *test = malloc(16); cxFree(cxDefaultAllocator, test); CU_PASS("Testing standard free is not possible.") } @@ -84,7 +81,7 @@ (NULL == CU_add_test(suite, "test of realloc()", test_default_realloc)) || (NULL == CU_add_test(suite, "test of realloc()", test_default_calloc)) || (NULL == CU_add_test(suite, "test of free()", test_default_free)) - ) { + ) { CU_cleanup_registry(); return CU_get_error(); }