Mon, 01 Mar 2021 22:19:06 +0100
adds cxReallocate()
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 14 15:37:12 2021 +0100 1.2 +++ b/src/allocator.c Mon Mar 01 22:19:06 2021 +0100 1.3 @@ -29,6 +29,7 @@ 1.4 #include "cx/allocator.h" 1.5 1.6 #include <stdlib.h> 1.7 +#include <errno.h> 1.8 1.9 void *cx_malloc_stdlib(void *unused, size_t n) { 1.10 return malloc(n); 1.11 @@ -67,6 +68,20 @@ 1.12 return allocator->cl->realloc(allocator->data, mem, n); 1.13 } 1.14 1.15 +int cxReallocate(CxAllocator allocator, void **mem, size_t n) { 1.16 + if (mem == NULL) { 1.17 + errno = EINVAL; 1.18 + return 1; 1.19 + } 1.20 + void* nmem = allocator->cl->realloc(allocator->data, *mem, n); 1.21 + if (nmem == NULL) { 1.22 + return 1; 1.23 + } else { 1.24 + *mem = nmem; 1.25 + return 0; 1.26 + } 1.27 +} 1.28 + 1.29 void *cxCalloc(CxAllocator allocator, size_t nelem, size_t n) { 1.30 return allocator->cl->calloc(allocator->data, nelem, n); 1.31 }
2.1 --- a/src/cx/allocator.h Sun Feb 14 15:37:12 2021 +0100 2.2 +++ b/src/cx/allocator.h Mon Mar 01 22:19:06 2021 +0100 2.3 @@ -52,6 +52,8 @@ 2.4 2.5 void *cxRealloc(CxAllocator allocator, void *mem, size_t n); 2.6 2.7 +int cxReallocate(CxAllocator allocator, void **mem, size_t n); 2.8 + 2.9 void *cxCalloc(CxAllocator allocator, size_t nelem, size_t n); 2.10 2.11 void cxFree(CxAllocator allocator, void *mem);
3.1 --- a/test/test_allocator.c Sun Feb 14 15:37:12 2021 +0100 3.2 +++ b/test/test_allocator.c Mon Mar 01 22:19:06 2021 +0100 3.3 @@ -49,6 +49,21 @@ 3.4 free(test); 3.5 } 3.6 3.7 +void test_default_reallocate(void) { 3.8 + void *test = calloc(8, 1); 3.9 + memcpy(test, "Test", 4); 3.10 + int rval = cxReallocate(cxDefaultAllocator, &test, 16); 3.11 + CU_ASSERT_EQUAL(rval, 0); 3.12 + CU_ASSERT_PTR_NOT_NULL(test) 3.13 + CU_ASSERT_STRING_EQUAL("Test", test) 3.14 + free(test); 3.15 +} 3.16 + 3.17 +void test_reallocate_null(void) { 3.18 + int rval = cxReallocate(cxDefaultAllocator, NULL, 16); 3.19 + CU_ASSERT_EQUAL(rval, EINVAL); 3.20 +} 3.21 + 3.22 void test_default_calloc(void) { 3.23 void *test = cxCalloc(cxDefaultAllocator, 8, 2); 3.24 CU_ASSERT_PTR_NOT_NULL(test) 3.25 @@ -78,7 +93,9 @@ 3.26 !CU_add_test(suite, "default allocator available", test_default_allocator_available) || 3.27 !CU_add_test(suite, "test of malloc()", test_default_malloc)|| 3.28 !CU_add_test(suite, "test of realloc()", test_default_realloc) || 3.29 - !CU_add_test(suite, "test of realloc()", test_default_calloc) || 3.30 + !CU_add_test(suite, "test of realloc() via cxReallocate", test_default_reallocate) || 3.31 + !CU_add_test(suite, "test of cxReallocate with NULL", test_default_reallocate) || 3.32 + !CU_add_test(suite, "test of calloc()", test_default_calloc) || 3.33 !CU_add_test(suite, "test of free()", test_default_free) 3.34 ) { 3.35 CU_cleanup_registry();