src/ucx/cx/allocator.h

changeset 582
82b60a8dd55c
parent 579
e10457d74fe1
child 621
956c03c25edd
--- a/src/ucx/cx/allocator.h	Mon May 26 21:02:30 2025 +0200
+++ b/src/ucx/cx/allocator.h	Mon May 26 21:06:17 2025 +0200
@@ -98,10 +98,17 @@
 typedef struct cx_allocator_s CxAllocator;
 
 /**
- * A default allocator using standard library malloc() etc.
+ * A pre-defined allocator using standard library malloc() etc.
  */
 cx_attr_export
-extern const CxAllocator * const cxDefaultAllocator;
+extern const CxAllocator * const cxStdlibAllocator;
+
+/**
+ * The default allocator that is used by UCX.
+ * Initialized with cxStdlibAllocator, but you may change it.
+ */
+cx_attr_export
+extern const CxAllocator * cxDefaultAllocator;
 
 /**
  * Function pointer type for destructor functions.
@@ -135,6 +142,8 @@
  * Reallocate a previously allocated block and changes the pointer in-place,
  * if necessary.
  *
+ * @note This will use stdlib reallocate and @em not the cxDefaultAllocator.
+ *
  * @par Error handling
  * @c errno will be set by realloc() on failure.
  *
@@ -158,6 +167,8 @@
  *
  * The size is calculated by multiplying @p nemb and @p size.
  *
+ * @note This will use stdlib reallocate and @em not the cxDefaultAllocator.
+ *
  * @par Error handling
  * @c errno will be set by realloc() on failure or when the multiplication of
  * @p nmemb and @p size overflows.
@@ -182,6 +193,8 @@
  * Reallocate a previously allocated block and changes the pointer in-place,
  * if necessary.
  *
+ * @note This will use stdlib reallocate and @em not the cxDefaultAllocator.
+ *
  * @par Error handling
  * @c errno will be set by realloc() on failure.
  *
@@ -199,6 +212,8 @@
  *
  * The size is calculated by multiplying @p nemb and @p size.
  *
+ * @note This will use stdlib reallocate and @em not the cxDefaultAllocator.
+ *
  * @par Error handling
  * @c errno will be set by realloc() on failure or when the multiplication of
  * @p nmemb and @p size overflows.
@@ -213,6 +228,14 @@
     cx_reallocatearray_((void**)(mem), nmemb, size)
 
 /**
+ * Allocates memory and sets every byte to zero.
+ *
+ * @param n (@c size_t) the number of bytes
+ * @return (@c void*) a pointer to the allocated memory
+ */
+#define cx_zalloc(n) calloc(1, n)
+
+/**
  * Free a block allocated by this allocator.
  *
  * @note Freeing a block of a different allocator is undefined.
@@ -414,6 +437,57 @@
         size_t size
 );
 
+/**
+ * Allocate @p n bytes of memory and sets every byte to zero.
+ *
+ * @param allocator the allocator
+ * @param n the number of bytes
+ * @return a pointer to the allocated memory
+ */
+cx_attr_nodiscard
+cx_attr_nonnull
+cx_attr_malloc
+cx_attr_dealloc_ucx
+cx_attr_allocsize(2)
+cx_attr_export
+void *cxZalloc(
+        const CxAllocator *allocator,
+        size_t n
+);
+
+/**
+ * Convenience macro that invokes cxMalloc() with the cxDefaultAllocator.
+ */
+#define cxMallocDefault(...) cxMalloc(cxDefaultAllocator, __VA_ARGS__)
+/**
+ * Convenience macro that invokes cxZalloc() with the cxDefaultAllocator.
+ */
+#define cxZallocDefault(...) cxZalloc(cxDefaultAllocator, __VA_ARGS__)
+/**
+ * Convenience macro that invokes cxCalloc() with the cxDefaultAllocator.
+ */
+#define cxCallocDefault(...) cxCalloc(cxDefaultAllocator, __VA_ARGS__)
+/**
+ * Convenience macro that invokes cxRealloc() with the cxDefaultAllocator.
+ */
+#define cxReallocDefault(...) cxRealloc(cxDefaultAllocator, __VA_ARGS__)
+/**
+ * Convenience macro that invokes cxReallocate() with the cxDefaultAllocator.
+ */
+#define cxReallocateDefault(...) cxReallocate(cxDefaultAllocator, __VA_ARGS__)
+/**
+ * Convenience macro that invokes cxReallocateArray() with the cxDefaultAllocator.
+ */
+#define cxReallocateArrayDefault(...) cxReallocateArray(cxDefaultAllocator, __VA_ARGS__)
+/**
+ * Convenience macro that invokes cxReallocArray() with the cxDefaultAllocator.
+ */
+#define cxReallocArrayDefault(...) cxReallocArray(cxDefaultAllocator, __VA_ARGS__)
+/**
+ * Convenience macro that invokes cxFree() with the cxDefaultAllocator.
+ */
+#define cxFreeDefault(...) cxFree(cxDefaultAllocator, __VA_ARGS__)
+
 #ifdef __cplusplus
 } // extern "C"
 #endif

mercurial