Interface for custom allocators.
More...
Go to the source code of this file.
|
typedef struct cx_allocator_s | CxAllocator |
| High-Level type alias for the allocator type.
|
|
typedef void(* | cx_destructor_func) (void *memory) |
| Function pointer type for destructor functions.
|
|
typedef void(* | cx_destructor_func2) (void *data, void *memory) |
| Function pointer type for destructor functions.
|
|
|
int | cx_reallocate (void **mem, size_t n) |
| Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
|
|
void * | cxMalloc (CxAllocator const *allocator, size_t n) |
| Allocate n bytes of memory.
|
|
void * | cxRealloc (CxAllocator const *allocator, void *mem, size_t n) |
| Re-allocate the previously allocated block in mem , making the new block n bytes long.
|
|
int | cxReallocate (CxAllocator const *allocator, void **mem, size_t n) |
| Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
|
|
void * | cxCalloc (CxAllocator const *allocator, size_t nelem, size_t n) |
| Allocate nelem elements of n bytes each, all initialized to zero.
|
|
void | cxFree (CxAllocator const *allocator, void *mem) |
| Free a block allocated by this allocator.
|
|
|
CxAllocator * | cxDefaultAllocator |
| A default allocator using standard library malloc() etc.
|
|
Interface for custom allocators.
◆ cx_destructor_func
typedef void(* cx_destructor_func) (void *memory) |
Function pointer type for destructor functions.
A destructor function deallocates possible contents and MAY free the memory pointed to by memory
. Read the documentation of the respective function pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in that particular implementation.
- Parameters
-
memory | a pointer to the object to destruct |
◆ cx_destructor_func2
typedef void(* cx_destructor_func2) (void *data, void *memory) |
Function pointer type for destructor functions.
A destructor function deallocates possible contents and MAY free the memory pointed to by memory
. Read the documentation of the respective function pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in that particular implementation.
- Parameters
-
data | an optional pointer to custom data |
memory | a pointer to the object to destruct |
◆ cx_reallocate()
int cx_reallocate |
( |
void ** |
mem, |
|
|
size_t |
n |
|
) |
| |
Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
- Error handling
errno
will be set by realloc() on failure.
- Parameters
-
mem | pointer to the pointer to allocated block |
n | the new size in bytes |
- Returns
- zero on success, non-zero on failure
◆ cxCalloc()
void * cxCalloc |
( |
CxAllocator const * |
allocator, |
|
|
size_t |
nelem, |
|
|
size_t |
n |
|
) |
| |
Allocate nelem
elements of n
bytes each, all initialized to zero.
- Parameters
-
allocator | the allocator |
nelem | the number of elements |
n | the size of each element in bytes |
- Returns
- a pointer to the allocated memory
◆ cxFree()
void cxFree |
( |
CxAllocator const * |
allocator, |
|
|
void * |
mem |
|
) |
| |
Free a block allocated by this allocator.
- Note
- Freeing a block of a different allocator is undefined.
- Parameters
-
allocator | the allocator |
mem | a pointer to the block to free |
◆ cxMalloc()
void * cxMalloc |
( |
CxAllocator const * |
allocator, |
|
|
size_t |
n |
|
) |
| |
Allocate n
bytes of memory.
- Parameters
-
allocator | the allocator |
n | the number of bytes |
- Returns
- a pointer to the allocated memory
◆ cxRealloc()
void * cxRealloc |
( |
CxAllocator const * |
allocator, |
|
|
void * |
mem, |
|
|
size_t |
n |
|
) |
| |
Re-allocate the previously allocated block in mem
, making the new block n
bytes long.
This function may return the same pointer that was passed to it, if moving the memory was not necessary.
- Note
- Re-allocating a block allocated by a different allocator is undefined.
- Parameters
-
allocator | the allocator |
mem | pointer to the previously allocated block |
n | the new size in bytes |
- Returns
- a pointer to the re-allocated memory
◆ cxReallocate()
int cxReallocate |
( |
CxAllocator const * |
allocator, |
|
|
void ** |
mem, |
|
|
size_t |
n |
|
) |
| |
Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
This function acts like cxRealloc() using the pointer pointed to by mem
.
- Note
- Re-allocating a block allocated by a different allocator is undefined.
- Error handling
errno
will be set, if the underlying realloc function does so.
- Parameters
-
allocator | the allocator |
mem | pointer to the pointer to allocated block |
n | the new size in bytes |
- Returns
- zero on success, non-zero on failure