ucx/cx/allocator.h

branch
dav-2
changeset 886
da79af4baec8
parent 854
1c8401ece69e
child 889
42cdbf9bbd49
equal deleted inserted replaced
885:591377a27fa3 886:da79af4baec8
96 * High-Level type alias for the allocator type. 96 * High-Level type alias for the allocator type.
97 */ 97 */
98 typedef struct cx_allocator_s CxAllocator; 98 typedef struct cx_allocator_s CxAllocator;
99 99
100 /** 100 /**
101 * A default allocator using standard library malloc() etc. 101 * A pre-defined allocator using standard library malloc() etc.
102 */ 102 */
103 cx_attr_export 103 cx_attr_export
104 extern const CxAllocator * const cxDefaultAllocator; 104 extern const CxAllocator * const cxStdlibAllocator;
105
106 /**
107 * The default allocator that is used by UCX.
108 * Initialized with cxStdlibAllocator, but you may change it.
109 */
110 cx_attr_export
111 extern const CxAllocator * cxDefaultAllocator;
105 112
106 /** 113 /**
107 * Function pointer type for destructor functions. 114 * Function pointer type for destructor functions.
108 * 115 *
109 * A destructor function deallocates possible contents and MAY free the memory 116 * A destructor function deallocates possible contents and MAY free the memory
133 140
134 /** 141 /**
135 * Reallocate a previously allocated block and changes the pointer in-place, 142 * Reallocate a previously allocated block and changes the pointer in-place,
136 * if necessary. 143 * if necessary.
137 * 144 *
145 * @note This will use stdlib reallocate and @em not the cxDefaultAllocator.
146 *
138 * @par Error handling 147 * @par Error handling
139 * @c errno will be set by realloc() on failure. 148 * @c errno will be set by realloc() on failure.
140 * 149 *
141 * @param mem pointer to the pointer to allocated block 150 * @param mem pointer to the pointer to allocated block
142 * @param n the new size in bytes 151 * @param n the new size in bytes
155 /** 164 /**
156 * Reallocate a previously allocated block and changes the pointer in-place, 165 * Reallocate a previously allocated block and changes the pointer in-place,
157 * if necessary. 166 * if necessary.
158 * 167 *
159 * The size is calculated by multiplying @p nemb and @p size. 168 * The size is calculated by multiplying @p nemb and @p size.
169 *
170 * @note This will use stdlib reallocate and @em not the cxDefaultAllocator.
160 * 171 *
161 * @par Error handling 172 * @par Error handling
162 * @c errno will be set by realloc() on failure or when the multiplication of 173 * @c errno will be set by realloc() on failure or when the multiplication of
163 * @p nmemb and @p size overflows. 174 * @p nmemb and @p size overflows.
164 * 175 *
180 191
181 /** 192 /**
182 * Reallocate a previously allocated block and changes the pointer in-place, 193 * Reallocate a previously allocated block and changes the pointer in-place,
183 * if necessary. 194 * if necessary.
184 * 195 *
196 * @note This will use stdlib reallocate and @em not the cxDefaultAllocator.
197 *
185 * @par Error handling 198 * @par Error handling
186 * @c errno will be set by realloc() on failure. 199 * @c errno will be set by realloc() on failure.
187 * 200 *
188 * @param mem (@c void**) pointer to the pointer to allocated block 201 * @param mem (@c void**) pointer to the pointer to allocated block
189 * @param n (@c size_t) the new size in bytes 202 * @param n (@c size_t) the new size in bytes
196 /** 209 /**
197 * Reallocate a previously allocated block and changes the pointer in-place, 210 * Reallocate a previously allocated block and changes the pointer in-place,
198 * if necessary. 211 * if necessary.
199 * 212 *
200 * The size is calculated by multiplying @p nemb and @p size. 213 * The size is calculated by multiplying @p nemb and @p size.
214 *
215 * @note This will use stdlib reallocate and @em not the cxDefaultAllocator.
201 * 216 *
202 * @par Error handling 217 * @par Error handling
203 * @c errno will be set by realloc() on failure or when the multiplication of 218 * @c errno will be set by realloc() on failure or when the multiplication of
204 * @p nmemb and @p size overflows. 219 * @p nmemb and @p size overflows.
205 * 220 *
209 * @retval zero success 224 * @retval zero success
210 * @retval non-zero failure 225 * @retval non-zero failure
211 */ 226 */
212 #define cx_reallocatearray(mem, nmemb, size) \ 227 #define cx_reallocatearray(mem, nmemb, size) \
213 cx_reallocatearray_((void**)(mem), nmemb, size) 228 cx_reallocatearray_((void**)(mem), nmemb, size)
229
230 /**
231 * Allocates memory and sets every byte to zero.
232 *
233 * @param n (@c size_t) the number of bytes
234 * @return (@c void*) a pointer to the allocated memory
235 */
236 #define cx_zalloc(n) calloc(1, n)
214 237
215 /** 238 /**
216 * Free a block allocated by this allocator. 239 * Free a block allocated by this allocator.
217 * 240 *
218 * @note Freeing a block of a different allocator is undefined. 241 * @note Freeing a block of a different allocator is undefined.
412 const CxAllocator *allocator, 435 const CxAllocator *allocator,
413 size_t nmemb, 436 size_t nmemb,
414 size_t size 437 size_t size
415 ); 438 );
416 439
440 /**
441 * Allocate @p n bytes of memory and sets every byte to zero.
442 *
443 * @param allocator the allocator
444 * @param n the number of bytes
445 * @return a pointer to the allocated memory
446 */
447 cx_attr_nodiscard
448 cx_attr_nonnull
449 cx_attr_malloc
450 cx_attr_dealloc_ucx
451 cx_attr_allocsize(2)
452 cx_attr_export
453 void *cxZalloc(
454 const CxAllocator *allocator,
455 size_t n
456 );
457
458 /**
459 * Convenience macro that invokes cxMalloc() with the cxDefaultAllocator.
460 */
461 #define cxMallocDefault(...) cxMalloc(cxDefaultAllocator, __VA_ARGS__)
462 /**
463 * Convenience macro that invokes cxZalloc() with the cxDefaultAllocator.
464 */
465 #define cxZallocDefault(...) cxZalloc(cxDefaultAllocator, __VA_ARGS__)
466 /**
467 * Convenience macro that invokes cxCalloc() with the cxDefaultAllocator.
468 */
469 #define cxCallocDefault(...) cxCalloc(cxDefaultAllocator, __VA_ARGS__)
470 /**
471 * Convenience macro that invokes cxRealloc() with the cxDefaultAllocator.
472 */
473 #define cxReallocDefault(...) cxRealloc(cxDefaultAllocator, __VA_ARGS__)
474 /**
475 * Convenience macro that invokes cxReallocate() with the cxDefaultAllocator.
476 */
477 #define cxReallocateDefault(...) cxReallocate(cxDefaultAllocator, __VA_ARGS__)
478 /**
479 * Convenience macro that invokes cxReallocateArray() with the cxDefaultAllocator.
480 */
481 #define cxReallocateArrayDefault(...) cxReallocateArray(cxDefaultAllocator, __VA_ARGS__)
482 /**
483 * Convenience macro that invokes cxReallocArray() with the cxDefaultAllocator.
484 */
485 #define cxReallocArrayDefault(...) cxReallocArray(cxDefaultAllocator, __VA_ARGS__)
486 /**
487 * Convenience macro that invokes cxFree() with the cxDefaultAllocator.
488 */
489 #define cxFreeDefault(...) cxFree(cxDefaultAllocator, __VA_ARGS__)
490
417 #ifdef __cplusplus 491 #ifdef __cplusplus
418 } // extern "C" 492 } // extern "C"
419 #endif 493 #endif
420 494
421 #endif // UCX_ALLOCATOR_H 495 #endif // UCX_ALLOCATOR_H

mercurial