Sun, 26 Sep 2021 18:01:51 +0200
add attributes to allocator functions
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 Sep 26 17:58:27 2021 +0200 1.2 +++ b/src/allocator.c Sun Sep 26 18:01:51 2021 +0200 1.3 @@ -29,21 +29,23 @@ 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 +__attribute__((malloc)) 1.11 +void *cx_malloc_stdlib(__attribute__((unused)) void *d, size_t n) { 1.12 return malloc(n); 1.13 } 1.14 1.15 -void *cx_realloc_stdlib(void *unused, void *mem, size_t n) { 1.16 +void *cx_realloc_stdlib(__attribute__((unused)) void *d, void *mem, size_t n) { 1.17 return realloc(mem, n); 1.18 } 1.19 1.20 -void *cx_calloc_stdlib(void *unused, size_t nelem, size_t n) { 1.21 +__attribute__((malloc)) 1.22 +void *cx_calloc_stdlib(__attribute__((unused)) void *d, size_t nelem, size_t n) { 1.23 return calloc(nelem, n); 1.24 } 1.25 1.26 -void cx_free_stdlib(void *unused, void *mem) { 1.27 +__attribute__((nonnull)) 1.28 +void cx_free_stdlib(__attribute__((unused)) void *d, void *mem) { 1.29 free(mem); 1.30 } 1.31 1.32 @@ -60,6 +62,8 @@ 1.33 }; 1.34 CxAllocator cxDefaultAllocator = &cx_default_allocator; 1.35 1.36 +/* IMPLEMENTATION OF HIGH LEVEL API */ 1.37 + 1.38 void *cxMalloc(CxAllocator allocator, size_t n) { 1.39 return allocator->cl->malloc(allocator->data, n); 1.40 } 1.41 @@ -69,10 +73,6 @@ 1.42 } 1.43 1.44 int cxReallocate(CxAllocator allocator, void **mem, size_t n) { 1.45 - if (mem == NULL) { 1.46 - errno = EINVAL; 1.47 - return 1; 1.48 - } 1.49 void* nmem = allocator->cl->realloc(allocator->data, *mem, n); 1.50 if (nmem == NULL) { 1.51 return 1;
2.1 --- a/src/cx/allocator.h Sun Sep 26 17:58:27 2021 +0200 2.2 +++ b/src/cx/allocator.h Sun Sep 26 18:01:51 2021 +0200 2.3 @@ -118,6 +118,7 @@ 2.4 * @param n the number of bytes 2.5 * @return a pointer to the allocated memory 2.6 */ 2.7 +__attribute__ ((malloc)) 2.8 void *cxMalloc(CxAllocator allocator, size_t n); 2.9 2.10 /** 2.11 @@ -149,6 +150,7 @@ 2.12 * @param n the new size in bytes 2.13 * @return zero on success, non-zero on failure 2.14 */ 2.15 +__attribute__ ((nonnull)) 2.16 int cxReallocate(CxAllocator allocator, void **mem, size_t n); 2.17 2.18 /** 2.19 @@ -159,6 +161,7 @@ 2.20 * @param n the size of each element in bytes 2.21 * @return a pointer to the allocated memory 2.22 */ 2.23 +__attribute__ ((malloc)) 2.24 void *cxCalloc(CxAllocator allocator, size_t nelem, size_t n); 2.25 2.26 /** 2.27 @@ -169,6 +172,7 @@ 2.28 * @param allocator the allocator 2.29 * @param mem a pointer to the block to free 2.30 */ 2.31 +__attribute__((nonnull)) 2.32 void cxFree(CxAllocator allocator, void *mem); 2.33 2.34 #ifdef __cplusplus
3.1 --- a/test/test_allocator.c Sun Sep 26 17:58:27 2021 +0200 3.2 +++ b/test/test_allocator.c Sun Sep 26 18:01:51 2021 +0200 3.3 @@ -60,12 +60,6 @@ 3.4 free(test); 3.5 } 3.6 3.7 -void test_reallocate_null(void) { 3.8 - int rval = cxReallocate(cxDefaultAllocator, NULL, 16); 3.9 - CU_ASSERT_NOT_EQUAL(rval, 0); 3.10 - CU_ASSERT_EQUAL(errno, EINVAL); 3.11 -} 3.12 - 3.13 void test_default_calloc(void) { 3.14 void *test = cxCalloc(cxDefaultAllocator, 8, 2); 3.15 CU_ASSERT_PTR_NOT_NULL(test) 3.16 @@ -96,7 +90,6 @@ 3.17 !CU_add_test(suite, "test of malloc()", test_default_malloc)|| 3.18 !CU_add_test(suite, "test of realloc()", test_default_realloc) || 3.19 !CU_add_test(suite, "test of realloc() via cxReallocate", test_default_reallocate) || 3.20 - !CU_add_test(suite, "test of cxReallocate with NULL", test_reallocate_null) || 3.21 !CU_add_test(suite, "test of calloc()", test_default_calloc) || 3.22 !CU_add_test(suite, "test of free()", test_default_free) 3.23 ) {