35 |
44 |
36 #ifdef __cplusplus |
45 #ifdef __cplusplus |
37 extern "C" { |
46 extern "C" { |
38 #endif |
47 #endif |
39 |
48 |
|
49 /** |
|
50 * A function pointer to a destructor function. |
|
51 * @see ucx_mempool_setdestr() |
|
52 * @see ucx_mempool_regdestr() |
|
53 */ |
40 typedef void(*ucx_destructor)(void*); |
54 typedef void(*ucx_destructor)(void*); |
41 |
55 |
|
56 /** |
|
57 * UCX mempool structure. |
|
58 */ |
42 typedef struct { |
59 typedef struct { |
|
60 /** List of pointers to pooled memory. */ |
43 void **data; |
61 void **data; |
|
62 /** Count of pooled memory items. */ |
44 size_t ndata; |
63 size_t ndata; |
|
64 /** Memory pool size. */ |
45 size_t size; |
65 size_t size; |
46 } UcxMempool; |
66 } UcxMempool; |
47 |
67 |
48 |
68 /** Shorthand for a new default memory pool with a capacity of 16 elements. */ |
49 #define ucx_mempool_new_default() ucx_mempool_new(16) |
69 #define ucx_mempool_new_default() ucx_mempool_new(16) |
|
70 |
|
71 |
|
72 /** |
|
73 * Creates a memory pool with the specified initial size. |
|
74 * |
|
75 * As the created memory pool automatically grows in size by 16 elements, when |
|
76 * trying to allocate memory on a full pool, it is recommended that you use |
|
77 * a multiple of 16 for the initial size. |
|
78 * |
|
79 * @param n initial pool size (should be a multiple of 16) |
|
80 * @return a pointer to the new memory pool |
|
81 */ |
50 UcxMempool *ucx_mempool_new(size_t n); |
82 UcxMempool *ucx_mempool_new(size_t n); |
|
83 |
|
84 /** |
|
85 * Resizes a memory pool. |
|
86 * |
|
87 * @param pool the pool to resize |
|
88 * @param newcap the new capacity |
|
89 * @return <code>EXIT_SUCCESS</code> on success or |
|
90 * <code>EXIT_FAILURE</code> on failure |
|
91 */ |
51 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap); |
92 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap); |
52 |
93 |
|
94 /** |
|
95 * Allocates pooled memory. |
|
96 * |
|
97 * @param pool the memory pool |
|
98 * @param n amount of memory to allocate |
|
99 * @return a pointer to the allocated memory |
|
100 * @see ucx_allocator_malloc() |
|
101 */ |
53 void *ucx_mempool_malloc(UcxMempool *pool, size_t n); |
102 void *ucx_mempool_malloc(UcxMempool *pool, size_t n); |
|
103 /** |
|
104 * Allocates a pooled memory array. |
|
105 * |
|
106 * The contents of the allocated memory is set to zero. |
|
107 * |
|
108 * @param pool the memory pool |
|
109 * @param nelem amount of elements to allocate |
|
110 * @param elsize amount of memory per element |
|
111 * @return a pointer to the allocated memory |
|
112 * @see ucx_allocator_calloc() |
|
113 */ |
54 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize); |
114 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize); |
|
115 /** |
|
116 * Reallocates pooled memory. |
|
117 * |
|
118 * @param pool the memory pool |
|
119 * @param ptr a pointer to the memory that shall be reallocated |
|
120 * @param n the new size of the memory |
|
121 * @return a pointer to the the location of the memory |
|
122 * @see ucx_allocator_realloc() |
|
123 */ |
55 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n); |
124 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n); |
|
125 /** |
|
126 * Frees pooled memory. |
|
127 * |
|
128 * Before freeing the memory, the specified destructor function (if any) |
|
129 * is called. |
|
130 * |
|
131 * If you specify memory, that is not pooled by the specified memory pool, this |
|
132 * is considered as a severe error and an error message is written to |
|
133 * <code>stderr</code> and the application is shut down with code |
|
134 * <code>EXIT_FAILURE</code>. |
|
135 * |
|
136 * @param pool the memory pool |
|
137 * @param ptr a pointer to the memory that shall be freed |
|
138 * @see ucx_mempool_set_destr() |
|
139 */ |
56 void ucx_mempool_free(UcxMempool *pool, void *ptr); |
140 void ucx_mempool_free(UcxMempool *pool, void *ptr); |
57 |
141 |
|
142 /** |
|
143 * Destroys a memory pool. |
|
144 * |
|
145 * For each element the destructor function (if any) is called and the element |
|
146 * is freed. |
|
147 * |
|
148 * Each of the registered destructor function that has no corresponding element |
|
149 * within the pool (namely those registered by ucx_mempool_reg_destr) is |
|
150 * called interleaving with the element destruction, but with guarantee to the |
|
151 * order in which they were registered (FIFO order). |
|
152 * |
|
153 * |
|
154 * @param pool the mempool to destroy |
|
155 */ |
58 void ucx_mempool_destroy(UcxMempool *pool); |
156 void ucx_mempool_destroy(UcxMempool *pool); |
59 |
157 |
|
158 /** |
|
159 * Sets a destructor function for the specified memory. |
|
160 * |
|
161 * The destructor is automatically called when the memory is freed or the |
|
162 * pool is destroyed. |
|
163 * |
|
164 * The only requirement for the specified memory is, that it <b>MUST</b> be |
|
165 * pooled memory by an UcxMempool or an element-compatible mempool. The pointer |
|
166 * to the destructor function is saved in a reserved area before the actual |
|
167 * memory. |
|
168 * |
|
169 * @param ptr pooled memory |
|
170 * @param func a pointer to the destructor function |
|
171 * @see ucx_mempool_free() |
|
172 * @see ucx_mempool_destroy() |
|
173 */ |
60 void ucx_mempool_set_destr(void *ptr, ucx_destructor func); |
174 void ucx_mempool_set_destr(void *ptr, ucx_destructor func); |
|
175 |
|
176 /** |
|
177 * Registers a destructor function for the specified (non-pooled) memory. |
|
178 * |
|
179 * This is useful, if you have memory that has not been allocated by a mempool, |
|
180 * but shall be managed by a mempool. |
|
181 * |
|
182 * This function creates an entry in the specified mempool and the memory will |
|
183 * therefore (logically) convert to pooled memory. |
|
184 * |
|
185 * @param pool the memory pool |
|
186 * @param ptr data the destructor is registered for |
|
187 * @param destr a pointer to the destructor function |
|
188 */ |
61 void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr); |
189 void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr); |
62 |
190 |
|
191 /** |
|
192 * Creates an UcxAllocator based on an UcxMempool. |
|
193 * |
|
194 * @param pool the mempool to create the UcxAllocator for |
|
195 * @return a new UcxAllocator based on the specified pool |
|
196 */ |
63 UcxAllocator* ucx_mempool_allocator(UcxMempool *pool); |
197 UcxAllocator* ucx_mempool_allocator(UcxMempool *pool); |
64 |
198 |
65 #ifdef __cplusplus |
199 #ifdef __cplusplus |
66 } |
200 } |
67 #endif |
201 #endif |