# HG changeset patch # User Mike Becker # Date 1632672111 -7200 # Node ID 38ee262e8b946bad0357bc3be5f061029f57f3de # Parent f1e4c6dabfb4419cbc7801c9c5e11119ff4433bf add attributes to allocator functions diff -r f1e4c6dabfb4 -r 38ee262e8b94 src/allocator.c --- a/src/allocator.c Sun Sep 26 17:58:27 2021 +0200 +++ b/src/allocator.c Sun Sep 26 18:01:51 2021 +0200 @@ -29,21 +29,23 @@ #include "cx/allocator.h" #include -#include -void *cx_malloc_stdlib(void *unused, size_t n) { +__attribute__((malloc)) +void *cx_malloc_stdlib(__attribute__((unused)) void *d, size_t n) { return malloc(n); } -void *cx_realloc_stdlib(void *unused, void *mem, size_t n) { +void *cx_realloc_stdlib(__attribute__((unused)) void *d, void *mem, size_t n) { return realloc(mem, n); } -void *cx_calloc_stdlib(void *unused, size_t nelem, size_t n) { +__attribute__((malloc)) +void *cx_calloc_stdlib(__attribute__((unused)) void *d, size_t nelem, size_t n) { return calloc(nelem, n); } -void cx_free_stdlib(void *unused, void *mem) { +__attribute__((nonnull)) +void cx_free_stdlib(__attribute__((unused)) void *d, void *mem) { free(mem); } @@ -60,6 +62,8 @@ }; CxAllocator cxDefaultAllocator = &cx_default_allocator; +/* IMPLEMENTATION OF HIGH LEVEL API */ + void *cxMalloc(CxAllocator allocator, size_t n) { return allocator->cl->malloc(allocator->data, n); } @@ -69,10 +73,6 @@ } int cxReallocate(CxAllocator allocator, void **mem, size_t n) { - if (mem == NULL) { - errno = EINVAL; - return 1; - } void* nmem = allocator->cl->realloc(allocator->data, *mem, n); if (nmem == NULL) { return 1; diff -r f1e4c6dabfb4 -r 38ee262e8b94 src/cx/allocator.h --- a/src/cx/allocator.h Sun Sep 26 17:58:27 2021 +0200 +++ b/src/cx/allocator.h Sun Sep 26 18:01:51 2021 +0200 @@ -118,6 +118,7 @@ * @param n the number of bytes * @return a pointer to the allocated memory */ +__attribute__ ((malloc)) void *cxMalloc(CxAllocator allocator, size_t n); /** @@ -149,6 +150,7 @@ * @param n the new size in bytes * @return zero on success, non-zero on failure */ +__attribute__ ((nonnull)) int cxReallocate(CxAllocator allocator, void **mem, size_t n); /** @@ -159,6 +161,7 @@ * @param n the size of each element in bytes * @return a pointer to the allocated memory */ +__attribute__ ((malloc)) void *cxCalloc(CxAllocator allocator, size_t nelem, size_t n); /** @@ -169,6 +172,7 @@ * @param allocator the allocator * @param mem a pointer to the block to free */ +__attribute__((nonnull)) void cxFree(CxAllocator allocator, void *mem); #ifdef __cplusplus diff -r f1e4c6dabfb4 -r 38ee262e8b94 test/test_allocator.c --- a/test/test_allocator.c Sun Sep 26 17:58:27 2021 +0200 +++ b/test/test_allocator.c Sun Sep 26 18:01:51 2021 +0200 @@ -60,12 +60,6 @@ free(test); } -void test_reallocate_null(void) { - int rval = cxReallocate(cxDefaultAllocator, NULL, 16); - CU_ASSERT_NOT_EQUAL(rval, 0); - CU_ASSERT_EQUAL(errno, EINVAL); -} - void test_default_calloc(void) { void *test = cxCalloc(cxDefaultAllocator, 8, 2); CU_ASSERT_PTR_NOT_NULL(test) @@ -96,7 +90,6 @@ !CU_add_test(suite, "test of malloc()", test_default_malloc)|| !CU_add_test(suite, "test of realloc()", test_default_realloc) || !CU_add_test(suite, "test of realloc() via cxReallocate", test_default_reallocate) || - !CU_add_test(suite, "test of cxReallocate with NULL", test_reallocate_null) || !CU_add_test(suite, "test of calloc()", test_default_calloc) || !CU_add_test(suite, "test of free()", test_default_free) ) {