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
--- a/src/allocator.c	Tue Jun 27 20:04:48 2023 +0200
+++ b/src/allocator.c	Wed Jun 28 19:18:01 2023 +0200
@@ -75,6 +75,20 @@
 };
 CxAllocator *cxDefaultAllocator = &cx_default_allocator;
 
+
+int cx_reallocate(
+        void **mem,
+        size_t n
+) {
+    void *nmem = realloc(*mem, n);
+    if (nmem == NULL) {
+        return 1;
+    } else {
+        *mem = nmem;
+        return 0;
+    }
+}
+
 // IMPLEMENTATION OF HIGH LEVEL API
 
 void *cxMalloc(
--- a/src/cx/allocator.h	Tue Jun 27 20:04:48 2023 +0200
+++ b/src/cx/allocator.h	Wed Jun 28 19:18:01 2023 +0200
@@ -133,6 +133,22 @@
 ) __attribute__((__nonnull__(2)));
 
 /**
+ * Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
+ *
+ * \par Error handling
+ * \c errno will be set by realloc() on failure.
+ *
+ * @param mem pointer to the pointer to allocated block
+ * @param n the new size in bytes
+ * @return zero on success, non-zero on failure
+ */
+int cx_reallocate(
+        void **mem,
+        size_t n
+)
+__attribute__((__nonnull__));
+
+/**
  * Allocate \p n bytes of memory.
  *
  * @param allocator the allocator
@@ -169,7 +185,6 @@
 /**
  * Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
  * This function acts like cxRealloc() using the pointer pointed to by \p mem.
- * On success, the pointer is changed to the new location (in case the
  *
  * \note Re-allocating a block allocated by a different allocator is undefined.
  *

mercurial