add a low-level stdlib-based cx_reallocate()

Wed, 28 Jun 2023 19:18:01 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 28 Jun 2023 19:18:01 +0200
changeset 726
44986c0e2b05
parent 725
c9b882bef838
child 727
d92a59f5d261

add a low-level stdlib-based cx_reallocate()

src/allocator.c file | annotate | diff | comparison | revisions
src/cx/allocator.h file | annotate | diff | comparison | revisions
     1.1 --- a/src/allocator.c	Tue Jun 27 20:04:48 2023 +0200
     1.2 +++ b/src/allocator.c	Wed Jun 28 19:18:01 2023 +0200
     1.3 @@ -75,6 +75,20 @@
     1.4  };
     1.5  CxAllocator *cxDefaultAllocator = &cx_default_allocator;
     1.6  
     1.7 +
     1.8 +int cx_reallocate(
     1.9 +        void **mem,
    1.10 +        size_t n
    1.11 +) {
    1.12 +    void *nmem = realloc(*mem, n);
    1.13 +    if (nmem == NULL) {
    1.14 +        return 1;
    1.15 +    } else {
    1.16 +        *mem = nmem;
    1.17 +        return 0;
    1.18 +    }
    1.19 +}
    1.20 +
    1.21  // IMPLEMENTATION OF HIGH LEVEL API
    1.22  
    1.23  void *cxMalloc(
     2.1 --- a/src/cx/allocator.h	Tue Jun 27 20:04:48 2023 +0200
     2.2 +++ b/src/cx/allocator.h	Wed Jun 28 19:18:01 2023 +0200
     2.3 @@ -133,6 +133,22 @@
     2.4  ) __attribute__((__nonnull__(2)));
     2.5  
     2.6  /**
     2.7 + * Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
     2.8 + *
     2.9 + * \par Error handling
    2.10 + * \c errno will be set by realloc() on failure.
    2.11 + *
    2.12 + * @param mem pointer to the pointer to allocated block
    2.13 + * @param n the new size in bytes
    2.14 + * @return zero on success, non-zero on failure
    2.15 + */
    2.16 +int cx_reallocate(
    2.17 +        void **mem,
    2.18 +        size_t n
    2.19 +)
    2.20 +__attribute__((__nonnull__));
    2.21 +
    2.22 +/**
    2.23   * Allocate \p n bytes of memory.
    2.24   *
    2.25   * @param allocator the allocator
    2.26 @@ -169,7 +185,6 @@
    2.27  /**
    2.28   * Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
    2.29   * This function acts like cxRealloc() using the pointer pointed to by \p mem.
    2.30 - * On success, the pointer is changed to the new location (in case the
    2.31   *
    2.32   * \note Re-allocating a block allocated by a different allocator is undefined.
    2.33   *

mercurial