ucx/cx/mempool.h

changeset 11
0aa8cbd7912e
parent 0
1a157da63d7c
child 16
04c9f8d8f03b
--- a/ucx/cx/mempool.h	Fri Jan 03 21:40:57 2025 +0100
+++ b/ucx/cx/mempool.h	Sat Jan 04 13:03:01 2025 +0100
@@ -52,7 +52,7 @@
  */
 struct cx_mempool_s {
     /** The provided allocator. */
-    CxAllocator const *allocator;
+    const CxAllocator *allocator;
 
     /**
      * A destructor that shall be automatically registered for newly allocated memory.
@@ -76,15 +76,24 @@
 typedef struct cx_mempool_s CxMempool;
 
 /**
+ * Deallocates a memory pool and frees the managed memory.
+ *
+ * @param pool the memory pool to free
+ */
+void cxMempoolFree(CxMempool *pool);
+
+/**
  * Creates an array-based memory pool with a shared destructor function.
  *
  * This destructor MUST NOT free the memory.
  *
  * @param capacity the initial capacity of the pool
- * @param destr the destructor function to use for allocated memory
+ * @param destr optional destructor function to use for allocated memory
  * @return the created memory pool or \c NULL if allocation failed
  */
-__attribute__((__warn_unused_result__))
+cx_attr_nodiscard
+cx_attr_malloc
+cx_attr_dealloc(cxMempoolFree, 1)
 CxMempool *cxMempoolCreate(size_t capacity, cx_destructor_func destr);
 
 /**
@@ -93,18 +102,7 @@
  * @param capacity the initial capacity of the pool
  * @return the created memory pool or \c NULL if allocation failed
  */
-__attribute__((__warn_unused_result__))
-static inline CxMempool *cxBasicMempoolCreate(size_t capacity) {
-    return cxMempoolCreate(capacity, NULL);
-}
-
-/**
- * Destroys a memory pool and frees the managed memory.
- *
- * @param pool the memory pool to destroy
- */
-__attribute__((__nonnull__))
-void cxMempoolDestroy(CxMempool *pool);
+#define cxBasicMempoolCreate(capacity) cxMempoolCreate(capacity, NULL)
 
 /**
  * Sets the destructor function for a specific allocated memory object.
@@ -115,13 +113,24 @@
  * @param memory the object allocated in the pool
  * @param fnc the destructor function
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 void cxMempoolSetDestructor(
         void *memory,
         cx_destructor_func fnc
 );
 
 /**
+ * Removes the destructor function for a specific allocated memory object.
+ *
+ * If the memory is not managed by a UCX memory pool, the behavior is undefined.
+ * The destructor MUST NOT free the memory.
+ *
+ * @param memory the object allocated in the pool
+ */
+cx_attr_nonnull
+void cxMempoolRemoveDestructor(void *memory);
+
+/**
  * Registers foreign memory with this pool.
  *
  * The destructor, in contrast to memory allocated by the pool, MUST free the memory.
@@ -130,11 +139,11 @@
  * If that allocation fails, this function will return non-zero.
  *
  * @param pool the pool
- * @param memory the object allocated in the pool
+ * @param memory the object to register (MUST NOT be already allocated in the pool)
  * @param destr the destructor function
  * @return zero on success, non-zero on failure
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 int cxMempoolRegister(
         CxMempool *pool,
         void *memory,

mercurial