src/cx/allocator.h

changeset 1610
ce0b0bf7d29c
parent 1593
0df5128ab675
equal deleted inserted replaced
1609:a767bf59cad3 1610:ce0b0bf7d29c
155 */ 155 */
156 cx_attr_nodiscard 156 cx_attr_nodiscard
157 CX_EXPORT unsigned long cx_system_page_size(void); 157 CX_EXPORT unsigned long cx_system_page_size(void);
158 158
159 /** 159 /**
160 * Reallocate a previously allocated block and changes the pointer in-place, 160 * Reallocate a previously allocated block.
161 * if necessary. 161 *
162 * 162 * Internal function - do not use.
163 * @note This will use stdlib reallocate and @em not the cxDefaultAllocator.
164 *
165 * @par Error handling
166 * @c errno will be set by realloc() on failure.
167 * 163 *
168 * @param mem pointer to the pointer to allocated block 164 * @param mem pointer to the pointer to allocated block
169 * @param n the new size in bytes 165 * @param n the new size in bytes
170 * @retval zero success 166 * @retval zero success
171 * @retval non-zero failure 167 * @retval non-zero failure
173 */ 169 */
174 cx_attr_nonnull cx_attr_nodiscard 170 cx_attr_nonnull cx_attr_nodiscard
175 CX_EXPORT int cx_reallocate_(void **mem, size_t n); 171 CX_EXPORT int cx_reallocate_(void **mem, size_t n);
176 172
177 /** 173 /**
178 * Reallocate a previously allocated block and changes the pointer in-place, 174 * Reallocate a previously allocated block.
179 * if necessary. 175 *
180 * 176 * Internal function - do not use.
181 * The size is calculated by multiplying @p nemb and @p size.
182 *
183 * @note This will use stdlib reallocate and @em not the cxDefaultAllocator.
184 *
185 * @par Error handling
186 * @c errno will be set by realloc() on failure or when the multiplication of
187 * @p nmemb and @p size overflows.
188 * 177 *
189 * @param mem pointer to the pointer to allocated block 178 * @param mem pointer to the pointer to allocated block
190 * @param nmemb the number of elements 179 * @param nmemb the number of elements
191 * @param size the size of each element 180 * @param size the size of each element
192 * @retval zero success 181 * @retval zero success
270 * This function may return the same pointer passed to it if moving 259 * This function may return the same pointer passed to it if moving
271 * the memory was not necessary. 260 * the memory was not necessary.
272 * 261 *
273 * @note Re-allocating a block allocated by a different allocator is undefined. 262 * @note Re-allocating a block allocated by a different allocator is undefined.
274 * 263 *
264 * @attention This function is bug-prone. Consider using cxReallocate().
265 *
275 * @param allocator the allocator 266 * @param allocator the allocator
276 * @param mem pointer to the previously allocated block 267 * @param mem pointer to the previously allocated block
277 * @param n the new size in bytes 268 * @param n the new size in bytes
278 * @return a pointer to the reallocated memory 269 * @return a pointer to the reallocated memory
279 */ 270 */
280 cx_attr_nodiscard cx_attr_nonnull_arg(1) 271 cx_attr_nodiscard cx_attr_nonnull_arg(1)
281 cx_attr_dealloc_ucx cx_attr_allocsize(3) 272 cx_attr_dealloc_ucx cx_attr_allocsize(3)
282 CX_EXPORT void *cxRealloc(const CxAllocator *allocator, void *mem, size_t n); 273 CX_EXPORT void *cxRealloc(const CxAllocator *allocator, void *mem, size_t n);
283 274
284 /** 275 /**
285 * Reallocate the previously allocated block in @p mem, making the new block 276 * Reallocate the previously allocated block in @p mem.
286 * @p n bytes long. 277 *
287 * This function may return the same pointer passed to it if moving 278 * This function may return the same pointer passed to it if moving
288 * the memory was not necessary. 279 * the memory was not necessary.
289 * 280 *
290 * The size is calculated by multiplying @p nemb and @p size. 281 * The size is calculated by multiplying @p nemb and @p size.
291 * If that multiplication overflows, this function returns @c NULL, and @c errno 282 * If that multiplication overflows, this function returns @c NULL, and @c errno
292 * will be set. 283 * will be set.
293 * 284 *
294 * @note Re-allocating a block allocated by a different allocator is undefined. 285 * @note Re-allocating a block allocated by a different allocator is undefined.
286 *
287 * @attention This function is bug-prone. Consider using cxReallocateArray().
295 * 288 *
296 * @param allocator the allocator 289 * @param allocator the allocator
297 * @param mem pointer to the previously allocated block 290 * @param mem pointer to the previously allocated block
298 * @param nmemb the number of elements 291 * @param nmemb the number of elements
299 * @param size the size of each element 292 * @param size the size of each element
303 cx_attr_dealloc_ucx cx_attr_allocsize(3, 4) 296 cx_attr_dealloc_ucx cx_attr_allocsize(3, 4)
304 CX_EXPORT void *cxReallocArray(const CxAllocator *allocator, 297 CX_EXPORT void *cxReallocArray(const CxAllocator *allocator,
305 void *mem, size_t nmemb, size_t size); 298 void *mem, size_t nmemb, size_t size);
306 299
307 /** 300 /**
301 * Reallocate a previously allocated block.
302 *
303 * Internal function - do not use.
304 *
305 * @param allocator the allocator
306 * @param mem pointer to the pointer to allocated block
307 * @param n the new size in bytes
308 * @retval zero success
309 * @retval non-zero failure
310 */
311 cx_attr_nodiscard cx_attr_nonnull
312 CX_EXPORT int cxReallocate_(const CxAllocator *allocator, void **mem, size_t n);
313
314 /**
308 * Reallocate a previously allocated block and changes the pointer in-place, 315 * Reallocate a previously allocated block and changes the pointer in-place,
309 * if necessary. 316 * if necessary.
310 * This function acts like cxRealloc() using the pointer pointed to by @p mem. 317 * This function acts like cxRealloc() using the pointer pointed to by @p mem.
311 * 318 *
312 * @note Re-allocating a block allocated by a different allocator is undefined. 319 * @note Re-allocating a block allocated by a different allocator is undefined.
313 * 320 *
314 * @par Error handling 321 * @par Error handling
315 * @c errno will be set if the underlying realloc function does so. 322 * @c errno will be set if the underlying realloc function does so.
316 * 323 *
317 * @param allocator the allocator
318 * @param mem pointer to the pointer to allocated block
319 * @param n the new size in bytes
320 * @retval zero success
321 * @retval non-zero failure
322 */
323 cx_attr_nodiscard cx_attr_nonnull
324 CX_EXPORT int cxReallocate_(const CxAllocator *allocator, void **mem, size_t n);
325
326 /**
327 * Reallocate a previously allocated block and changes the pointer in-place,
328 * if necessary.
329 * This function acts like cxRealloc() using the pointer pointed to by @p mem.
330 *
331 * @note Re-allocating a block allocated by a different allocator is undefined.
332 *
333 * @par Error handling
334 * @c errno will be set if the underlying realloc function does so.
335 *
336 * @param allocator (@c CxAllocator*) the allocator 324 * @param allocator (@c CxAllocator*) the allocator
337 * @param mem (@c void**) pointer to the pointer to allocated block 325 * @param mem (@c void**) pointer to the pointer to allocated block
338 * @param n (@c size_t) the new size in bytes 326 * @param n (@c size_t) the new size in bytes
339 * @retval zero success 327 * @retval zero success
340 * @retval non-zero failure 328 * @retval non-zero failure
341 */ 329 */
342 #define cxReallocate(allocator, mem, n) \ 330 #define cxReallocate(allocator, mem, n) \
343 cxReallocate_(allocator, (void**)(mem), n) 331 cxReallocate_(allocator, (void**)(mem), n)
332
333 /**
334 * Reallocate a previously allocated block.
335 *
336 * Internal function - do not use.
337 *
338 * @param allocator the allocator
339 * @param mem pointer to the pointer to allocated block
340 * @param nmemb the number of elements
341 * @param size the size of each element
342 * @retval zero success
343 * @retval non-zero on failure
344 */
345 cx_attr_nodiscard cx_attr_nonnull
346 CX_EXPORT int cxReallocateArray_(const CxAllocator *allocator,
347 void **mem, size_t nmemb, size_t size);
344 348
345 /** 349 /**
346 * Reallocate a previously allocated block and changes the pointer in-place, 350 * Reallocate a previously allocated block and changes the pointer in-place,
347 * if necessary. 351 * if necessary.
348 * This function acts like cxReallocArray() using the pointer pointed to 352 * This function acts like cxReallocArray() using the pointer pointed to
352 * 356 *
353 * @par Error handling 357 * @par Error handling
354 * @c errno will be set, if the underlying realloc function does so or the 358 * @c errno will be set, if the underlying realloc function does so or the
355 * multiplication of @p nmemb and @p size overflows. 359 * multiplication of @p nmemb and @p size overflows.
356 * 360 *
357 * @param allocator the allocator 361 * @param allocator (@c CxAllocator*) the allocator
358 * @param mem pointer to the pointer to allocated block 362 * @param mem (@c void**) pointer to the pointer to allocated block
363 * @param nmemb (@c size_t) the number of elements
364 * @param size (@c size_t) the size of each element
365 * @retval zero success
366 * @retval non-zero failure
367 */
368 #define cxReallocateArray(allocator, mem, nmemb, size) \
369 cxReallocateArray_(allocator, (void**) (mem), nmemb, size)
370
371 /**
372 * Allocate @p nmemb elements of @p size bytes each, all initialized to zero.
373 *
374 * @param allocator the allocator
359 * @param nmemb the number of elements 375 * @param nmemb the number of elements
360 * @param size the size of each element 376 * @param size the size of each element in bytes
361 * @retval zero success 377 * @return a pointer to the allocated memory
362 * @retval non-zero on failure 378 */
379 cx_attr_nonnull_arg(1) cx_attr_nodiscard
380 cx_attr_malloc cx_attr_dealloc_ucx cx_attr_allocsize(2, 3)
381 CX_EXPORT void *cxCalloc(const CxAllocator *allocator, size_t nmemb, size_t size);
382
383 /**
384 * Allocate @p n bytes of memory and sets every byte to zero.
385 *
386 * @param allocator the allocator
387 * @param n the number of bytes
388 * @return a pointer to the allocated memory
363 */ 389 */
364 cx_attr_nodiscard cx_attr_nonnull 390 cx_attr_nodiscard cx_attr_nonnull
365 CX_EXPORT int cxReallocateArray_(const CxAllocator *allocator, 391 cx_attr_malloc cx_attr_dealloc_ucx cx_attr_allocsize(2)
366 void **mem, size_t nmemb, size_t size); 392 CX_EXPORT void *cxZalloc(const CxAllocator *allocator, size_t n);
393
394 /**
395 * Allocate @p n bytes of memory.
396 *
397 * Convenience macro that invokes cxMalloc() with the cxDefaultAllocator.
398 *
399 * @param n (@c size_t) the number of bytes
400 * @return (@c void*) a pointer to the allocated memory
401 */
402 #define cxMallocDefault(n) cxMalloc(cxDefaultAllocator, n)
403
404 /**
405 * Allocate @p n bytes of memory and sets every byte to zero.
406 *
407 * Convenience macro that invokes cxZalloc() with the cxDefaultAllocator.
408 *
409 * @param n (@c size_t) the number of bytes
410 * @return (@c void*) a pointer to the allocated memory
411 */
412 #define cxZallocDefault(n) cxZalloc(cxDefaultAllocator, n)
413
414 /**
415 * Allocate @p nmemb elements of @p size bytes each, all initialized to zero.
416 *
417 * Convenience macro that invokes cxCalloc() with the cxDefaultAllocator.
418 *
419 * @param nmemb (@c size_t) the number of elements
420 * @param size (@c size_t) the size of each element in bytes
421 * @return (@c void*) a pointer to the allocated memory
422 */
423 #define cxCallocDefault(nmemb, size) cxCalloc(cxDefaultAllocator, nmemb, size)
424
425 /**
426 * Reallocate the previously allocated block in @p mem.
427 *
428 * This function may return the same pointer passed to it if moving
429 * the memory was not necessary.
430 *
431 * Convenience macro that invokes cxRealloc() with the cxDefaultAllocator.
432 *
433 * @attention This function is bug-prone. Consider using cxReallocateDefault().
434 *
435 * @param mem (@c void*) pointer to the previously allocated block
436 * @param n (@c size_t) the new size in bytes
437 * @return (@c void*) a pointer to the reallocated memory
438 */
439 #define cxReallocDefault(mem, n) cxRealloc(cxDefaultAllocator, mem, n)
440
441 /**
442 * Reallocate a previously allocated block and changes the pointer in-place,
443 * if necessary.
444 * This function acts like cxRealloc() using the pointer pointed to by @p mem.
445 *
446 * Convenience macro that invokes cxReallocate() with the cxDefaultAllocator.
447 *
448 * @note Re-allocating a block allocated by a different allocator is undefined.
449 *
450 * @par Error handling
451 * @c errno will be set if the underlying realloc function does so.
452 *
453 * @param mem (@c void**) pointer to the pointer to allocated block
454 * @param n (@c size_t) the new size in bytes
455 * @retval zero success
456 * @retval non-zero failure
457 */
458 #define cxReallocateDefault(mem, n) cxReallocate(cxDefaultAllocator, mem, n)
367 459
368 /** 460 /**
369 * Reallocate a previously allocated block and changes the pointer in-place, 461 * Reallocate a previously allocated block and changes the pointer in-place,
370 * if necessary. 462 * if necessary.
371 * This function acts like cxReallocArray() using the pointer pointed to 463 * This function acts like cxReallocArray() using the pointer pointed to
372 * by @p mem. 464 * by @p mem.
373 * 465 *
466 * Convenience macro that invokes cxReallocateArray() with the cxDefaultAllocator.
467 *
374 * @note Re-allocating a block allocated by a different allocator is undefined. 468 * @note Re-allocating a block allocated by a different allocator is undefined.
375 * 469 *
376 * @par Error handling 470 * @par Error handling
377 * @c errno will be set, if the underlying realloc function does so or the 471 * @c errno will be set, if the underlying realloc function does so or the
378 * multiplication of @p nmemb and @p size overflows. 472 * multiplication of @p nmemb and @p size overflows.
379 * 473 *
380 * @param allocator (@c CxAllocator*) the allocator
381 * @param mem (@c void**) pointer to the pointer to allocated block 474 * @param mem (@c void**) pointer to the pointer to allocated block
382 * @param nmemb (@c size_t) the number of elements 475 * @param nmemb (@c size_t) the number of elements
383 * @param size (@c size_t) the size of each element 476 * @param size (@c size_t) the size of each element
384 * @retval zero success 477 * @retval zero success
385 * @retval non-zero failure 478 * @retval non-zero failure
386 */ 479 */
387 #define cxReallocateArray(allocator, mem, nmemb, size) \ 480 #define cxReallocateArrayDefault(mem, nmemb, size) \
388 cxReallocateArray_(allocator, (void**) (mem), nmemb, size) 481 cxReallocateArray(cxDefaultAllocator, mem, nmemb, size)
389 482
390 /** 483 /**
391 * Allocate @p nmemb elements of @p n bytes each, all initialized to zero. 484 * Reallocate the previously allocated block in @p mem.
392 * 485 *
393 * @param allocator the allocator
394 * @param nmemb the number of elements
395 * @param size the size of each element in bytes
396 * @return a pointer to the allocated memory
397 */
398 cx_attr_nonnull_arg(1) cx_attr_nodiscard
399 cx_attr_malloc cx_attr_dealloc_ucx cx_attr_allocsize(2, 3)
400 CX_EXPORT void *cxCalloc(const CxAllocator *allocator, size_t nmemb, size_t size);
401
402 /**
403 * Allocate @p n bytes of memory and sets every byte to zero.
404 *
405 * @param allocator the allocator
406 * @param n the number of bytes
407 * @return a pointer to the allocated memory
408 */
409 cx_attr_nodiscard cx_attr_nonnull
410 cx_attr_malloc cx_attr_dealloc_ucx cx_attr_allocsize(2)
411 CX_EXPORT void *cxZalloc(const CxAllocator *allocator, size_t n);
412
413 /**
414 * Convenience macro that invokes cxMalloc() with the cxDefaultAllocator.
415 */
416 #define cxMallocDefault(...) cxMalloc(cxDefaultAllocator, __VA_ARGS__)
417 /**
418 * Convenience macro that invokes cxZalloc() with the cxDefaultAllocator.
419 */
420 #define cxZallocDefault(...) cxZalloc(cxDefaultAllocator, __VA_ARGS__)
421 /**
422 * Convenience macro that invokes cxCalloc() with the cxDefaultAllocator.
423 */
424 #define cxCallocDefault(...) cxCalloc(cxDefaultAllocator, __VA_ARGS__)
425 /**
426 * Convenience macro that invokes cxRealloc() with the cxDefaultAllocator.
427 */
428 #define cxReallocDefault(...) cxRealloc(cxDefaultAllocator, __VA_ARGS__)
429 /**
430 * Convenience macro that invokes cxReallocate() with the cxDefaultAllocator.
431 */
432 #define cxReallocateDefault(...) cxReallocate(cxDefaultAllocator, __VA_ARGS__)
433 /**
434 * Convenience macro that invokes cxReallocateArray() with the cxDefaultAllocator.
435 */
436 #define cxReallocateArrayDefault(...) cxReallocateArray(cxDefaultAllocator, __VA_ARGS__)
437 /**
438 * Convenience macro that invokes cxReallocArray() with the cxDefaultAllocator. 486 * Convenience macro that invokes cxReallocArray() with the cxDefaultAllocator.
439 */ 487 *
440 #define cxReallocArrayDefault(...) cxReallocArray(cxDefaultAllocator, __VA_ARGS__) 488 * This function may return the same pointer passed to it if moving
441 /** 489 * the memory was not necessary.
490 *
491 * The size is calculated by multiplying @p nemb and @p size.
492 * If that multiplication overflows, this function returns @c NULL, and @c errno
493 * will be set.
494 *
495 * @note Re-allocating a block allocated by a different allocator is undefined.
496 *
497 * @attention This function is bug-prone. Consider using cxReallocateArrayDefault().
498 *
499 * @param mem (@c void*) pointer to the previously allocated block
500 * @param nmemb (@c size_t) the number of elements
501 * @param size (@c size_t) the size of each element
502 * @return (@c void*) a pointer to the reallocated memory
503 */
504 #define cxReallocArrayDefault(mem, nmemb, size) cxReallocArray(cxDefaultAllocator, mem, nmemb, size)
505
506 /**
507 * Free a block of memory.
508 *
442 * Convenience function that invokes cxFree() with the cxDefaultAllocator. 509 * Convenience function that invokes cxFree() with the cxDefaultAllocator.
510 *
511 * @param mem the memory to deallocate
443 */ 512 */
444 CX_EXPORT void cxFreeDefault(void *mem); 513 CX_EXPORT void cxFreeDefault(void *mem);
445 514
446 #ifdef __cplusplus 515 #ifdef __cplusplus
447 } // extern "C" 516 } // extern "C"

mercurial