2 |
2 |
3 The allocator interface provides a mechanism to implement own custom allocators |
3 The allocator interface provides a mechanism to implement own custom allocators |
4 that can also be used in many other function in UCX. |
4 that can also be used in many other function in UCX. |
5 |
5 |
6 A default allocator implementation using the stdlib functions is |
6 A default allocator implementation using the stdlib functions is |
7 available via the global symbol `cxDefaultAllocator` |
7 available via the global symbol `cxStdlibAllocator`, |
8 and UCX also provides a [memory pool](mempool.h.md) implementation. |
8 and UCX also provides a [memory pool](mempool.h.md) implementation. |
9 You are free to add additional own custom implementations. |
9 You are free to add your additional, own custom implementations. |
10 A general sketch that illustrates how to do this can be found [below](#custom-allocator). |
10 A general sketch that illustrates how to do this can be found [below](#custom-allocator). |
|
11 |
|
12 ## Default Allocator |
|
13 |
|
14 The global default allocator which is used by UCX, |
|
15 when no specific allocator is specified, |
|
16 can be configured via the `cxDefaultAllocator`. |
|
17 It is by default set to the `cxStdlibAllocator`. |
11 |
18 |
12 ## Overview |
19 ## Overview |
13 |
20 |
14 ```C |
21 ```C |
15 #include <cx/allocator.h> |
22 #include <cx/allocator.h> |
34 int cx_reallocate(void **mem, size_t size); |
41 int cx_reallocate(void **mem, size_t size); |
35 |
42 |
36 int cx_reallocatearray(void **mem, size_t nmemb, size_t size); |
43 int cx_reallocatearray(void **mem, size_t nmemb, size_t size); |
37 |
44 |
38 // predefined allocator that uses stdlib functions |
45 // predefined allocator that uses stdlib functions |
39 CxAllocator *cxDefaultAllocator; |
46 CxAllocator * const cxStdlibAllocator; |
|
47 |
|
48 // default allocator that can be changed |
|
49 CxAllocator *cxDefaultAllocator = cxStdlibAllocator; |
40 ``` |
50 ``` |
41 |
51 |
42 > All UCX functions that are not _explicitly_ designed for taking an allocator argument |
52 > All UCX functions that are not _explicitly_ designed for taking an allocator argument |
43 > (recognizable by a `_a` suffix in the function's name) do support a `NULL` argument |
53 > (recognizable by a `_a` suffix in the function's name) do support a `NULL` argument |
44 > in which case the `cxDefaultAllocator` will be used. |
54 > in which case the `cxDefaultAllocator` will be used. |
|
55 > |
|
56 > You may change the default allocator at any time, but it is strongly recommended to |
|
57 > do it only once at program start to avoid accidentally freeing memory that was |
|
58 > allocated by a different allocator. |
45 |
59 |
46 ## Description |
60 ## Description |
47 |
61 |
48 The functions `cxMalloc()`, `cxCalloc()`, `cxRealloc()`, `cxReallocArray()`, and `cxFree()` |
62 The functions `cxMalloc()`, `cxCalloc()`, `cxRealloc()`, `cxReallocArray()`, and `cxFree()` |
49 invoke the memory management functions specified in the `allocator` and should behave like |
63 invoke the memory management functions specified in the `allocator` and should behave like |