src/ucx/cx/mempool.h

changeset 504
c094afcdfb27
parent 490
d218607f5a7e
equal deleted inserted replaced
503:aeaf7db26fac 504:c094afcdfb27
42 42
43 #ifdef __cplusplus 43 #ifdef __cplusplus
44 extern "C" { 44 extern "C" {
45 #endif 45 #endif
46 46
47 /** 47 /** Internal structure for pooled memory. */
48 * Memory pool class type. 48 struct cx_mempool_memory_s;
49 */
50 typedef struct cx_mempool_class_s cx_mempool_class;
51 49
52 /** 50 /**
53 * The basic structure of a memory pool. 51 * The basic structure of a memory pool.
54 * Should be the first member of an actual memory pool implementation. 52 * Should be the first member of an actual memory pool implementation.
55 */ 53 */
56 struct cx_mempool_s { 54 struct cx_mempool_s {
55 /** The provided allocator. */
56 CxAllocator const *allocator;
57
57 /** 58 /**
58 * The pool class definition. 59 * A destructor that shall be automatically registered for newly allocated memory.
60 * This destructor MUST NOT free the memory.
59 */ 61 */
60 cx_mempool_class *cl; 62 cx_destructor_func auto_destr;
61 /** 63
62 * The provided allocator. 64 /** Array of pooled memory. */
63 */ 65 struct cx_mempool_memory_s **data;
64 CxAllocator const *allocator; 66
67 /** Number of pooled memory items. */
68 size_t size;
69
70 /** Memory pool capacity. */
71 size_t capacity;
65 }; 72 };
66 73
67 /** 74 /**
68 * Common type for all memory pool implementations. 75 * Common type for all memory pool implementations.
69 */ 76 */
70 typedef struct cx_mempool_s CxMempool; 77 typedef struct cx_mempool_s CxMempool;
71 78
72 /** 79 /**
73 * The class definition for a memory pool. 80 * Creates an array-based memory pool with a shared destructor function.
81 *
82 * This destructor MUST NOT free the memory.
83 *
84 * @param capacity the initial capacity of the pool
85 * @param destr the destructor function to use for allocated memory
86 * @return the created memory pool or \c NULL if allocation failed
74 */ 87 */
75 struct cx_mempool_class_s { 88 __attribute__((__warn_unused_result__))
76 /** Member function for destroying the pool. */ 89 CxMempool *cxMempoolCreate(size_t capacity, cx_destructor_func destr);
77 __attribute__((__nonnull__))
78 void (*destroy)(CxMempool *pool);
79
80 /** Member function for setting a destructor. */
81 __attribute__((__nonnull__))
82 void (*set_destructor)(
83 CxMempool *pool,
84 void *memory,
85 cx_destructor_func fnc
86 );
87 };
88
89 90
90 /** 91 /**
91 * Destroys a memory pool including their contents. 92 * Creates a basic array-based memory pool.
93 *
94 * @param capacity the initial capacity of the pool
95 * @return the created memory pool or \c NULL if allocation failed
96 */
97 __attribute__((__warn_unused_result__))
98 static inline CxMempool *cxBasicMempoolCreate(size_t capacity) {
99 return cxMempoolCreate(capacity, NULL);
100 }
101
102 /**
103 * Destroys a memory pool and frees the managed memory.
92 * 104 *
93 * @param pool the memory pool to destroy 105 * @param pool the memory pool to destroy
94 */ 106 */
95 __attribute__((__nonnull__)) 107 __attribute__((__nonnull__))
96 static inline void cxMempoolDestroy(CxMempool *pool) { 108 void cxMempoolDestroy(CxMempool *pool);
97 pool->cl->destroy(pool);
98 }
99 109
100 /** 110 /**
101 * Sets a destructor function for an allocated memory object. 111 * Sets the destructor function for a specific allocated memory object.
102 * 112 *
103 * If the memory is not managed by the pool, the behavior is undefined. 113 * If the memory is not managed by a UCX memory pool, the behavior is undefined.
114 * The destructor MUST NOT free the memory.
104 * 115 *
105 * @param pool the pool 116 * @param memory the object allocated in the pool
106 * @param memory the objected allocated in the pool
107 * @param fnc the destructor function 117 * @param fnc the destructor function
108 */ 118 */
109 __attribute__((__nonnull__)) 119 __attribute__((__nonnull__))
110 static inline void cxMempoolSetDestructor( 120 void cxMempoolSetDestructor(
121 void *memory,
122 cx_destructor_func fnc
123 );
124
125 /**
126 * Registers foreign memory with this pool.
127 *
128 * The destructor, in contrast to memory allocated by the pool, MUST free the memory.
129 *
130 * A small portion of memory will be allocated to register the information in the pool.
131 * If that allocation fails, this function will return non-zero.
132 *
133 * @param pool the pool
134 * @param memory the object allocated in the pool
135 * @param destr the destructor function
136 * @return zero on success, non-zero on failure
137 */
138 __attribute__((__nonnull__))
139 int cxMempoolRegister(
111 CxMempool *pool, 140 CxMempool *pool,
112 void *memory, 141 void *memory,
113 cx_destructor_func fnc 142 cx_destructor_func destr
114 ) { 143 );
115 pool->cl->set_destructor(pool, memory, fnc);
116 }
117 144
118 #ifdef __cplusplus 145 #ifdef __cplusplus
119 } // extern "C" 146 } // extern "C"
120 #endif 147 #endif
121 148

mercurial