docs/Writerside/topics/allocator.h.md

changeset 1330
33c95cfc088e
parent 1319
aa1f580f8f59
equal deleted inserted replaced
1329:343eac5ac824 1330:33c95cfc088e
21 ```C 21 ```C
22 #include <cx/allocator.h> 22 #include <cx/allocator.h>
23 23
24 void *cxMalloc(const CxAllocator *allocator, size_t n); 24 void *cxMalloc(const CxAllocator *allocator, size_t n);
25 25
26 void *cxZalloc(const CxAllocator *allocator, size_t n);
27
26 void *cxCalloc(const CxAllocator *allocator, 28 void *cxCalloc(const CxAllocator *allocator,
27 size_t nmemb, size_t size); 29 size_t nmemb, size_t size);
28 30
29 void *cxRealloc(const CxAllocator *allocator, void *mem, size_t n); 31 void *cxRealloc(const CxAllocator *allocator, void *mem, size_t n);
30 32
36 int cxReallocateArray(const CxAllocator *allocator, void **mem, 38 int cxReallocateArray(const CxAllocator *allocator, void **mem,
37 size_t nmemb, size_t size); 39 size_t nmemb, size_t size);
38 40
39 void cxFree(const CxAllocator *allocator, void *mem); 41 void cxFree(const CxAllocator *allocator, void *mem);
40 42
41 int cx_reallocate(void **mem, size_t size); 43 void *cx_zalloc(size_t n);
44
45 int cx_reallocate(void **mem, size_t n);
42 46
43 int cx_reallocatearray(void **mem, size_t nmemb, size_t size); 47 int cx_reallocatearray(void **mem, size_t nmemb, size_t size);
44 48
45 // predefined allocator that uses stdlib functions 49 // predefined allocator that uses stdlib functions
46 CxAllocator * const cxStdlibAllocator; 50 CxAllocator * const cxStdlibAllocator;
48 // default allocator that can be changed 52 // default allocator that can be changed
49 CxAllocator *cxDefaultAllocator = cxStdlibAllocator; 53 CxAllocator *cxDefaultAllocator = cxStdlibAllocator;
50 54
51 // Convenience macros that invokes above functions with the cxDefaultAllocator. 55 // Convenience macros that invokes above functions with the cxDefaultAllocator.
52 #define cxMallocDefault(...) 56 #define cxMallocDefault(...)
57 #define cxZallocDefault(...)
53 #define cxCallocDefault(...) 58 #define cxCallocDefault(...)
54 #define cxReallocDefault(...) 59 #define cxReallocDefault(...)
55 #define cxReallocateDefault(...) 60 #define cxReallocateDefault(...)
56 #define cxReallocateArrayDefault(...) 61 #define cxReallocateArrayDefault(...)
57 #define cxReallocArrayDefault(...) 62 #define cxReallocArrayDefault(...)
72 invoke the memory management functions specified in the `allocator` and should behave like 77 invoke the memory management functions specified in the `allocator` and should behave like
73 their respective stdlibc pendants. 78 their respective stdlibc pendants.
74 Implementations of the allocator interface are strongly encouraged to guarantee this behavior, 79 Implementations of the allocator interface are strongly encouraged to guarantee this behavior,
75 most prominently that invocations of `cxFree()` with a `NULL`-pointer for `mem` are ignored 80 most prominently that invocations of `cxFree()` with a `NULL`-pointer for `mem` are ignored
76 instead of causing segfault error. 81 instead of causing segfault error.
82
83 The functions `cxZalloc()` and `cx_zalloc()` allocate memory and set every allocated byte to zero.
84 The latter is merely a macro for stdlibc `calloc(1,n)`.
77 85
78 Additionally, UCX provides the functions `cxReallocate()` and `cxReallocateArray()`, as well as 86 Additionally, UCX provides the functions `cxReallocate()` and `cxReallocateArray()`, as well as
79 their independent pendants `cx_reallocate()` and `cx_reallocatearray()`. 87 their independent pendants `cx_reallocate()` and `cx_reallocatearray()`.
80 All those functions solve the problem that a possible reallocation might fail, 88 All those functions solve the problem that a possible reallocation might fail,
81 leading to a quite common programming mistake: 89 leading to a quite common programming mistake:

mercurial