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 |
1.1 --- a/src/allocator.c Sun Feb 07 17:17:46 2021 +0100 1.2 +++ b/src/allocator.c Sun Feb 07 18:08:21 2021 +0100 1.3 @@ -30,6 +30,22 @@ 1.4 1.5 #include <stdlib.h> 1.6 1.7 +void *cx_malloc_stdlib(void *unused, size_t n) { 1.8 + return malloc(n); 1.9 +} 1.10 + 1.11 +void *cx_realloc_stdlib(void *unused, void *mem, size_t n) { 1.12 + return realloc(mem, n); 1.13 +} 1.14 + 1.15 +void *cx_calloc_stdlib(void *unused, size_t nelem, size_t n) { 1.16 + return calloc(nelem, n); 1.17 +} 1.18 + 1.19 +void cx_free_stdlib(void *unused, void *mem) { 1.20 + free(mem); 1.21 +} 1.22 + 1.23 cx_allocator_class cx_default_allocator_class = { 1.24 cx_malloc_stdlib, 1.25 cx_realloc_stdlib, 1.26 @@ -43,34 +59,18 @@ 1.27 }; 1.28 CxAllocator cxDefaultAllocator = &cx_default_allocator; 1.29 1.30 -void* cx_malloc_stdlib(cx_allocator a, size_t n) { 1.31 - return malloc(n); 1.32 -} 1.33 - 1.34 -void* cx_realloc_stdlib(cx_allocator a, void* mem, size_t n) { 1.35 - return realloc(mem, n); 1.36 -} 1.37 - 1.38 -void* cx_calloc_stdlib(cx_allocator a, size_t nelem, size_t n) { 1.39 - return calloc(nelem, n); 1.40 -} 1.41 - 1.42 -void cx_free_stdlib(cx_allocator a, void* mem) { 1.43 - free(mem); 1.44 -} 1.45 - 1.46 -void* cxMalloc(CxAllocator allocator, size_t n) { 1.47 +void *cxMalloc(CxAllocator allocator, size_t n) { 1.48 return allocator->cl->malloc(allocator->data, n); 1.49 } 1.50 1.51 -void* cxRealloc(CxAllocator allocator, void* mem, size_t n) { 1.52 +void *cxRealloc(CxAllocator allocator, void *mem, size_t n) { 1.53 return allocator->cl->realloc(allocator->data, mem, n); 1.54 } 1.55 1.56 -void* cxCalloc(CxAllocator allocator, size_t nelem, size_t n) { 1.57 +void *cxCalloc(CxAllocator allocator, size_t nelem, size_t n) { 1.58 return allocator->cl->calloc(allocator->data, nelem, n); 1.59 } 1.60 1.61 -void cxFree(CxAllocator allocator, void* mem) { 1.62 +void cxFree(CxAllocator allocator, void *mem) { 1.63 allocator->cl->free(allocator->data, mem); 1.64 -} 1.65 \ No newline at end of file 1.66 +}
2.1 --- a/src/cx/allocator.h Sun Feb 07 17:17:46 2021 +0100 2.2 +++ b/src/cx/allocator.h Sun Feb 07 18:08:21 2021 +0100 2.3 @@ -31,18 +31,13 @@ 2.4 2.5 #include <stdlib.h> 2.6 2.7 -/* LOW LEVEL API */ 2.8 +typedef void *(*cx_malloc_func)(void *data, size_t n); 2.9 2.10 -typedef void* cx_allocator; 2.11 -typedef void*(*cx_malloc_func)(cx_allocator a, size_t n); 2.12 -typedef void*(*cx_realloc_func)(cx_allocator a, void* mem, size_t n); 2.13 -typedef void*(*cx_calloc_func)(cx_allocator a, size_t nelem, size_t n); 2.14 -typedef void(*cx_free_func)(cx_allocator a, void* mem); 2.15 +typedef void *(*cx_realloc_func)(void *data, void *mem, size_t n); 2.16 2.17 -void* cx_malloc_stdlib(cx_allocator a, size_t n); 2.18 -void* cx_realloc_stdlib(cx_allocator a, void* mem, size_t n); 2.19 -void* cx_calloc_stdlib(cx_allocator a, size_t nelem, size_t n); 2.20 -void cx_free_stdlib(cx_allocator a, void* mem); 2.21 +typedef void *(*cx_calloc_func)(void *data, size_t nelem, size_t n); 2.22 + 2.23 +typedef void(*cx_free_func)(void *data, void *mem); 2.24 2.25 typedef struct { 2.26 cx_malloc_func malloc; 2.27 @@ -53,19 +48,20 @@ 2.28 2.29 extern cx_allocator_class cx_default_allocator_class; 2.30 2.31 -/* HIGH LEVEL API */ 2.32 - 2.33 struct cx_allocator_s { 2.34 cx_allocator_class *cl; 2.35 - cx_allocator data; 2.36 + void *data; 2.37 }; 2.38 -typedef struct cx_allocator_s* CxAllocator; 2.39 +typedef struct cx_allocator_s *CxAllocator; 2.40 2.41 extern CxAllocator cxDefaultAllocator; 2.42 2.43 -void* cxMalloc(CxAllocator allocator, size_t n); 2.44 -void* cxRealloc(CxAllocator allocator, void* mem, size_t n); 2.45 -void* cxCalloc(CxAllocator allocator, size_t nelem, size_t n); 2.46 -void cxFree(CxAllocator allocator, void* mem); 2.47 +void *cxMalloc(CxAllocator allocator, size_t n); 2.48 + 2.49 +void *cxRealloc(CxAllocator allocator, void *mem, size_t n); 2.50 + 2.51 +void *cxCalloc(CxAllocator allocator, size_t nelem, size_t n); 2.52 + 2.53 +void cxFree(CxAllocator allocator, void *mem); 2.54 2.55 #endif /* UCX_ALLOCATOR_H */
3.1 --- a/test/test_allocator.c Sun Feb 07 17:17:46 2021 +0100 3.2 +++ b/test/test_allocator.c Sun Feb 07 18:08:21 2021 +0100 3.3 @@ -31,36 +31,33 @@ 3.4 #include <CUnit/Basic.h> 3.5 3.6 void test_default_allocator_available(void) { 3.7 - cx_allocator_class* clazz = cxDefaultAllocator->cl; 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, &cx_default_allocator_class) 3.14 } 3.15 3.16 void test_default_malloc(void) { 3.17 - void* test = cxMalloc(cxDefaultAllocator, 16); 3.18 + void *test = cxMalloc(cxDefaultAllocator, 16); 3.19 CU_ASSERT_PTR_NOT_NULL(test); 3.20 free(test); 3.21 } 3.22 3.23 void test_default_realloc(void) { 3.24 - void* test = calloc(8, 1); 3.25 + void *test = calloc(8, 1); 3.26 memcpy(test, "Test", 4); 3.27 test = cxRealloc(cxDefaultAllocator, test, 16); 3.28 - CU_ASSERT_PTR_NOT_NULL(test); 3.29 + CU_ASSERT_PTR_NOT_NULL(test) 3.30 CU_ASSERT_STRING_EQUAL("Test", test) 3.31 free(test); 3.32 } 3.33 3.34 void test_default_calloc(void) { 3.35 - void* test = cxCalloc(cxDefaultAllocator, 8, 2); 3.36 - CU_ASSERT_PTR_NOT_NULL(test); 3.37 + void *test = cxCalloc(cxDefaultAllocator, 8, 2); 3.38 + CU_ASSERT_PTR_NOT_NULL(test) 3.39 free(test); 3.40 } 3.41 3.42 void test_default_free(void) { 3.43 - void* test = malloc(16); 3.44 + void *test = malloc(16); 3.45 cxFree(cxDefaultAllocator, test); 3.46 CU_PASS("Testing standard free is not possible.") 3.47 } 3.48 @@ -84,7 +81,7 @@ 3.49 (NULL == CU_add_test(suite, "test of realloc()", test_default_realloc)) || 3.50 (NULL == CU_add_test(suite, "test of realloc()", test_default_calloc)) || 3.51 (NULL == CU_add_test(suite, "test of free()", test_default_free)) 3.52 - ) { 3.53 + ) { 3.54 CU_cleanup_registry(); 3.55 return CU_get_error(); 3.56 }