Sun, 05 Jan 2025 22:00:39 +0100
update ucx
174 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | /** | |
440 | 29 | * @file allocator.h |
174 | 30 | * Interface for custom allocators. |
31 | */ | |
32 | ||
33 | #ifndef UCX_ALLOCATOR_H | |
34 | #define UCX_ALLOCATOR_H | |
35 | ||
36 | #include "common.h" | |
37 | ||
38 | #ifdef __cplusplus | |
39 | extern "C" { | |
40 | #endif | |
41 | ||
42 | /** | |
43 | * The class definition for an allocator. | |
44 | */ | |
45 | typedef struct { | |
46 | /** | |
47 | * The allocator's malloc() implementation. | |
48 | */ | |
49 | void *(*malloc)( | |
50 | void *data, | |
51 | size_t n | |
52 | ); | |
53 | ||
54 | /** | |
55 | * The allocator's realloc() implementation. | |
56 | */ | |
57 | void *(*realloc)( | |
58 | void *data, | |
59 | void *mem, | |
60 | size_t n | |
324 | 61 | ); |
174 | 62 | |
63 | /** | |
64 | * The allocator's calloc() implementation. | |
65 | */ | |
66 | void *(*calloc)( | |
67 | void *data, | |
68 | size_t nelem, | |
69 | size_t n | |
70 | ); | |
71 | ||
72 | /** | |
73 | * The allocator's free() implementation. | |
74 | */ | |
75 | void (*free)( | |
76 | void *data, | |
77 | void *mem | |
324 | 78 | ); |
174 | 79 | } cx_allocator_class; |
80 | ||
81 | /** | |
82 | * Structure holding the data for an allocator. | |
83 | */ | |
84 | struct cx_allocator_s { | |
85 | /** | |
86 | * A pointer to the instance of the allocator class. | |
87 | */ | |
88 | cx_allocator_class *cl; | |
89 | /** | |
90 | * A pointer to the data this allocator uses. | |
91 | */ | |
92 | void *data; | |
93 | }; | |
94 | ||
95 | /** | |
96 | * High-Level type alias for the allocator type. | |
97 | */ | |
98 | typedef struct cx_allocator_s CxAllocator; | |
99 | ||
100 | /** | |
101 | * A default allocator using standard library malloc() etc. | |
102 | */ | |
103 | extern CxAllocator *cxDefaultAllocator; | |
104 | ||
105 | /** | |
106 | * Function pointer type for destructor functions. | |
107 | * | |
108 | * A destructor function deallocates possible contents and MAY free the memory | |
440 | 109 | * pointed to by @p memory. Read the documentation of the respective function |
110 | * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in | |
111 | * that particular implementation. | |
174 | 112 | * |
113 | * @param memory a pointer to the object to destruct | |
114 | */ | |
324 | 115 | typedef void (*cx_destructor_func)(void *memory); |
174 | 116 | |
117 | /** | |
118 | * Function pointer type for destructor functions. | |
119 | * | |
120 | * A destructor function deallocates possible contents and MAY free the memory | |
440 | 121 | * pointed to by @p memory. Read the documentation of the respective function |
122 | * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in | |
123 | * that particular implementation. | |
174 | 124 | * |
125 | * @param data an optional pointer to custom data | |
126 | * @param memory a pointer to the object to destruct | |
127 | */ | |
128 | typedef void (*cx_destructor_func2)( | |
129 | void *data, | |
130 | void *memory | |
324 | 131 | ); |
174 | 132 | |
133 | /** | |
440 | 134 | * Re-allocate a previously allocated block and changes the pointer in-place, |
135 | * if necessary. | |
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
136 | * |
440 | 137 | * @par Error handling |
138 | * @c errno will be set by realloc() on failure. | |
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
139 | * |
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
140 | * @param mem pointer to the pointer to allocated block |
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
141 | * @param n the new size in bytes |
440 | 142 | * @retval zero success |
143 | * @retval non-zero failure | |
144 | * @see cx_reallocatearray() | |
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
145 | */ |
440 | 146 | cx_attr_nonnull |
147 | cx_attr_nodiscard | |
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
148 | int cx_reallocate( |
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
149 | void **mem, |
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
150 | size_t n |
324 | 151 | ); |
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
152 | |
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
153 | /** |
440 | 154 | * Re-allocate a previously allocated block and changes the pointer in-place, |
155 | * if necessary. | |
156 | * | |
157 | * The size is calculated by multiplying @p nemb and @p size. | |
158 | * | |
159 | * @par Error handling | |
160 | * @c errno will be set by realloc() on failure or when the multiplication of | |
161 | * @p nmemb and @p size overflows. | |
162 | * | |
163 | * @param mem pointer to the pointer to allocated block | |
164 | * @param nmemb the number of elements | |
165 | * @param size the size of each element | |
166 | * @retval zero success | |
167 | * @retval non-zero failure | |
168 | * @see cx_reallocate() | |
169 | */ | |
170 | cx_attr_nonnull | |
171 | cx_attr_nodiscard | |
172 | int cx_reallocatearray( | |
173 | void **mem, | |
174 | size_t nmemb, | |
175 | size_t size | |
176 | ); | |
177 | ||
178 | /** | |
179 | * Re-allocate a previously allocated block and changes the pointer in-place, | |
180 | * if necessary. | |
181 | * | |
182 | * @par Error handling | |
183 | * @c errno will be set by realloc() on failure. | |
184 | * | |
185 | * @param mem (@c void**) pointer to the pointer to allocated block | |
186 | * @param n (@c size_t) the new size in bytes | |
187 | * @retval zero success | |
188 | * @retval non-zero failure | |
189 | * @see cx_reallocatearray() | |
190 | */ | |
191 | #define cx_reallocate(mem, n) cx_reallocate((void**)(mem), n) | |
192 | ||
193 | /** | |
194 | * Re-allocate a previously allocated block and changes the pointer in-place, | |
195 | * if necessary. | |
196 | * | |
197 | * The size is calculated by multiplying @p nemb and @p size. | |
198 | * | |
199 | * @par Error handling | |
200 | * @c errno will be set by realloc() on failure or when the multiplication of | |
201 | * @p nmemb and @p size overflows. | |
202 | * | |
203 | * @param mem (@c void**) pointer to the pointer to allocated block | |
204 | * @param nmemb (@c size_t) the number of elements | |
205 | * @param size (@c size_t) the size of each element | |
206 | * @retval zero success | |
207 | * @retval non-zero failure | |
208 | */ | |
209 | #define cx_reallocatearray(mem, nmemb, size) \ | |
210 | cx_reallocatearray((void**)(mem), nmemb, size) | |
211 | ||
212 | /** | |
213 | * Free a block allocated by this allocator. | |
214 | * | |
215 | * @note Freeing a block of a different allocator is undefined. | |
216 | * | |
217 | * @param allocator the allocator | |
218 | * @param mem a pointer to the block to free | |
219 | */ | |
220 | cx_attr_nonnull_arg(1) | |
221 | void cxFree( | |
222 | const CxAllocator *allocator, | |
223 | void *mem | |
224 | ); | |
225 | ||
226 | /** | |
227 | * Allocate @p n bytes of memory. | |
174 | 228 | * |
229 | * @param allocator the allocator | |
230 | * @param n the number of bytes | |
231 | * @return a pointer to the allocated memory | |
232 | */ | |
440 | 233 | cx_attr_nodiscard |
234 | cx_attr_nonnull | |
235 | cx_attr_malloc | |
236 | cx_attr_dealloc_ucx | |
237 | cx_attr_allocsize(2) | |
174 | 238 | void *cxMalloc( |
324 | 239 | const CxAllocator *allocator, |
174 | 240 | size_t n |
324 | 241 | ); |
174 | 242 | |
243 | /** | |
440 | 244 | * Re-allocate the previously allocated block in @p mem, making the new block |
245 | * @p n bytes long. | |
246 | * This function may return the same pointer that was passed to it, if moving | |
247 | * the memory was not necessary. | |
174 | 248 | * |
440 | 249 | * @note Re-allocating a block allocated by a different allocator is undefined. |
174 | 250 | * |
251 | * @param allocator the allocator | |
252 | * @param mem pointer to the previously allocated block | |
253 | * @param n the new size in bytes | |
254 | * @return a pointer to the re-allocated memory | |
255 | */ | |
440 | 256 | cx_attr_nodiscard |
257 | cx_attr_nonnull_arg(1) | |
258 | cx_attr_dealloc_ucx | |
259 | cx_attr_allocsize(3) | |
174 | 260 | void *cxRealloc( |
324 | 261 | const CxAllocator *allocator, |
174 | 262 | void *mem, |
263 | size_t n | |
324 | 264 | ); |
174 | 265 | |
266 | /** | |
440 | 267 | * Re-allocate the previously allocated block in @p mem, making the new block |
268 | * @p n bytes long. | |
269 | * This function may return the same pointer that was passed to it, if moving | |
270 | * the memory was not necessary. | |
271 | * | |
272 | * The size is calculated by multiplying @p nemb and @p size. | |
273 | * If that multiplication overflows, this function returns @c NULL and @c errno | |
274 | * will be set. | |
275 | * | |
276 | * @note Re-allocating a block allocated by a different allocator is undefined. | |
174 | 277 | * |
440 | 278 | * @param allocator the allocator |
279 | * @param mem pointer to the previously allocated block | |
280 | * @param nmemb the number of elements | |
281 | * @param size the size of each element | |
282 | * @return a pointer to the re-allocated memory | |
283 | */ | |
284 | cx_attr_nodiscard | |
285 | cx_attr_nonnull_arg(1) | |
286 | cx_attr_dealloc_ucx | |
287 | cx_attr_allocsize(3, 4) | |
288 | void *cxReallocArray( | |
289 | const CxAllocator *allocator, | |
290 | void *mem, | |
291 | size_t nmemb, | |
292 | size_t size | |
293 | ); | |
294 | ||
295 | /** | |
296 | * Re-allocate a previously allocated block and changes the pointer in-place, | |
297 | * if necessary. | |
298 | * This function acts like cxRealloc() using the pointer pointed to by @p mem. | |
174 | 299 | * |
440 | 300 | * @note Re-allocating a block allocated by a different allocator is undefined. |
301 | * | |
302 | * @par Error handling | |
303 | * @c errno will be set, if the underlying realloc function does so. | |
174 | 304 | * |
305 | * @param allocator the allocator | |
306 | * @param mem pointer to the pointer to allocated block | |
307 | * @param n the new size in bytes | |
440 | 308 | * @retval zero success |
309 | * @retval non-zero failure | |
174 | 310 | */ |
440 | 311 | cx_attr_nodiscard |
312 | cx_attr_nonnull | |
174 | 313 | int cxReallocate( |
324 | 314 | const CxAllocator *allocator, |
174 | 315 | void **mem, |
316 | size_t n | |
324 | 317 | ); |
174 | 318 | |
319 | /** | |
440 | 320 | * Re-allocate a previously allocated block and changes the pointer in-place, |
321 | * if necessary. | |
322 | * This function acts like cxRealloc() using the pointer pointed to by @p mem. | |
323 | * | |
324 | * @note Re-allocating a block allocated by a different allocator is undefined. | |
325 | * | |
326 | * @par Error handling | |
327 | * @c errno will be set, if the underlying realloc function does so. | |
328 | * | |
329 | * @param allocator (@c CxAllocator*) the allocator | |
330 | * @param mem (@c void**) pointer to the pointer to allocated block | |
331 | * @param n (@c size_t) the new size in bytes | |
332 | * @retval zero success | |
333 | * @retval non-zero failure | |
334 | */ | |
335 | #define cxReallocate(allocator, mem, n) \ | |
336 | cxReallocate(allocator, (void**)(mem), n) | |
337 | ||
338 | /** | |
339 | * Re-allocate a previously allocated block and changes the pointer in-place, | |
340 | * if necessary. | |
341 | * This function acts like cxReallocArray() using the pointer pointed to | |
342 | * by @p mem. | |
343 | * | |
344 | * @note Re-allocating a block allocated by a different allocator is undefined. | |
345 | * | |
346 | * @par Error handling | |
347 | * @c errno will be set, if the underlying realloc function does so or the | |
348 | * multiplication of @p nmemb and @p size overflows. | |
349 | * | |
350 | * @param allocator the allocator | |
351 | * @param mem pointer to the pointer to allocated block | |
352 | * @param nmemb the number of elements | |
353 | * @param size the size of each element | |
354 | * @retval zero success | |
355 | * @retval non-zero on failure | |
356 | */ | |
357 | cx_attr_nodiscard | |
358 | cx_attr_nonnull | |
359 | int cxReallocateArray( | |
360 | const CxAllocator *allocator, | |
361 | void **mem, | |
362 | size_t nmemb, | |
363 | size_t size | |
364 | ); | |
365 | ||
366 | /** | |
367 | * Re-allocate a previously allocated block and changes the pointer in-place, | |
368 | * if necessary. | |
369 | * This function acts like cxReallocArray() using the pointer pointed to | |
370 | * by @p mem. | |
371 | * | |
372 | * @note Re-allocating a block allocated by a different allocator is undefined. | |
373 | * | |
374 | * @par Error handling | |
375 | * @c errno will be set, if the underlying realloc function does so or the | |
376 | * multiplication of @p nmemb and @p size overflows. | |
377 | * | |
378 | * @param allocator (@c CxAllocator*) the allocator | |
379 | * @param mem (@c void**) pointer to the pointer to allocated block | |
380 | * @param nmemb (@c size_t) the number of elements | |
381 | * @param size (@c size_t) the size of each element | |
382 | * @retval zero success | |
383 | * @retval non-zero failure | |
384 | */ | |
385 | #define cxReallocateArray(allocator, mem, nmemb, size) \ | |
386 | cxReallocateArray(allocator, (void**) (mem), nmemb, size) | |
387 | ||
388 | /** | |
389 | * Allocate @p nelem elements of @p n bytes each, all initialized to zero. | |
174 | 390 | * |
391 | * @param allocator the allocator | |
392 | * @param nelem the number of elements | |
393 | * @param n the size of each element in bytes | |
394 | * @return a pointer to the allocated memory | |
395 | */ | |
440 | 396 | cx_attr_nonnull_arg(1) |
397 | cx_attr_nodiscard | |
398 | cx_attr_malloc | |
399 | cx_attr_dealloc_ucx | |
400 | cx_attr_allocsize(2, 3) | |
174 | 401 | void *cxCalloc( |
324 | 402 | const CxAllocator *allocator, |
174 | 403 | size_t nelem, |
404 | size_t n | |
324 | 405 | ); |
174 | 406 | |
407 | #ifdef __cplusplus | |
408 | } // extern "C" | |
409 | #endif | |
410 | ||
411 | #endif // UCX_ALLOCATOR_H |