diff -r aeaf7db26fac -r c094afcdfb27 src/ucx/cx/mempool.h --- a/src/ucx/cx/mempool.h Sun Jul 09 15:14:26 2023 +0200 +++ b/src/ucx/cx/mempool.h Mon Jul 10 18:39:24 2023 +0200 @@ -44,24 +44,31 @@ extern "C" { #endif -/** - * Memory pool class type. - */ -typedef struct cx_mempool_class_s cx_mempool_class; +/** Internal structure for pooled memory. */ +struct cx_mempool_memory_s; /** * The basic structure of a memory pool. * Should be the first member of an actual memory pool implementation. */ struct cx_mempool_s { + /** The provided allocator. */ + CxAllocator const *allocator; + /** - * The pool class definition. + * A destructor that shall be automatically registered for newly allocated memory. + * This destructor MUST NOT free the memory. */ - cx_mempool_class *cl; - /** - * The provided allocator. - */ - CxAllocator const *allocator; + cx_destructor_func auto_destr; + + /** Array of pooled memory. */ + struct cx_mempool_memory_s **data; + + /** Number of pooled memory items. */ + size_t size; + + /** Memory pool capacity. */ + size_t capacity; }; /** @@ -70,50 +77,70 @@ typedef struct cx_mempool_s CxMempool; /** - * The class definition for a memory 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 + * @return the created memory pool or \c NULL if allocation failed */ -struct cx_mempool_class_s { - /** Member function for destroying the pool. */ - __attribute__((__nonnull__)) - void (*destroy)(CxMempool *pool); - - /** Member function for setting a destructor. */ - __attribute__((__nonnull__)) - void (*set_destructor)( - CxMempool *pool, - void *memory, - cx_destructor_func fnc - ); -}; - +__attribute__((__warn_unused_result__)) +CxMempool *cxMempoolCreate(size_t capacity, cx_destructor_func destr); /** - * Destroys a memory pool including their contents. + * Creates a basic array-based memory pool. + * + * @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__)) -static inline void cxMempoolDestroy(CxMempool *pool) { - pool->cl->destroy(pool); -} +void cxMempoolDestroy(CxMempool *pool); /** - * Sets a destructor function for an allocated memory object. + * Sets the destructor function for a specific allocated memory object. * - * If the memory is not managed by the pool, the behavior is undefined. + * If the memory is not managed by a UCX memory pool, the behavior is undefined. + * The destructor MUST NOT free the memory. * - * @param pool the pool - * @param memory the objected allocated in the pool + * @param memory the object allocated in the pool * @param fnc the destructor function */ __attribute__((__nonnull__)) -static inline void cxMempoolSetDestructor( - CxMempool *pool, +void cxMempoolSetDestructor( void *memory, cx_destructor_func fnc -) { - pool->cl->set_destructor(pool, memory, fnc); -} +); + +/** + * Registers foreign memory with this pool. + * + * The destructor, in contrast to memory allocated by the pool, MUST free the memory. + * + * A small portion of memory will be allocated to register the information in the pool. + * If that allocation fails, this function will return non-zero. + * + * @param pool the pool + * @param memory the object allocated in the pool + * @param destr the destructor function + * @return zero on success, non-zero on failure + */ +__attribute__((__nonnull__)) +int cxMempoolRegister( + CxMempool *pool, + void *memory, + cx_destructor_func destr +); #ifdef __cplusplus } // extern "C"