docs/Writerside/topics/allocator.h.md

Sun, 23 Nov 2025 13:15:19 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 23 Nov 2025 13:15:19 +0100
changeset 1508
dfc0ddd9571e
parent 1446
5732947cbcc2
permissions
-rw-r--r--

optimize sorted insertion by using the infimum instead of the supremum

The reason is that the supremum returns the equal element with the smallest index, and we want the largest.
Therefore, we use the infimum, which already gives us the largest index when there are equal elements, and increase the index by one. The infimum is also guaranteed to exist in that case.

1143
0559812df10c assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents: 1142
diff changeset
1 # Allocator
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
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
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1420
diff changeset
4 that can also be used in many other functions in UCX.
1169
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
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1420
diff changeset
14 The global default allocator which is used by UCX
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1420
diff changeset
15 when no specific allocator is specified
1318
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
1332
dd5d2402d161 implement zalloc() - resolves #679
Mike Becker <universe@uap-core.de>
parents: 1319
diff changeset
26 void *cxZalloc(const CxAllocator *allocator, size_t n);
dd5d2402d161 implement zalloc() - resolves #679
Mike Becker <universe@uap-core.de>
parents: 1319
diff changeset
27
1169
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
28 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
29 size_t nmemb, size_t size);
1169
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 *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
32
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
33 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
34 size_t nmemb, size_t size);
1169
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 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
37
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
38 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
39 size_t nmemb, size_t size);
1169
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 void cxFree(const CxAllocator *allocator, void *mem);
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
42
1332
dd5d2402d161 implement zalloc() - resolves #679
Mike Becker <universe@uap-core.de>
parents: 1319
diff changeset
43 void *cx_zalloc(size_t n);
dd5d2402d161 implement zalloc() - resolves #679
Mike Becker <universe@uap-core.de>
parents: 1319
diff changeset
44
dd5d2402d161 implement zalloc() - resolves #679
Mike Becker <universe@uap-core.de>
parents: 1319
diff changeset
45 int cx_reallocate(void **mem, size_t n);
1169
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
46
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
47 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
48
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
49 // predefined allocator that uses stdlib functions
1318
12fa1d37fe48 allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents: 1210
diff changeset
50 CxAllocator * const cxStdlibAllocator;
12fa1d37fe48 allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents: 1210
diff changeset
51
12fa1d37fe48 allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents: 1210
diff changeset
52 // default allocator that can be changed
12fa1d37fe48 allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents: 1210
diff changeset
53 CxAllocator *cxDefaultAllocator = cxStdlibAllocator;
1319
aa1f580f8f59 add convenience macros for using the default allocator - relates to #669
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
54
1420
c6f55a2b3495 fix various typos in the web documentation
Mike Becker <universe@uap-core.de>
parents: 1332
diff changeset
55 // convenience macros that invoke the above with cxDefaultAllocator
1319
aa1f580f8f59 add convenience macros for using the default allocator - relates to #669
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
56 #define cxMallocDefault(...)
1332
dd5d2402d161 implement zalloc() - resolves #679
Mike Becker <universe@uap-core.de>
parents: 1319
diff changeset
57 #define cxZallocDefault(...)
1319
aa1f580f8f59 add convenience macros for using the default allocator - relates to #669
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
58 #define cxCallocDefault(...)
aa1f580f8f59 add convenience macros for using the default allocator - relates to #669
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
59 #define cxReallocDefault(...)
aa1f580f8f59 add convenience macros for using the default allocator - relates to #669
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
60 #define cxReallocateDefault(...)
aa1f580f8f59 add convenience macros for using the default allocator - relates to #669
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
61 #define cxReallocateArrayDefault(...)
aa1f580f8f59 add convenience macros for using the default allocator - relates to #669
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
62 #define cxReallocArrayDefault(...)
aa1f580f8f59 add convenience macros for using the default allocator - relates to #669
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
63 #define cxFreeDefault(...)
1169
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
64 ```
1146
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
65
1169
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
66 > 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
67 > (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
68 > 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
69 >
12fa1d37fe48 allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents: 1210
diff changeset
70 > 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
71 > 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
72 > allocated by a different allocator.
1169
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 ## Description
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
75
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
76 The functions `cxMalloc()`, `cxCalloc()`, `cxRealloc()`, `cxReallocArray()`, and `cxFree()`
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
77 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
78 their respective stdlibc pendants.
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
79 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
80 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
81 instead of causing segfault error.
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
82
1332
dd5d2402d161 implement zalloc() - resolves #679
Mike Becker <universe@uap-core.de>
parents: 1319
diff changeset
83 The functions `cxZalloc()` and `cx_zalloc()` allocate memory and set every allocated byte to zero.
dd5d2402d161 implement zalloc() - resolves #679
Mike Becker <universe@uap-core.de>
parents: 1319
diff changeset
84 The latter is merely a macro for stdlibc `calloc(1,n)`.
dd5d2402d161 implement zalloc() - resolves #679
Mike Becker <universe@uap-core.de>
parents: 1319
diff changeset
85
1169
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
86 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
87 their independent pendants `cx_reallocate()` and `cx_reallocatearray()`.
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
88 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
89 leading to a quite common programming mistake:
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 ```C
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
92 // 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
93 mem = realloc(mem, capacity + 32);
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
94 if (mem == NULL) // ... do error handling
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
95 ```
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
96
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
97 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
98
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
99 ```C
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
100 // 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
101 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
102 ```
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
103
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
104 > 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
105 > 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
106 {style="warning"}
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
107
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
108 ## Custom Allocator
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 If you want to define your own allocator, you need to initialize the `CxAllocator` structure
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 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
112 functions) and an optional pointer to custom data. An example is shown below:
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 ```c
1169
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
115
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 struct my_allocator_state {
1169
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
117 // ... some internal state ...
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 };
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 static cx_allocator_class my_allocator_class = {
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 my_malloc_impl,
1169
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
122 my_realloc_impl, // all these functions are somewhere defined
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 my_calloc_impl,
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 my_free_impl
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 };
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126
1169
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
127 CxAllocator create_my_allocator(void) {
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 CxAllocator alloc;
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 alloc.cl = &my_allocator_class;
1169
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
130 struct my_allocator_state *state = malloc(sizeof(*state));
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
131 // ... initialize state ...
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
132 alloc.data = state;
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 return alloc;
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 }
1169
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
135
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
136 void destroy_my_allocator(CxAllocator *al) {
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
137 struct my_allocator_state *state = al->state;
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
138 // ... destroy state --
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
139 free(state);
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
140 }
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 ```
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142
1171
155bc3b0dcb3 adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents: 1169
diff changeset
143 When you are implementing
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
155bc3b0dcb3 adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents: 1169
diff changeset
146
155bc3b0dcb3 adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents: 1169
diff changeset
147 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
148
155bc3b0dcb3 adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents: 1169
diff changeset
149 ```C
1436
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
150 #include <cx/allocator.h>
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
151
1171
155bc3b0dcb3 adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents: 1169
diff changeset
152 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
153 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
154 ```
155bc3b0dcb3 adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents: 1169
diff changeset
155
155bc3b0dcb3 adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents: 1169
diff changeset
156 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
157 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
158 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
159
1210
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1174
diff changeset
160 Destructor functions play a vital role in deep deallocations.
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1420
diff changeset
161 Another scenario, besides destroying elements in a collection, is the deallocation of objects
1210
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1174
diff changeset
162 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
163
155bc3b0dcb3 adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents: 1169
diff changeset
164 > 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
165 > The fundamental differences are that
155bc3b0dcb3 adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents: 1169
diff changeset
166 > * 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
167 > * 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
168 >
155bc3b0dcb3 adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents: 1169
diff changeset
169 {style="note"}
155bc3b0dcb3 adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents: 1169
diff changeset
170
155bc3b0dcb3 adds documentation for destructor functions and collections
Mike Becker <universe@uap-core.de>
parents: 1169
diff changeset
171 > 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
172 > 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
173 > 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
174 > 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
175
1436
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
176 ## Clone Function
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
177
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
178 ```C
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
179 #include <cx/allocator.h>
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
180
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
181 typedef void*(cx_clone_func)(
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
182 void *target, const void *source,
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
183 const CxAllocator *allocator,
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
184 void *data);
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
185 ```
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
186
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
187 A clone function is a callback invoked when a deep copy of an object is supposed to be made.
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
188 If `target` is not `NULL`, memory for the first level was already allocated, and only the deeper levels need to
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
189 be allocated by the clone function.
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
190 If `target` is `NULL`, the clone function must allocate all memory.
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
191 The `source` pointer is never `NULL` and points to the source object.
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
192 The optional `data` pointer can be used to pass additional state.
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
193
1446
5732947cbcc2 default NULL allocator to cxDefaultAllocator
Mike Becker <universe@uap-core.de>
parents: 1440
diff changeset
194 All allocations should be performed by the specified allocator, which is guaranteed to be not `NULL`.
5732947cbcc2 default NULL allocator to cxDefaultAllocator
Mike Becker <universe@uap-core.de>
parents: 1440
diff changeset
195
1436
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
196 A very basic clone function that performs a deep copy of a struct with two strings is shown below.
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
197
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
198 ```C
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
199 #include <cx/string.h>
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
200 #include <cx/allocator.h>
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
201
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
202 struct kv_pair {
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
203 cxmutstr key;
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
204 cxmutstr value;
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
205 };
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
206
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
207 void *clone_kv_pair(
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
208 void *target, const void *source,
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
209 const CxAllocator *allocator,
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
210 [[maybe_unused]] void *data) {
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
211 const struct kv_pair *src = source;
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
212 struct kv_pair *dst;
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
213 if (target == NULL) {
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
214 dst = cxMalloc(allocator, sizeof(struct kv_pair));
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
215 } else {
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
216 dst = target;
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
217 }
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
218 dst->key = cx_strdup_a(allocator, src->key);
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
219 dst->value = cx_strdup_a(allocator, src->value);
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
220 return dst;
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
221 }
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
222 ```
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
223
1440
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1436
diff changeset
224 Clone functions are, for example, used by the functions to clone [lists](list.h.md#clone) or [maps](map.h.md#clone).
1436
c331add0d9f8 add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
225
1169
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
226 <seealso>
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
227 <category ref="apidoc">
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
228 <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
229 </category>
6a33a5648027 add documentation for allocator.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
230 </seealso>

mercurial