ucx/cx/allocator.h

changeset 101
7b3a3130be44
parent 49
2f71f4ee247a
equal deleted inserted replaced
100:d2bd73d28ff1 101:7b3a3130be44
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 /** 28 /**
29 * \file allocator.h 29 * @file allocator.h
30 * Interface for custom allocators. 30 * Interface for custom allocators.
31 */ 31 */
32 32
33 #ifndef UCX_ALLOCATOR_H 33 #ifndef UCX_ALLOCATOR_H
34 #define UCX_ALLOCATOR_H 34 #define UCX_ALLOCATOR_H
52 ); 52 );
53 53
54 /** 54 /**
55 * The allocator's realloc() implementation. 55 * The allocator's realloc() implementation.
56 */ 56 */
57 __attribute__((__warn_unused_result__))
58 void *(*realloc)( 57 void *(*realloc)(
59 void *data, 58 void *data,
60 void *mem, 59 void *mem,
61 size_t n 60 size_t n
62 ); 61 );
71 ); 70 );
72 71
73 /** 72 /**
74 * The allocator's free() implementation. 73 * The allocator's free() implementation.
75 */ 74 */
76 __attribute__((__nonnull__))
77 void (*free)( 75 void (*free)(
78 void *data, 76 void *data,
79 void *mem 77 void *mem
80 ); 78 );
81 } cx_allocator_class; 79 } cx_allocator_class;
106 104
107 /** 105 /**
108 * Function pointer type for destructor functions. 106 * Function pointer type for destructor functions.
109 * 107 *
110 * A destructor function deallocates possible contents and MAY free the memory 108 * A destructor function deallocates possible contents and MAY free the memory
111 * pointed to by \p memory. Read the documentation of the respective function 109 * pointed to by @p memory. Read the documentation of the respective function
112 * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in that 110 * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in
113 * particular implementation. 111 * that particular implementation.
114 * 112 *
115 * @param memory a pointer to the object to destruct 113 * @param memory a pointer to the object to destruct
116 */ 114 */
117 __attribute__((__nonnull__))
118 typedef void (*cx_destructor_func)(void *memory); 115 typedef void (*cx_destructor_func)(void *memory);
119 116
120 /** 117 /**
121 * Function pointer type for destructor functions. 118 * Function pointer type for destructor functions.
122 * 119 *
123 * A destructor function deallocates possible contents and MAY free the memory 120 * A destructor function deallocates possible contents and MAY free the memory
124 * pointed to by \p memory. Read the documentation of the respective function 121 * pointed to by @p memory. Read the documentation of the respective function
125 * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in that 122 * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in
126 * particular implementation. 123 * that particular implementation.
127 * 124 *
128 * @param data an optional pointer to custom data 125 * @param data an optional pointer to custom data
129 * @param memory a pointer to the object to destruct 126 * @param memory a pointer to the object to destruct
130 */ 127 */
131 __attribute__((__nonnull__(2)))
132 typedef void (*cx_destructor_func2)( 128 typedef void (*cx_destructor_func2)(
133 void *data, 129 void *data,
134 void *memory 130 void *memory
135 ); 131 );
136 132
137 /** 133 /**
138 * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. 134 * Re-allocate a previously allocated block and changes the pointer in-place,
139 * 135 * if necessary.
140 * \par Error handling 136 *
141 * \c errno will be set by realloc() on failure. 137 * @par Error handling
138 * @c errno will be set by realloc() on failure.
142 * 139 *
143 * @param mem pointer to the pointer to allocated block 140 * @param mem pointer to the pointer to allocated block
144 * @param n the new size in bytes 141 * @param n the new size in bytes
145 * @return zero on success, non-zero on failure 142 * @retval zero success
146 */ 143 * @retval non-zero failure
147 __attribute__((__nonnull__)) 144 * @see cx_reallocatearray()
145 */
146 cx_attr_nonnull
147 cx_attr_nodiscard
148 int cx_reallocate( 148 int cx_reallocate(
149 void **mem, 149 void **mem,
150 size_t n 150 size_t n
151 ); 151 );
152 152
153 /** 153 /**
154 * Allocate \p n bytes of memory. 154 * Re-allocate a previously allocated block and changes the pointer in-place,
155 * if necessary.
156 *
157 * The size is calculated by multiplying @p nemb and @p size.
158 *
159 * @par Error handling
160 * @c errno will be set by realloc() on failure or when the multiplication of
161 * @p nmemb and @p size overflows.
162 *
163 * @param mem pointer to the pointer to allocated block
164 * @param nmemb the number of elements
165 * @param size the size of each element
166 * @retval zero success
167 * @retval non-zero failure
168 * @see cx_reallocate()
169 */
170 cx_attr_nonnull
171 cx_attr_nodiscard
172 int cx_reallocatearray(
173 void **mem,
174 size_t nmemb,
175 size_t size
176 );
177
178 /**
179 * Re-allocate a previously allocated block and changes the pointer in-place,
180 * if necessary.
181 *
182 * @par Error handling
183 * @c errno will be set by realloc() on failure.
184 *
185 * @param mem (@c void**) pointer to the pointer to allocated block
186 * @param n (@c size_t) the new size in bytes
187 * @retval zero success
188 * @retval non-zero failure
189 * @see cx_reallocatearray()
190 */
191 #define cx_reallocate(mem, n) cx_reallocate((void**)(mem), n)
192
193 /**
194 * Re-allocate a previously allocated block and changes the pointer in-place,
195 * if necessary.
196 *
197 * The size is calculated by multiplying @p nemb and @p size.
198 *
199 * @par Error handling
200 * @c errno will be set by realloc() on failure or when the multiplication of
201 * @p nmemb and @p size overflows.
202 *
203 * @param mem (@c void**) pointer to the pointer to allocated block
204 * @param nmemb (@c size_t) the number of elements
205 * @param size (@c size_t) the size of each element
206 * @retval zero success
207 * @retval non-zero failure
208 */
209 #define cx_reallocatearray(mem, nmemb, size) \
210 cx_reallocatearray((void**)(mem), nmemb, size)
211
212 /**
213 * Free a block allocated by this allocator.
214 *
215 * @note Freeing a block of a different allocator is undefined.
216 *
217 * @param allocator the allocator
218 * @param mem a pointer to the block to free
219 */
220 cx_attr_nonnull_arg(1)
221 void cxFree(
222 const CxAllocator *allocator,
223 void *mem
224 );
225
226 /**
227 * Allocate @p n bytes of memory.
155 * 228 *
156 * @param allocator the allocator 229 * @param allocator the allocator
157 * @param n the number of bytes 230 * @param n the number of bytes
158 * @return a pointer to the allocated memory 231 * @return a pointer to the allocated memory
159 */ 232 */
160 __attribute__((__malloc__)) 233 cx_attr_nodiscard
161 __attribute__((__alloc_size__(2))) 234 cx_attr_nonnull
235 cx_attr_malloc
236 cx_attr_dealloc_ucx
237 cx_attr_allocsize(2)
162 void *cxMalloc( 238 void *cxMalloc(
163 const CxAllocator *allocator, 239 const CxAllocator *allocator,
164 size_t n 240 size_t n
165 ); 241 );
166 242
167 /** 243 /**
168 * Re-allocate the previously allocated block in \p mem, making the new block \p n bytes long. 244 * Re-allocate the previously allocated block in @p mem, making the new block
169 * This function may return the same pointer that was passed to it, if moving the memory 245 * @p n bytes long.
170 * was not necessary. 246 * This function may return the same pointer that was passed to it, if moving
171 * 247 * the memory was not necessary.
172 * \note Re-allocating a block allocated by a different allocator is undefined. 248 *
249 * @note Re-allocating a block allocated by a different allocator is undefined.
173 * 250 *
174 * @param allocator the allocator 251 * @param allocator the allocator
175 * @param mem pointer to the previously allocated block 252 * @param mem pointer to the previously allocated block
176 * @param n the new size in bytes 253 * @param n the new size in bytes
177 * @return a pointer to the re-allocated memory 254 * @return a pointer to the re-allocated memory
178 */ 255 */
179 __attribute__((__warn_unused_result__)) 256 cx_attr_nodiscard
180 __attribute__((__alloc_size__(3))) 257 cx_attr_nonnull_arg(1)
258 cx_attr_dealloc_ucx
259 cx_attr_allocsize(3)
181 void *cxRealloc( 260 void *cxRealloc(
182 const CxAllocator *allocator, 261 const CxAllocator *allocator,
183 void *mem, 262 void *mem,
184 size_t n 263 size_t n
185 ); 264 );
186 265
187 /** 266 /**
188 * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. 267 * Re-allocate the previously allocated block in @p mem, making the new block
189 * This function acts like cxRealloc() using the pointer pointed to by \p mem. 268 * @p n bytes long.
190 * 269 * This function may return the same pointer that was passed to it, if moving
191 * \note Re-allocating a block allocated by a different allocator is undefined. 270 * the memory was not necessary.
192 * 271 *
193 * \par Error handling 272 * The size is calculated by multiplying @p nemb and @p size.
194 * \c errno will be set, if the underlying realloc function does so. 273 * If that multiplication overflows, this function returns @c NULL and @c errno
274 * will be set.
275 *
276 * @note Re-allocating a block allocated by a different allocator is undefined.
277 *
278 * @param allocator the allocator
279 * @param mem pointer to the previously allocated block
280 * @param nmemb the number of elements
281 * @param size the size of each element
282 * @return a pointer to the re-allocated memory
283 */
284 cx_attr_nodiscard
285 cx_attr_nonnull_arg(1)
286 cx_attr_dealloc_ucx
287 cx_attr_allocsize(3, 4)
288 void *cxReallocArray(
289 const CxAllocator *allocator,
290 void *mem,
291 size_t nmemb,
292 size_t size
293 );
294
295 /**
296 * Re-allocate a previously allocated block and changes the pointer in-place,
297 * if necessary.
298 * This function acts like cxRealloc() using the pointer pointed to by @p mem.
299 *
300 * @note Re-allocating a block allocated by a different allocator is undefined.
301 *
302 * @par Error handling
303 * @c errno will be set, if the underlying realloc function does so.
195 * 304 *
196 * @param allocator the allocator 305 * @param allocator the allocator
197 * @param mem pointer to the pointer to allocated block 306 * @param mem pointer to the pointer to allocated block
198 * @param n the new size in bytes 307 * @param n the new size in bytes
199 * @return zero on success, non-zero on failure 308 * @retval zero success
200 */ 309 * @retval non-zero failure
201 __attribute__((__nonnull__)) 310 */
311 cx_attr_nodiscard
312 cx_attr_nonnull
202 int cxReallocate( 313 int cxReallocate(
203 const CxAllocator *allocator, 314 const CxAllocator *allocator,
204 void **mem, 315 void **mem,
205 size_t n 316 size_t n
206 ); 317 );
207 318
208 /** 319 /**
209 * Allocate \p nelem elements of \p n bytes each, all initialized to zero. 320 * Re-allocate a previously allocated block and changes the pointer in-place,
321 * if necessary.
322 * This function acts like cxRealloc() using the pointer pointed to by @p mem.
323 *
324 * @note Re-allocating a block allocated by a different allocator is undefined.
325 *
326 * @par Error handling
327 * @c errno will be set, if the underlying realloc function does so.
328 *
329 * @param allocator (@c CxAllocator*) the allocator
330 * @param mem (@c void**) pointer to the pointer to allocated block
331 * @param n (@c size_t) the new size in bytes
332 * @retval zero success
333 * @retval non-zero failure
334 */
335 #define cxReallocate(allocator, mem, n) \
336 cxReallocate(allocator, (void**)(mem), n)
337
338 /**
339 * Re-allocate a previously allocated block and changes the pointer in-place,
340 * if necessary.
341 * This function acts like cxReallocArray() using the pointer pointed to
342 * by @p mem.
343 *
344 * @note Re-allocating a block allocated by a different allocator is undefined.
345 *
346 * @par Error handling
347 * @c errno will be set, if the underlying realloc function does so or the
348 * multiplication of @p nmemb and @p size overflows.
349 *
350 * @param allocator the allocator
351 * @param mem pointer to the pointer to allocated block
352 * @param nmemb the number of elements
353 * @param size the size of each element
354 * @retval zero success
355 * @retval non-zero on failure
356 */
357 cx_attr_nodiscard
358 cx_attr_nonnull
359 int cxReallocateArray(
360 const CxAllocator *allocator,
361 void **mem,
362 size_t nmemb,
363 size_t size
364 );
365
366 /**
367 * Re-allocate a previously allocated block and changes the pointer in-place,
368 * if necessary.
369 * This function acts like cxReallocArray() using the pointer pointed to
370 * by @p mem.
371 *
372 * @note Re-allocating a block allocated by a different allocator is undefined.
373 *
374 * @par Error handling
375 * @c errno will be set, if the underlying realloc function does so or the
376 * multiplication of @p nmemb and @p size overflows.
377 *
378 * @param allocator (@c CxAllocator*) the allocator
379 * @param mem (@c void**) pointer to the pointer to allocated block
380 * @param nmemb (@c size_t) the number of elements
381 * @param size (@c size_t) the size of each element
382 * @retval zero success
383 * @retval non-zero failure
384 */
385 #define cxReallocateArray(allocator, mem, nmemb, size) \
386 cxReallocateArray(allocator, (void**) (mem), nmemb, size)
387
388 /**
389 * Allocate @p nelem elements of @p n bytes each, all initialized to zero.
210 * 390 *
211 * @param allocator the allocator 391 * @param allocator the allocator
212 * @param nelem the number of elements 392 * @param nelem the number of elements
213 * @param n the size of each element in bytes 393 * @param n the size of each element in bytes
214 * @return a pointer to the allocated memory 394 * @return a pointer to the allocated memory
215 */ 395 */
216 __attribute__((__malloc__)) 396 cx_attr_nonnull_arg(1)
217 __attribute__((__alloc_size__(2, 3))) 397 cx_attr_nodiscard
398 cx_attr_malloc
399 cx_attr_dealloc_ucx
400 cx_attr_allocsize(2, 3)
218 void *cxCalloc( 401 void *cxCalloc(
219 const CxAllocator *allocator, 402 const CxAllocator *allocator,
220 size_t nelem, 403 size_t nelem,
221 size_t n 404 size_t n
222 );
223
224 /**
225 * Free a block allocated by this allocator.
226 *
227 * \note Freeing a block of a different allocator is undefined.
228 *
229 * @param allocator the allocator
230 * @param mem a pointer to the block to free
231 */
232 __attribute__((__nonnull__))
233 void cxFree(
234 const CxAllocator *allocator,
235 void *mem
236 ); 405 );
237 406
238 #ifdef __cplusplus 407 #ifdef __cplusplus
239 } // extern "C" 408 } // extern "C"
240 #endif 409 #endif

mercurial