src/cx/allocator.h

changeset 1675
36c0fb2b60b2
parent 1610
ce0b0bf7d29c
equal deleted inserted replaced
1674:8b0f162ac88e 1675:36c0fb2b60b2
33 #ifndef UCX_ALLOCATOR_H 33 #ifndef UCX_ALLOCATOR_H
34 #define UCX_ALLOCATOR_H 34 #define UCX_ALLOCATOR_H
35 35
36 #include "common.h" 36 #include "common.h"
37 37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 /** 38 /**
43 * The class definition for an allocator. 39 * The class definition for an allocator.
44 */ 40 */
45 typedef struct { 41 typedef struct {
46 /** 42 /**
151 * If the page size cannot be retrieved from the system, 147 * If the page size cannot be retrieved from the system,
152 * a default of 4096 bytes is assumed. 148 * a default of 4096 bytes is assumed.
153 * 149 *
154 * @return the system's memory page size in bytes 150 * @return the system's memory page size in bytes
155 */ 151 */
156 cx_attr_nodiscard 152 CX_EXTERN CX_NODISCARD
157 CX_EXPORT unsigned long cx_system_page_size(void); 153 unsigned long cx_system_page_size(void);
158 154
159 /** 155 /**
160 * Reallocate a previously allocated block. 156 * Reallocate a previously allocated block.
161 * 157 *
162 * Internal function - do not use. 158 * Internal function - do not use.
165 * @param n the new size in bytes 161 * @param n the new size in bytes
166 * @retval zero success 162 * @retval zero success
167 * @retval non-zero failure 163 * @retval non-zero failure
168 * @see cx_reallocatearray() 164 * @see cx_reallocatearray()
169 */ 165 */
170 cx_attr_nonnull cx_attr_nodiscard 166 CX_EXTERN CX_NONNULL CX_NODISCARD
171 CX_EXPORT int cx_reallocate_(void **mem, size_t n); 167 int cx_reallocate_(void **mem, size_t n);
172 168
173 /** 169 /**
174 * Reallocate a previously allocated block. 170 * Reallocate a previously allocated block.
175 * 171 *
176 * Internal function - do not use. 172 * Internal function - do not use.
180 * @param size the size of each element 176 * @param size the size of each element
181 * @retval zero success 177 * @retval zero success
182 * @retval non-zero failure 178 * @retval non-zero failure
183 * @see cx_reallocate() 179 * @see cx_reallocate()
184 */ 180 */
185 cx_attr_nonnull cx_attr_nodiscard 181 CX_EXTERN CX_NONNULL CX_NODISCARD
186 CX_EXPORT int cx_reallocatearray_(void **mem, size_t nmemb, size_t size); 182 int cx_reallocatearray_(void **mem, size_t nmemb, size_t size);
187 183
188 /** 184 /**
189 * Reallocate a previously allocated block and changes the pointer in-place, 185 * Reallocate a previously allocated block and changes the pointer in-place,
190 * if necessary. 186 * if necessary.
191 * 187 *
237 * @note Freeing a block of a different allocator is undefined. 233 * @note Freeing a block of a different allocator is undefined.
238 * 234 *
239 * @param allocator the allocator 235 * @param allocator the allocator
240 * @param mem a pointer to the block to free 236 * @param mem a pointer to the block to free
241 */ 237 */
242 cx_attr_nonnull_arg(1) 238 CX_EXTERN CX_NONNULL_ARG(1)
243 CX_EXPORT void cxFree(const CxAllocator *allocator, void *mem); 239 void cxFree(const CxAllocator *allocator, void *mem);
244 240
245 /** 241 /**
246 * Allocate @p n bytes of memory. 242 * Allocate @p n bytes of memory.
247 * 243 *
248 * @param allocator the allocator 244 * @param allocator the allocator
249 * @param n the number of bytes 245 * @param n the number of bytes
250 * @return a pointer to the allocated memory 246 * @return a pointer to the allocated memory
251 */ 247 */
252 cx_attr_nodiscard cx_attr_nonnull 248 CX_EXTERN CX_NODISCARD CX_NONNULL
253 cx_attr_malloc cx_attr_dealloc_ucx cx_attr_allocsize(2) 249 CX_MALLOC CX_DEALLOC_UCX CX_ALLOCSIZE(2)
254 CX_EXPORT void *cxMalloc(const CxAllocator *allocator, size_t n); 250 void *cxMalloc(const CxAllocator *allocator, size_t n);
255 251
256 /** 252 /**
257 * Reallocate the previously allocated block in @p mem, making the new block 253 * Reallocate the previously allocated block in @p mem, making the new block
258 * @p n bytes long. 254 * @p n bytes long.
259 * This function may return the same pointer passed to it if moving 255 * This function may return the same pointer passed to it if moving
266 * @param allocator the allocator 262 * @param allocator the allocator
267 * @param mem pointer to the previously allocated block 263 * @param mem pointer to the previously allocated block
268 * @param n the new size in bytes 264 * @param n the new size in bytes
269 * @return a pointer to the reallocated memory 265 * @return a pointer to the reallocated memory
270 */ 266 */
271 cx_attr_nodiscard cx_attr_nonnull_arg(1) 267 CX_EXTERN CX_NODISCARD CX_NONNULL_ARG(1)
272 cx_attr_dealloc_ucx cx_attr_allocsize(3) 268 CX_DEALLOC_UCX CX_ALLOCSIZE(3)
273 CX_EXPORT void *cxRealloc(const CxAllocator *allocator, void *mem, size_t n); 269 void *cxRealloc(const CxAllocator *allocator, void *mem, size_t n);
274 270
275 /** 271 /**
276 * Reallocate the previously allocated block in @p mem. 272 * Reallocate the previously allocated block in @p mem.
277 * 273 *
278 * This function may return the same pointer passed to it if moving 274 * This function may return the same pointer passed to it if moving
290 * @param mem pointer to the previously allocated block 286 * @param mem pointer to the previously allocated block
291 * @param nmemb the number of elements 287 * @param nmemb the number of elements
292 * @param size the size of each element 288 * @param size the size of each element
293 * @return a pointer to the reallocated memory 289 * @return a pointer to the reallocated memory
294 */ 290 */
295 cx_attr_nodiscard cx_attr_nonnull_arg(1) 291 CX_EXTERN CX_NODISCARD CX_NONNULL_ARG(1)
296 cx_attr_dealloc_ucx cx_attr_allocsize(3, 4) 292 CX_DEALLOC_UCX CX_ALLOCSIZE(3, 4)
297 CX_EXPORT void *cxReallocArray(const CxAllocator *allocator, 293 void *cxReallocArray(const CxAllocator *allocator,
298 void *mem, size_t nmemb, size_t size); 294 void *mem, size_t nmemb, size_t size);
299 295
300 /** 296 /**
301 * Reallocate a previously allocated block. 297 * Reallocate a previously allocated block.
302 * 298 *
306 * @param mem pointer to the pointer to allocated block 302 * @param mem pointer to the pointer to allocated block
307 * @param n the new size in bytes 303 * @param n the new size in bytes
308 * @retval zero success 304 * @retval zero success
309 * @retval non-zero failure 305 * @retval non-zero failure
310 */ 306 */
311 cx_attr_nodiscard cx_attr_nonnull 307 CX_EXTERN CX_NODISCARD CX_NONNULL
312 CX_EXPORT int cxReallocate_(const CxAllocator *allocator, void **mem, size_t n); 308 int cxReallocate_(const CxAllocator *allocator, void **mem, size_t n);
313 309
314 /** 310 /**
315 * Reallocate a previously allocated block and changes the pointer in-place, 311 * Reallocate a previously allocated block and changes the pointer in-place,
316 * if necessary. 312 * if necessary.
317 * This function acts like cxRealloc() using the pointer pointed to by @p mem. 313 * This function acts like cxRealloc() using the pointer pointed to by @p mem.
340 * @param nmemb the number of elements 336 * @param nmemb the number of elements
341 * @param size the size of each element 337 * @param size the size of each element
342 * @retval zero success 338 * @retval zero success
343 * @retval non-zero on failure 339 * @retval non-zero on failure
344 */ 340 */
345 cx_attr_nodiscard cx_attr_nonnull 341 CX_EXTERN CX_NODISCARD CX_NONNULL
346 CX_EXPORT int cxReallocateArray_(const CxAllocator *allocator, 342 int cxReallocateArray_(const CxAllocator *allocator,
347 void **mem, size_t nmemb, size_t size); 343 void **mem, size_t nmemb, size_t size);
348 344
349 /** 345 /**
350 * Reallocate a previously allocated block and changes the pointer in-place, 346 * Reallocate a previously allocated block and changes the pointer in-place,
351 * if necessary. 347 * if necessary.
374 * @param allocator the allocator 370 * @param allocator the allocator
375 * @param nmemb the number of elements 371 * @param nmemb the number of elements
376 * @param size the size of each element in bytes 372 * @param size the size of each element in bytes
377 * @return a pointer to the allocated memory 373 * @return a pointer to the allocated memory
378 */ 374 */
379 cx_attr_nonnull_arg(1) cx_attr_nodiscard 375 CX_EXTERN CX_NONNULL_ARG(1) CX_NODISCARD
380 cx_attr_malloc cx_attr_dealloc_ucx cx_attr_allocsize(2, 3) 376 CX_MALLOC CX_DEALLOC_UCX CX_ALLOCSIZE(2, 3)
381 CX_EXPORT void *cxCalloc(const CxAllocator *allocator, size_t nmemb, size_t size); 377 void *cxCalloc(const CxAllocator *allocator, size_t nmemb, size_t size);
382 378
383 /** 379 /**
384 * Allocate @p n bytes of memory and sets every byte to zero. 380 * Allocate @p n bytes of memory and sets every byte to zero.
385 * 381 *
386 * @param allocator the allocator 382 * @param allocator the allocator
387 * @param n the number of bytes 383 * @param n the number of bytes
388 * @return a pointer to the allocated memory 384 * @return a pointer to the allocated memory
389 */ 385 */
390 cx_attr_nodiscard cx_attr_nonnull 386 CX_EXTERN CX_NODISCARD CX_NONNULL
391 cx_attr_malloc cx_attr_dealloc_ucx cx_attr_allocsize(2) 387 CX_MALLOC CX_DEALLOC_UCX CX_ALLOCSIZE(2)
392 CX_EXPORT void *cxZalloc(const CxAllocator *allocator, size_t n); 388 void *cxZalloc(const CxAllocator *allocator, size_t n);
393 389
394 /** 390 /**
395 * Allocate @p n bytes of memory. 391 * Allocate @p n bytes of memory.
396 * 392 *
397 * Convenience macro that invokes cxMalloc() with the cxDefaultAllocator. 393 * Convenience macro that invokes cxMalloc() with the cxDefaultAllocator.
508 * 504 *
509 * Convenience function that invokes cxFree() with the cxDefaultAllocator. 505 * Convenience function that invokes cxFree() with the cxDefaultAllocator.
510 * 506 *
511 * @param mem the memory to deallocate 507 * @param mem the memory to deallocate
512 */ 508 */
513 CX_EXPORT void cxFreeDefault(void *mem); 509 CX_EXTERN
514 510 void cxFreeDefault(void *mem);
515 #ifdef __cplusplus
516 } // extern "C"
517 #endif
518 511
519 #endif // UCX_ALLOCATOR_H 512 #endif // UCX_ALLOCATOR_H

mercurial