1.1 --- a/src/cx/mempool.h Wed Jun 28 19:18:01 2023 +0200 1.2 +++ b/src/cx/mempool.h Wed Jun 28 20:07:52 2023 +0200 1.3 @@ -44,24 +44,31 @@ 1.4 extern "C" { 1.5 #endif 1.6 1.7 -/** 1.8 - * Memory pool class type. 1.9 - */ 1.10 -typedef struct cx_mempool_class_s cx_mempool_class; 1.11 +/** Internal structure for pooled memory. */ 1.12 +struct cx_mempool_memory_s; 1.13 1.14 /** 1.15 * The basic structure of a memory pool. 1.16 * Should be the first member of an actual memory pool implementation. 1.17 */ 1.18 struct cx_mempool_s { 1.19 + /** The provided allocator. */ 1.20 + CxAllocator const *allocator; 1.21 + 1.22 /** 1.23 - * The pool class definition. 1.24 + * A destructor that shall be automatically registered for newly allocated memory. 1.25 + * This destructor MUST NOT free the memory. 1.26 */ 1.27 - cx_mempool_class *cl; 1.28 - /** 1.29 - * The provided allocator. 1.30 - */ 1.31 - CxAllocator const *allocator; 1.32 + cx_destructor_func auto_destr; 1.33 + 1.34 + /** Array of pooled memory. */ 1.35 + struct cx_mempool_memory_s **data; 1.36 + 1.37 + /** Number of pooled memory items. */ 1.38 + size_t size; 1.39 + 1.40 + /** Memory pool capacity. */ 1.41 + size_t capacity; 1.42 }; 1.43 1.44 /** 1.45 @@ -70,22 +77,27 @@ 1.46 typedef struct cx_mempool_s CxMempool; 1.47 1.48 /** 1.49 - * The class definition for a memory pool. 1.50 + * Creates an array-based memory pool with a shared destructor function. 1.51 + * 1.52 + * This destructor MUST NOT free the memory. 1.53 + * 1.54 + * @param capacity the initial capacity of the pool 1.55 + * @param destr the destructor function to use for allocated memory 1.56 + * @return the created memory pool or \c NULL if allocation failed 1.57 */ 1.58 -struct cx_mempool_class_s { 1.59 - /** Member function for destroying the pool. */ 1.60 - __attribute__((__nonnull__)) 1.61 - void (*destroy)(CxMempool *pool); 1.62 +__attribute__((__warn_unused_result__)) 1.63 +CxMempool *cxMempoolCreate(size_t capacity, cx_destructor_func destr); 1.64 1.65 - /** Member function for setting a destructor. */ 1.66 - __attribute__((__nonnull__)) 1.67 - void (*set_destructor)( 1.68 - CxMempool *pool, 1.69 - void *memory, 1.70 - cx_destructor_func fnc 1.71 - ); 1.72 -}; 1.73 - 1.74 +/** 1.75 + * Creates a basic array-based memory pool. 1.76 + * 1.77 + * @param capacity the initial capacity of the pool 1.78 + * @return the created memory pool or \c NULL if allocation failed 1.79 + */ 1.80 +__attribute__((__warn_unused_result__)) 1.81 +static inline CxMempool *cxBasicMempoolCreate(size_t capacity) { 1.82 + return cxMempoolCreate(capacity, NULL); 1.83 +} 1.84 1.85 /** 1.86 * Destroys a memory pool including their contents. 1.87 @@ -93,27 +105,42 @@ 1.88 * @param pool the memory pool to destroy 1.89 */ 1.90 __attribute__((__nonnull__)) 1.91 -static inline void cxMempoolDestroy(CxMempool *pool) { 1.92 - pool->cl->destroy(pool); 1.93 -} 1.94 +void cxMempoolDestroy(CxMempool *pool); 1.95 1.96 /** 1.97 - * Sets a destructor function for an allocated memory object. 1.98 + * Sets the destructor function for a specific allocated memory object. 1.99 * 1.100 - * If the memory is not managed by the pool, the behavior is undefined. 1.101 + * If the memory is not managed by a UCX memory pool, the behavior is undefined. 1.102 + * The destructor MUST NOT free the memory. 1.103 * 1.104 - * @param pool the pool 1.105 - * @param memory the objected allocated in the pool 1.106 + * @param memory the object allocated in the pool 1.107 * @param fnc the destructor function 1.108 */ 1.109 __attribute__((__nonnull__)) 1.110 -static inline void cxMempoolSetDestructor( 1.111 +void cxMempoolSetDestructor( 1.112 + void *memory, 1.113 + cx_destructor_func fnc 1.114 +); 1.115 + 1.116 +/** 1.117 + * Registers foreign memory with this pool. 1.118 + * 1.119 + * The destructor, in contrast to memory allocated by the pool, MUST free the memory. 1.120 + * 1.121 + * A small portion of memory will be allocated to register the information in the pool. 1.122 + * If that allocation fails, this function will return non-zero. 1.123 + * 1.124 + * @param pool the pool 1.125 + * @param memory the object allocated in the pool 1.126 + * @param destr the destructor function 1.127 + * @return zero on success, non-zero on failure 1.128 + */ 1.129 +__attribute__((__nonnull__)) 1.130 +int cxMempoolRegister( 1.131 CxMempool *pool, 1.132 void *memory, 1.133 - cx_destructor_func fnc 1.134 -) { 1.135 - pool->cl->set_destructor(pool, memory, fnc); 1.136 -} 1.137 + cx_destructor_func destr 1.138 +); 1.139 1.140 #ifdef __cplusplus 1.141 } // extern "C"