| 15 #include <cx/allocator.h> |
15 #include <cx/allocator.h> |
| 16 |
16 |
| 17 void *cxMalloc(const CxAllocator *allocator, size_t n); |
17 void *cxMalloc(const CxAllocator *allocator, size_t n); |
| 18 |
18 |
| 19 void *cxCalloc(const CxAllocator *allocator, |
19 void *cxCalloc(const CxAllocator *allocator, |
| 20 size_t nmemb, size_t size); |
20 size_t nmemb, size_t size); |
| 21 |
21 |
| 22 void *cxRealloc(const CxAllocator *allocator, void *mem, size_t n); |
22 void *cxRealloc(const CxAllocator *allocator, void *mem, size_t n); |
| 23 |
23 |
| 24 void *cxReallocArray(const CxAllocator *allocator, void *mem, |
24 void *cxReallocArray(const CxAllocator *allocator, void *mem, |
| 25 size_t nmemb, size_t size); |
25 size_t nmemb, size_t size); |
| 26 |
26 |
| 27 int cxReallocate(const CxAllocator *allocator, void **mem, size_t n); |
27 int cxReallocate(const CxAllocator *allocator, void **mem, size_t n); |
| 28 |
28 |
| 29 int cxReallocateArray(const CxAllocator *allocator, void **mem, |
29 int cxReallocateArray(const CxAllocator *allocator, void **mem, |
| 30 size_t nmemb, size_t size); |
30 size_t nmemb, size_t size); |
| 31 |
31 |
| 32 void cxFree(const CxAllocator *allocator, void *mem); |
32 void cxFree(const CxAllocator *allocator, void *mem); |
| 33 |
33 |
| 34 int cx_reallocate(void **mem, size_t size); |
34 int cx_reallocate(void **mem, size_t size); |
| 35 |
35 |
| 122 |
122 |
| 123 The first one is called _simple_ destructor (e.g. in the context of [collections](collection.h.md)), |
123 The first one is called _simple_ destructor (e.g. in the context of [collections](collection.h.md)), |
| 124 and the second one is called _advanced_ destructor. |
124 and the second one is called _advanced_ destructor. |
| 125 The only difference is that you can pass additional custom `data` to an advanced destructor function. |
125 The only difference is that you can pass additional custom `data` to an advanced destructor function. |
| 126 |
126 |
| 127 Destructor functions play a vital role in deep de-allocations. |
127 Destructor functions play a vital role in deep deallocations. |
| 128 Another scenarios, besides destroying elements in a collection, are the de-allocation of objects |
128 Another scenarios, besides destroying elements in a collection, are the deallocation of objects |
| 129 stored in a [memory pool](mempool.h.md) or de-allocations of deeply nested [JSON](json.h.md) objects. |
129 stored in a [memory pool](mempool.h.md) or deallocations of deeply nested [JSON](json.h.md) objects. |
| 130 |
130 |
| 131 > Destructor functions are not to be confused with `free()`-like functions. |
131 > Destructor functions are not to be confused with `free()`-like functions. |
| 132 > The fundamental differences are that |
132 > The fundamental differences are that |
| 133 > * it is not safe to pass `NULL` to a destructor function |
133 > * it is not safe to pass `NULL` to a destructor function |
| 134 > * a destructor may only de-allocate the contents inside an object but not the object itself, depending on context |
134 > * a destructor may only deallocate the contents inside an object but not the object itself, depending on context |
| 135 > |
135 > |
| 136 {style="note"} |
136 {style="note"} |
| 137 |
137 |
| 138 > For example, when you are using a [list](list.h.md) that stores elements directly, a destructor function |
138 > For example, when you are using a [list](list.h.md) that stores elements directly, a destructor function |
| 139 > assigned to that collection may only destroy the element's contents but must not deallocate the element's memory. |
139 > assigned to that collection may only destroy the element's contents but must not deallocate the element's memory. |
| 140 > On the other hand, when the list is storing just pointers to the elements, you _may_ want the destructor |
140 > On the other hand, when the list is storing just pointers to the elements, you _may_ want the destructor |
| 141 > function to also de-allocate the element's memory when the element is removed from that list. |
141 > function to also deallocate the element's memory when the element is removed from that list. |
| 142 |
142 |
| 143 <seealso> |
143 <seealso> |
| 144 <category ref="apidoc"> |
144 <category ref="apidoc"> |
| 145 <a href="https://ucx.sourceforge.io/api/allocator_8h.html">allocator.h</a> |
145 <a href="https://ucx.sourceforge.io/api/allocator_8h.html">allocator.h</a> |
| 146 </category> |
146 </category> |