src/cx/allocator.h

changeset 1426
3a89b31f0724
parent 1424
563033aa998c
equal deleted inserted replaced
1425:83284b289430 1426:3a89b31f0724
44 */ 44 */
45 typedef struct { 45 typedef struct {
46 /** 46 /**
47 * The allocator's malloc() implementation. 47 * The allocator's malloc() implementation.
48 */ 48 */
49 void *(*malloc)( 49 void *(*malloc)(void *data, size_t n);
50 void *data,
51 size_t n
52 );
53 50
54 /** 51 /**
55 * The allocator's realloc() implementation. 52 * The allocator's realloc() implementation.
56 */ 53 */
57 void *(*realloc)( 54 void *(*realloc)(void *data, void *mem, size_t n);
58 void *data,
59 void *mem,
60 size_t n
61 );
62 55
63 /** 56 /**
64 * The allocator's calloc() implementation. 57 * The allocator's calloc() implementation.
65 */ 58 */
66 void *(*calloc)( 59 void *(*calloc)(void *data, size_t nmemb, size_t size);
67 void *data,
68 size_t nmemb,
69 size_t size
70 );
71 60
72 /** 61 /**
73 * The allocator's free() implementation. 62 * The allocator's free() implementation.
74 */ 63 */
75 void (*free)( 64 void (*free)(void *data, void *mem);
76 void *data,
77 void *mem
78 );
79 } cx_allocator_class; 65 } cx_allocator_class;
80 66
81 /** 67 /**
82 * Structure holding the data for an allocator. 68 * Structure holding the data for an allocator.
83 */ 69 */
98 typedef struct cx_allocator_s CxAllocator; 84 typedef struct cx_allocator_s CxAllocator;
99 85
100 /** 86 /**
101 * A pre-defined allocator using standard library malloc() etc. 87 * A pre-defined allocator using standard library malloc() etc.
102 */ 88 */
103 cx_attr_export 89 CX_EXPORT extern const CxAllocator * const cxStdlibAllocator;
104 extern const CxAllocator * const cxStdlibAllocator;
105 90
106 /** 91 /**
107 * The default allocator that is used by UCX. 92 * The default allocator that is used by UCX.
108 * Initialized with cxStdlibAllocator, but you may change it. 93 * Initialized with cxStdlibAllocator, but you may change it.
109 */ 94 */
110 cx_attr_export 95 CX_EXPORT extern const CxAllocator * cxDefaultAllocator;
111 extern const CxAllocator * cxDefaultAllocator;
112 96
113 /** 97 /**
114 * Function pointer type for destructor functions. 98 * Function pointer type for destructor functions.
115 * 99 *
116 * A destructor function deallocates possible contents and MAY free the memory 100 * A destructor function deallocates possible contents and MAY free the memory
131 * that particular implementation. 115 * that particular implementation.
132 * 116 *
133 * @param data an optional pointer to custom data 117 * @param data an optional pointer to custom data
134 * @param memory a pointer to the object to destruct 118 * @param memory a pointer to the object to destruct
135 */ 119 */
136 typedef void (*cx_destructor_func2)( 120 typedef void (*cx_destructor_func2)(void *data, void *memory);
137 void *data,
138 void *memory
139 );
140 121
141 /** 122 /**
142 * Reallocate a previously allocated block and changes the pointer in-place, 123 * Reallocate a previously allocated block and changes the pointer in-place,
143 * if necessary. 124 * if necessary.
144 * 125 *
151 * @param n the new size in bytes 132 * @param n the new size in bytes
152 * @retval zero success 133 * @retval zero success
153 * @retval non-zero failure 134 * @retval non-zero failure
154 * @see cx_reallocatearray() 135 * @see cx_reallocatearray()
155 */ 136 */
156 cx_attr_nonnull 137 cx_attr_nonnull cx_attr_nodiscard
157 cx_attr_nodiscard 138 CX_EXPORT int cx_reallocate_(
158 cx_attr_export
159 int cx_reallocate_(
160 void **mem, 139 void **mem,
161 size_t n 140 size_t n
162 ); 141 );
163 142
164 /** 143 /**
178 * @param size the size of each element 157 * @param size the size of each element
179 * @retval zero success 158 * @retval zero success
180 * @retval non-zero failure 159 * @retval non-zero failure
181 * @see cx_reallocate() 160 * @see cx_reallocate()
182 */ 161 */
183 cx_attr_nonnull 162 cx_attr_nonnull cx_attr_nodiscard
184 cx_attr_nodiscard 163 CX_EXPORT int cx_reallocatearray_(void **mem, size_t nmemb, size_t size);
185 cx_attr_export
186 int cx_reallocatearray_(
187 void **mem,
188 size_t nmemb,
189 size_t size
190 );
191 164
192 /** 165 /**
193 * Reallocate a previously allocated block and changes the pointer in-place, 166 * Reallocate a previously allocated block and changes the pointer in-place,
194 * if necessary. 167 * if necessary.
195 * 168 *
242 * 215 *
243 * @param allocator the allocator 216 * @param allocator the allocator
244 * @param mem a pointer to the block to free 217 * @param mem a pointer to the block to free
245 */ 218 */
246 cx_attr_nonnull_arg(1) 219 cx_attr_nonnull_arg(1)
247 cx_attr_export 220 CX_EXPORT void cxFree(const CxAllocator *allocator, void *mem);
248 void cxFree(
249 const CxAllocator *allocator,
250 void *mem
251 );
252 221
253 /** 222 /**
254 * Allocate @p n bytes of memory. 223 * Allocate @p n bytes of memory.
255 * 224 *
256 * @param allocator the allocator 225 * @param allocator the allocator
257 * @param n the number of bytes 226 * @param n the number of bytes
258 * @return a pointer to the allocated memory 227 * @return a pointer to the allocated memory
259 */ 228 */
260 cx_attr_nodiscard 229 cx_attr_nodiscard cx_attr_nonnull
261 cx_attr_nonnull 230 cx_attr_malloc cx_attr_dealloc_ucx cx_attr_allocsize(2)
262 cx_attr_malloc 231 CX_EXPORT void *cxMalloc(const CxAllocator *allocator, size_t n);
263 cx_attr_dealloc_ucx
264 cx_attr_allocsize(2)
265 cx_attr_export
266 void *cxMalloc(
267 const CxAllocator *allocator,
268 size_t n
269 );
270 232
271 /** 233 /**
272 * Reallocate the previously allocated block in @p mem, making the new block 234 * Reallocate the previously allocated block in @p mem, making the new block
273 * @p n bytes long. 235 * @p n bytes long.
274 * This function may return the same pointer passed to it if moving 236 * This function may return the same pointer passed to it if moving
279 * @param allocator the allocator 241 * @param allocator the allocator
280 * @param mem pointer to the previously allocated block 242 * @param mem pointer to the previously allocated block
281 * @param n the new size in bytes 243 * @param n the new size in bytes
282 * @return a pointer to the reallocated memory 244 * @return a pointer to the reallocated memory
283 */ 245 */
284 cx_attr_nodiscard 246 cx_attr_nodiscard cx_attr_nonnull_arg(1)
285 cx_attr_nonnull_arg(1) 247 cx_attr_dealloc_ucx cx_attr_allocsize(3)
286 cx_attr_dealloc_ucx 248 CX_EXPORT void *cxRealloc(const CxAllocator *allocator, void *mem, size_t n);
287 cx_attr_allocsize(3)
288 cx_attr_export
289 void *cxRealloc(
290 const CxAllocator *allocator,
291 void *mem,
292 size_t n
293 );
294 249
295 /** 250 /**
296 * Reallocate the previously allocated block in @p mem, making the new block 251 * Reallocate the previously allocated block in @p mem, making the new block
297 * @p n bytes long. 252 * @p n bytes long.
298 * This function may return the same pointer passed to it if moving 253 * This function may return the same pointer passed to it if moving
308 * @param mem pointer to the previously allocated block 263 * @param mem pointer to the previously allocated block
309 * @param nmemb the number of elements 264 * @param nmemb the number of elements
310 * @param size the size of each element 265 * @param size the size of each element
311 * @return a pointer to the reallocated memory 266 * @return a pointer to the reallocated memory
312 */ 267 */
313 cx_attr_nodiscard 268 cx_attr_nodiscard cx_attr_nonnull_arg(1)
314 cx_attr_nonnull_arg(1) 269 cx_attr_dealloc_ucx cx_attr_allocsize(3, 4)
315 cx_attr_dealloc_ucx 270 CX_EXPORT void *cxReallocArray(const CxAllocator *allocator,
316 cx_attr_allocsize(3, 4) 271 void *mem, size_t nmemb, size_t size);
317 cx_attr_export
318 void *cxReallocArray(
319 const CxAllocator *allocator,
320 void *mem,
321 size_t nmemb,
322 size_t size
323 );
324 272
325 /** 273 /**
326 * Reallocate a previously allocated block and changes the pointer in-place, 274 * Reallocate a previously allocated block and changes the pointer in-place,
327 * if necessary. 275 * if necessary.
328 * This function acts like cxRealloc() using the pointer pointed to by @p mem. 276 * This function acts like cxRealloc() using the pointer pointed to by @p mem.
336 * @param mem pointer to the pointer to allocated block 284 * @param mem pointer to the pointer to allocated block
337 * @param n the new size in bytes 285 * @param n the new size in bytes
338 * @retval zero success 286 * @retval zero success
339 * @retval non-zero failure 287 * @retval non-zero failure
340 */ 288 */
341 cx_attr_nodiscard 289 cx_attr_nodiscard cx_attr_nonnull
342 cx_attr_nonnull 290 CX_EXPORT int cxReallocate_(const CxAllocator *allocator, void **mem, size_t n);
343 cx_attr_export
344 int cxReallocate_(
345 const CxAllocator *allocator,
346 void **mem,
347 size_t n
348 );
349 291
350 /** 292 /**
351 * Reallocate a previously allocated block and changes the pointer in-place, 293 * Reallocate a previously allocated block and changes the pointer in-place,
352 * if necessary. 294 * if necessary.
353 * This function acts like cxRealloc() using the pointer pointed to by @p mem. 295 * This function acts like cxRealloc() using the pointer pointed to by @p mem.
383 * @param nmemb the number of elements 325 * @param nmemb the number of elements
384 * @param size the size of each element 326 * @param size the size of each element
385 * @retval zero success 327 * @retval zero success
386 * @retval non-zero on failure 328 * @retval non-zero on failure
387 */ 329 */
388 cx_attr_nodiscard 330 cx_attr_nodiscard cx_attr_nonnull
389 cx_attr_nonnull 331 CX_EXPORT int cxReallocateArray_(const CxAllocator *allocator,
390 cx_attr_export 332 void **mem, size_t nmemb, size_t size);
391 int cxReallocateArray_(
392 const CxAllocator *allocator,
393 void **mem,
394 size_t nmemb,
395 size_t size
396 );
397 333
398 /** 334 /**
399 * Reallocate a previously allocated block and changes the pointer in-place, 335 * Reallocate a previously allocated block and changes the pointer in-place,
400 * if necessary. 336 * if necessary.
401 * This function acts like cxReallocArray() using the pointer pointed to 337 * This function acts like cxReallocArray() using the pointer pointed to
423 * @param allocator the allocator 359 * @param allocator the allocator
424 * @param nmemb the number of elements 360 * @param nmemb the number of elements
425 * @param size the size of each element in bytes 361 * @param size the size of each element in bytes
426 * @return a pointer to the allocated memory 362 * @return a pointer to the allocated memory
427 */ 363 */
428 cx_attr_nonnull_arg(1) 364 cx_attr_nonnull_arg(1) cx_attr_nodiscard
429 cx_attr_nodiscard 365 cx_attr_malloc cx_attr_dealloc_ucx cx_attr_allocsize(2, 3)
430 cx_attr_malloc 366 CX_EXPORT void *cxCalloc(const CxAllocator *allocator, size_t nmemb, size_t size);
431 cx_attr_dealloc_ucx
432 cx_attr_allocsize(2, 3)
433 cx_attr_export
434 void *cxCalloc(
435 const CxAllocator *allocator,
436 size_t nmemb,
437 size_t size
438 );
439 367
440 /** 368 /**
441 * Allocate @p n bytes of memory and sets every byte to zero. 369 * Allocate @p n bytes of memory and sets every byte to zero.
442 * 370 *
443 * @param allocator the allocator 371 * @param allocator the allocator
444 * @param n the number of bytes 372 * @param n the number of bytes
445 * @return a pointer to the allocated memory 373 * @return a pointer to the allocated memory
446 */ 374 */
447 cx_attr_nodiscard 375 cx_attr_nodiscard cx_attr_nonnull
448 cx_attr_nonnull 376 cx_attr_malloc cx_attr_dealloc_ucx cx_attr_allocsize(2)
449 cx_attr_malloc 377 CX_EXPORT void *cxZalloc(const CxAllocator *allocator, size_t n);
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 378
458 /** 379 /**
459 * Convenience macro that invokes cxMalloc() with the cxDefaultAllocator. 380 * Convenience macro that invokes cxMalloc() with the cxDefaultAllocator.
460 */ 381 */
461 #define cxMallocDefault(...) cxMalloc(cxDefaultAllocator, __VA_ARGS__) 382 #define cxMallocDefault(...) cxMalloc(cxDefaultAllocator, __VA_ARGS__)

mercurial