improves usage of attributes

Sun, 03 Oct 2021 12:02:57 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 03 Oct 2021 12:02:57 +0200
changeset 450
7960298039cf
parent 449
68ad5750ba6b
child 451
db06dda7ac4d

improves usage of attributes

src/allocator.c file | annotate | diff | comparison | revisions
src/cx/allocator.h file | annotate | diff | comparison | revisions
     1.1 --- a/src/allocator.c	Sun Oct 03 10:43:31 2021 +0200
     1.2 +++ b/src/allocator.c	Sun Oct 03 12:02:57 2021 +0200
     1.3 @@ -30,26 +30,27 @@
     1.4  
     1.5  #include <stdlib.h>
     1.6  
     1.7 -__attribute__((malloc))
     1.8 -void *cx_malloc_stdlib(__attribute__((unused)) void *d, size_t n) {
     1.9 +__attribute__((__malloc__))
    1.10 +static void *cx_malloc_stdlib(__attribute__((__unused__)) void *d, size_t n) {
    1.11      return malloc(n);
    1.12  }
    1.13  
    1.14 -void *cx_realloc_stdlib(__attribute__((unused)) void *d, void *mem, size_t n) {
    1.15 +__attribute__((__warn_unused_result__))
    1.16 +static 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 -__attribute__((malloc))
    1.21 -void *cx_calloc_stdlib(__attribute__((unused)) void *d, size_t nelem, size_t n) {
    1.22 +__attribute__((__malloc__))
    1.23 +static void *cx_calloc_stdlib(__attribute__((__unused__)) void *d, size_t nelem, size_t n) {
    1.24      return calloc(nelem, n);
    1.25  }
    1.26  
    1.27 -__attribute__((nonnull))
    1.28 -void cx_free_stdlib(__attribute__((unused)) void *d, void *mem) {
    1.29 +__attribute__((__nonnull__))
    1.30 +static void cx_free_stdlib(__attribute__((__unused__)) void *d, void *mem) {
    1.31      free(mem);
    1.32  }
    1.33  
    1.34 -cx_allocator_class cx_default_allocator_class = {
    1.35 +static cx_allocator_class cx_default_allocator_class = {
    1.36          cx_malloc_stdlib,
    1.37          cx_realloc_stdlib,
    1.38          cx_calloc_stdlib,
    1.39 @@ -73,7 +74,7 @@
    1.40  }
    1.41  
    1.42  int cxReallocate(CxAllocator allocator, void **mem, size_t n) {
    1.43 -    void* nmem = allocator->cl->realloc(allocator->data, *mem, n);
    1.44 +    void *nmem = allocator->cl->realloc(allocator->data, *mem, n);
    1.45      if (nmem == NULL) {
    1.46          return 1;
    1.47      } else {
     2.1 --- a/src/cx/allocator.h	Sun Oct 03 10:43:31 2021 +0200
     2.2 +++ b/src/cx/allocator.h	Sun Oct 03 12:02:57 2021 +0200
     2.3 @@ -64,7 +64,8 @@
     2.4       * @param n the new size in bytes
     2.5       * @return a pointer to the re-allocated memory
     2.6       */
     2.7 -    void *(*realloc)(void *data, void *mem, size_t n);
     2.8 +    void *(*realloc)(void *data, void *mem, size_t n)
     2.9 +    __attribute__((__warn_unused_result__));
    2.10  
    2.11      /**
    2.12       * Allocate \p nelem elements of \p n bytes each, all initialized to zero.
    2.13 @@ -84,7 +85,8 @@
    2.14       * @param data the allocator's data
    2.15       * @param mem a pointer to the block to free
    2.16       */
    2.17 -    void (*free)(void *data, void *mem);
    2.18 +    void (*free)(void *data, void *mem)
    2.19 +    __attribute__((__nonnull__));
    2.20  } cx_allocator_class;
    2.21  
    2.22  /**
    2.23 @@ -118,8 +120,8 @@
    2.24   * @param n the number of bytes
    2.25   * @return a pointer to the allocated memory
    2.26   */
    2.27 -__attribute__ ((malloc))
    2.28 -void *cxMalloc(CxAllocator allocator, size_t n);
    2.29 +void *cxMalloc(CxAllocator allocator, size_t n)
    2.30 +__attribute__((__malloc__));
    2.31  
    2.32  /**
    2.33   * Re-allocate the previously allocated block in \p mem, making the new block \p n bytes long.
    2.34 @@ -133,7 +135,8 @@
    2.35   * @param n the new size in bytes
    2.36   * @return a pointer to the re-allocated memory
    2.37   */
    2.38 -void *cxRealloc(CxAllocator allocator, void *mem, size_t n);
    2.39 +void *cxRealloc(CxAllocator allocator, void *mem, size_t n)
    2.40 +__attribute__((warn_unused_result));
    2.41  
    2.42  /**
    2.43   * Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
    2.44 @@ -150,8 +153,8 @@
    2.45   * @param n the new size in bytes
    2.46   * @return zero on success, non-zero on failure
    2.47   */
    2.48 -__attribute__ ((nonnull))
    2.49 -int cxReallocate(CxAllocator allocator, void **mem, size_t n);
    2.50 +int cxReallocate(CxAllocator allocator, void **mem, size_t n)
    2.51 +__attribute__((nonnull));
    2.52  
    2.53  /**
    2.54   * Allocate \p nelem elements of \p n bytes each, all initialized to zero.
    2.55 @@ -161,8 +164,8 @@
    2.56   * @param n the size of each element in bytes
    2.57   * @return a pointer to the allocated memory
    2.58   */
    2.59 -__attribute__ ((malloc))
    2.60 -void *cxCalloc(CxAllocator allocator, size_t nelem, size_t n);
    2.61 +void *cxCalloc(CxAllocator allocator, size_t nelem, size_t n)
    2.62 +__attribute__((malloc));
    2.63  
    2.64  /**
    2.65   * Free a block allocated by this allocator.
    2.66 @@ -172,8 +175,8 @@
    2.67   * @param allocator the allocator
    2.68   * @param mem a pointer to the block to free
    2.69   */
    2.70 -__attribute__((nonnull))
    2.71 -void cxFree(CxAllocator allocator, void *mem);
    2.72 +void cxFree(CxAllocator allocator, void *mem)
    2.73 +__attribute__((nonnull));
    2.74  
    2.75  #ifdef __cplusplus
    2.76  } /* extern "C" */

mercurial