src/cx/mempool.h

changeset 1328
2cf66dee40b8
parent 1325
20caf6efaf07
child 1329
343eac5ac824
equal deleted inserted replaced
1327:ed75dc1db503 1328:2cf66dee40b8
41 41
42 #ifdef __cplusplus 42 #ifdef __cplusplus
43 extern "C" { 43 extern "C" {
44 #endif 44 #endif
45 45
46 /** A memory block in a simple memory pool. */
46 struct cx_mempool_memory_s { 47 struct cx_mempool_memory_s {
47 /** The destructor. */ 48 /** The destructor. */
48 cx_destructor_func destructor; 49 cx_destructor_func destructor;
49 /** The actual memory. */ 50 /** The actual memory. */
50 char c[]; 51 char c[];
51 }; 52 };
52 53
54 /** A memory block in an advanced memory pool. */
53 struct cx_mempool_memory2_s { 55 struct cx_mempool_memory2_s {
54 /** The destructor. */ 56 /** The destructor. */
55 cx_destructor_func2 destructor; 57 cx_destructor_func2 destructor;
56 /** Data for the destructor. */ 58 /** Data for the destructor. */
57 void *data; 59 void *data;
58 /** The actual memory. */ 60 /** The actual memory. */
59 char c[]; 61 char c[];
60 }; 62 };
61 63
64 /** Represents memory that is not allocated by, but registered with a pool. */
62 struct cx_mempool_foreign_memory_s { 65 struct cx_mempool_foreign_memory_s {
63 /** The foreign memory. */ 66 /** The foreign memory. */
64 void* mem; 67 void* mem;
65 union { 68 union {
66 /** Simple destructor. */ 69 /** Simple destructor. */
168 cx_attr_dealloc(cxMempoolFree, 1) 171 cx_attr_dealloc(cxMempoolFree, 1)
169 cx_attr_export 172 cx_attr_export
170 CxMempool *cxMempoolCreate(size_t capacity, enum cx_mempool_type type); 173 CxMempool *cxMempoolCreate(size_t capacity, enum cx_mempool_type type);
171 174
172 /** 175 /**
176 * Creates a basic array-based memory pool.
177 *
178 * Convenience macro to create a memory pool of type #CX_MEMPOOL_TYPE_SIMPLE.
179 *
180 * @param capacity (@c size_t) the initial capacity of the pool
181 * @return (@c CxMempool*) the created memory pool or @c NULL if allocation failed
182 */
183 #define cxMempoolCreateSimple(capacity) cxMempoolCreate(capacity, CX_MEMPOOL_TYPE_SIMPLE)
184
185 /**
186 * Creates a basic array-based memory pool.
187 *
188 * Convenience macro to create a memory pool of type #CX_MEMPOOL_TYPE_ADVANCED.
189 *
190 * @param capacity (@c size_t) the initial capacity of the pool
191 * @return (@c CxMempool*) the created memory pool or @c NULL if allocation failed
192 */
193 #define cxMempoolCreateAdvanced(capacity) cxMempoolCreate(capacity, CX_MEMPOOL_TYPE_ADVANCED)
194
195 /**
196 * Creates a basic array-based memory pool.
197 *
198 * Convenience macro to create a memory pool of type #CX_MEMPOOL_TYPE_PURE.
199 *
200 * @param capacity (@c size_t) the initial capacity of the pool
201 * @return (@c CxMempool*) the created memory pool or @c NULL if allocation failed
202 */
203 #define cxMempoolCreatePure(capacity) cxMempoolCreate(capacity, CX_MEMPOOL_TYPE_PURE)
204
205 /**
173 * Sets the global destructor for all memory blocks within the specified pool. 206 * Sets the global destructor for all memory blocks within the specified pool.
174 * 207 *
175 * @param pool the memory pool 208 * @param pool the memory pool
176 * @param fnc the destructor that shall be applied to all memory blocks 209 * @param fnc the destructor that shall be applied to all memory blocks
177 */ 210 */
187 * @param data additional data for the destructor function 220 * @param data additional data for the destructor function
188 */ 221 */
189 cx_attr_nonnull_arg(1) 222 cx_attr_nonnull_arg(1)
190 cx_attr_export 223 cx_attr_export
191 void cxMempoolGlobalDestructor2(CxMempool *pool, cx_destructor_func2 fnc, void *data); 224 void cxMempoolGlobalDestructor2(CxMempool *pool, cx_destructor_func2 fnc, void *data);
192
193 /**
194 * Creates a basic array-based memory pool.
195 *
196 * @param capacity (@c size_t) the initial capacity of the pool
197 * @return (@c CxMempool*) the created memory pool or @c NULL if allocation failed
198 */
199 #define cxMempoolCreateSimple(capacity) cxMempoolCreate(capacity, CX_MEMPOOL_TYPE_SIMPLE)
200 225
201 /** 226 /**
202 * Sets the destructor function for a specific allocated memory object. 227 * Sets the destructor function for a specific allocated memory object.
203 * 228 *
204 * If the type of memory pool is not #CX_MEMPOOL_TYPE_SIMPLE, the behavior is undefined. 229 * If the type of memory pool is not #CX_MEMPOOL_TYPE_SIMPLE, the behavior is undefined.

mercurial