Thu, 15 May 2025 16:02:54 +0200
allow changing the cxDefaultAllocator - resolves #669
|
1143
0559812df10c
assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents:
1142
diff
changeset
|
1 | # Allocator |
| 1141 | 2 | |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
3 | The allocator interface provides a mechanism to implement own custom allocators |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
4 | that can also be used in many other function in UCX. |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
5 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
6 | A default allocator implementation using the stdlib functions is |
|
1318
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1210
diff
changeset
|
7 | available via the global symbol `cxStdlibAllocator`, |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
8 | and UCX also provides a [memory pool](mempool.h.md) implementation. |
|
1318
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1210
diff
changeset
|
9 | You are free to add your additional, own custom implementations. |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
10 | A general sketch that illustrates how to do this can be found [below](#custom-allocator). |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
11 | |
|
1318
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1210
diff
changeset
|
12 | ## Default Allocator |
|
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1210
diff
changeset
|
13 | |
|
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1210
diff
changeset
|
14 | The global default allocator which is used by UCX, |
|
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1210
diff
changeset
|
15 | when no specific allocator is specified, |
|
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1210
diff
changeset
|
16 | can be configured via the `cxDefaultAllocator`. |
|
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1210
diff
changeset
|
17 | It is by default set to the `cxStdlibAllocator`. |
|
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1210
diff
changeset
|
18 | |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
19 | ## Overview |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
20 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
21 | ```C |
|
1174
ee473780cc0d
add missing documentation about what header to include
Mike Becker <universe@uap-core.de>
parents:
1171
diff
changeset
|
22 | #include <cx/allocator.h> |
|
ee473780cc0d
add missing documentation about what header to include
Mike Becker <universe@uap-core.de>
parents:
1171
diff
changeset
|
23 | |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
24 | void *cxMalloc(const CxAllocator *allocator, size_t n); |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
25 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
26 | void *cxCalloc(const CxAllocator *allocator, |
|
1210
2ad0cf0f314b
complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents:
1174
diff
changeset
|
27 | size_t nmemb, size_t size); |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
28 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
29 | void *cxRealloc(const CxAllocator *allocator, void *mem, size_t n); |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
30 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
31 | void *cxReallocArray(const CxAllocator *allocator, void *mem, |
|
1210
2ad0cf0f314b
complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents:
1174
diff
changeset
|
32 | size_t nmemb, size_t size); |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
33 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
34 | int cxReallocate(const CxAllocator *allocator, void **mem, size_t n); |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
35 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
36 | int cxReallocateArray(const CxAllocator *allocator, void **mem, |
|
1210
2ad0cf0f314b
complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents:
1174
diff
changeset
|
37 | size_t nmemb, size_t size); |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
38 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
39 | void cxFree(const CxAllocator *allocator, void *mem); |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
40 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
41 | int cx_reallocate(void **mem, size_t size); |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
42 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
43 | int cx_reallocatearray(void **mem, size_t nmemb, size_t size); |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
44 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
45 | // predefined allocator that uses stdlib functions |
|
1318
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1210
diff
changeset
|
46 | CxAllocator * const cxStdlibAllocator; |
|
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1210
diff
changeset
|
47 | |
|
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1210
diff
changeset
|
48 | // default allocator that can be changed |
|
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1210
diff
changeset
|
49 | CxAllocator *cxDefaultAllocator = cxStdlibAllocator; |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
50 | ``` |
|
1146
151c057faf7c
add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
51 | |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
52 | > All UCX functions that are not _explicitly_ designed for taking an allocator argument |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
53 | > (recognizable by a `_a` suffix in the function's name) do support a `NULL` argument |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
54 | > in which case the `cxDefaultAllocator` will be used. |
|
1318
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1210
diff
changeset
|
55 | > |
|
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1210
diff
changeset
|
56 | > You may change the default allocator at any time, but it is strongly recommended to |
|
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1210
diff
changeset
|
57 | > do it only once at program start to avoid accidentally freeing memory that was |
|
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1210
diff
changeset
|
58 | > allocated by a different allocator. |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
59 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
60 | ## Description |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
61 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
62 | The functions `cxMalloc()`, `cxCalloc()`, `cxRealloc()`, `cxReallocArray()`, and `cxFree()` |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
63 | invoke the memory management functions specified in the `allocator` and should behave like |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
64 | their respective stdlibc pendants. |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
65 | Implementations of the allocator interface are strongly encouraged to guarantee this behavior, |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
66 | most prominently that invocations of `cxFree()` with a `NULL`-pointer for `mem` are ignored |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
67 | instead of causing segfault error. |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
68 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
69 | Additionally, UCX provides the functions `cxReallocate()` and `cxReallocateArray()`, as well as |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
70 | their independent pendants `cx_reallocate()` and `cx_reallocatearray()`. |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
71 | All those functions solve the problem that a possible reallocation might fail, |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
72 | leading to a quite common programming mistake: |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
73 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
74 | ```C |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
75 | // common mistake - mem will be lost hen realloc() returns NULL |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
76 | mem = realloc(mem, capacity + 32); |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
77 | if (mem == NULL) // ... do error handling |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
78 | ``` |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
79 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
80 | The above code can be replaced with `cx_reallocate()` which keeps the pointer intact and returns an error code instead. |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
81 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
82 | ```C |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
83 | // when cx_reallocate() fails, mem will still point to the old memory |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
84 | if (cx_reallocate(&mem, capacity + 32)) // ... do error handling |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
85 | ``` |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
86 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
87 | > Please pay special attention to always use `cxFree()` and the `cxRealloc()`-family of functions |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
88 | > with the **same** allocator that was used to allocate the memory. |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
89 | {style="warning"} |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
90 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
91 | ## Custom Allocator |
| 1141 | 92 | |
| 93 | If you want to define your own allocator, you need to initialize the `CxAllocator` structure | |
| 94 | with a pointer to an allocator class (containing function pointers for the memory management | |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
95 | functions) and an optional pointer to custom data. An example is shown below: |
| 1141 | 96 | |
| 97 | ```c | |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
98 | |
| 1141 | 99 | struct my_allocator_state { |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
100 | // ... some internal state ... |
| 1141 | 101 | }; |
| 102 | ||
| 103 | static cx_allocator_class my_allocator_class = { | |
| 104 | my_malloc_impl, | |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
105 | my_realloc_impl, // all these functions are somewhere defined |
| 1141 | 106 | my_calloc_impl, |
| 107 | my_free_impl | |
| 108 | }; | |
| 109 | ||
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
110 | CxAllocator create_my_allocator(void) { |
| 1141 | 111 | CxAllocator alloc; |
| 112 | alloc.cl = &my_allocator_class; | |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
113 | struct my_allocator_state *state = malloc(sizeof(*state)); |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
114 | // ... initialize state ... |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
115 | alloc.data = state; |
| 1141 | 116 | return alloc; |
| 117 | } | |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
118 | |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
119 | void destroy_my_allocator(CxAllocator *al) { |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
120 | struct my_allocator_state *state = al->state; |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
121 | // ... destroy state -- |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
122 | free(state); |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
123 | } |
| 1141 | 124 | ``` |
| 125 | ||
|
1171
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
126 | When you are implementing |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
127 | |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
128 | ## Destructor Functions |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
129 | |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
130 | The `allocator.h` header also declares two function pointers for destructor functions. |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
131 | |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
132 | ```C |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
133 | typedef void (*cx_destructor_func)(void *memory); |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
134 | typedef void (*cx_destructor_func2)(void *data, void *memory); |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
135 | ``` |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
136 | |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
137 | The first one is called _simple_ destructor (e.g. in the context of [collections](collection.h.md)), |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
138 | and the second one is called _advanced_ destructor. |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
139 | The only difference is that you can pass additional custom `data` to an advanced destructor function. |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
140 | |
|
1210
2ad0cf0f314b
complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents:
1174
diff
changeset
|
141 | Destructor functions play a vital role in deep deallocations. |
|
2ad0cf0f314b
complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents:
1174
diff
changeset
|
142 | Another scenarios, besides destroying elements in a collection, are the deallocation of objects |
|
2ad0cf0f314b
complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents:
1174
diff
changeset
|
143 | stored in a [memory pool](mempool.h.md) or deallocations of deeply nested [JSON](json.h.md) objects. |
|
1171
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
144 | |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
145 | > Destructor functions are not to be confused with `free()`-like functions. |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
146 | > The fundamental differences are that |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
147 | > * it is not safe to pass `NULL` to a destructor function |
|
1210
2ad0cf0f314b
complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents:
1174
diff
changeset
|
148 | > * a destructor may only deallocate the contents inside an object but not the object itself, depending on context |
|
1171
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
149 | > |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
150 | {style="note"} |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
151 | |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
152 | > For example, when you are using a [list](list.h.md) that stores elements directly, a destructor function |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
153 | > assigned to that collection may only destroy the element's contents but must not deallocate the element's memory. |
|
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
154 | > On the other hand, when the list is storing just pointers to the elements, you _may_ want the destructor |
|
1210
2ad0cf0f314b
complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents:
1174
diff
changeset
|
155 | > function to also deallocate the element's memory when the element is removed from that list. |
|
1171
155bc3b0dcb3
adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents:
1169
diff
changeset
|
156 | |
|
1169
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
157 | <seealso> |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
158 | <category ref="apidoc"> |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
159 | <a href="https://ucx.sourceforge.io/api/allocator_8h.html">allocator.h</a> |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
160 | </category> |
|
6a33a5648027
add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
161 | </seealso> |