Wed, 31 Dec 2025 16:40:12 +0100
update ucx to version 4.0
| 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 | /** | |
| 39 | * The class definition for an allocator. | |
| 40 | */ | |
| 41 | typedef struct { | |
| 42 | /** | |
| 43 | * The allocator's malloc() implementation. | |
| 44 | */ | |
| 870 | 45 | void *(*malloc)(void *data, size_t n); |
| 174 | 46 | |
| 47 | /** | |
| 48 | * The allocator's realloc() implementation. | |
| 49 | */ | |
| 870 | 50 | void *(*realloc)(void *data, void *mem, size_t n); |
| 174 | 51 | |
| 52 | /** | |
| 53 | * The allocator's calloc() implementation. | |
| 54 | */ | |
| 870 | 55 | void *(*calloc)(void *data, size_t nmemb, size_t size); |
| 174 | 56 | |
| 57 | /** | |
| 58 | * The allocator's free() implementation. | |
| 59 | */ | |
| 870 | 60 | void (*free)(void *data, void *mem); |
| 174 | 61 | } cx_allocator_class; |
| 62 | ||
| 63 | /** | |
| 64 | * Structure holding the data for an allocator. | |
| 65 | */ | |
| 66 | struct cx_allocator_s { | |
| 67 | /** | |
| 68 | * A pointer to the instance of the allocator class. | |
| 69 | */ | |
| 70 | cx_allocator_class *cl; | |
| 71 | /** | |
| 72 | * A pointer to the data this allocator uses. | |
| 73 | */ | |
| 74 | void *data; | |
| 75 | }; | |
| 76 | ||
| 77 | /** | |
| 78 | * High-Level type alias for the allocator type. | |
| 79 | */ | |
| 80 | typedef struct cx_allocator_s CxAllocator; | |
| 81 | ||
| 82 | /** | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
83 | * A pre-defined allocator using standard library malloc() etc. |
| 174 | 84 | */ |
| 870 | 85 | CX_EXPORT extern const CxAllocator * const cxStdlibAllocator; |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
86 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
87 | /** |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
88 | * The default allocator that is used by UCX. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
89 | * Initialized with cxStdlibAllocator, but you may change it. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
90 | */ |
| 870 | 91 | CX_EXPORT extern const CxAllocator * cxDefaultAllocator; |
| 174 | 92 | |
| 93 | /** | |
| 94 | * Function pointer type for destructor functions. | |
| 95 | * | |
| 96 | * A destructor function deallocates possible contents and MAY free the memory | |
| 440 | 97 | * pointed to by @p memory. Read the documentation of the respective function |
| 98 | * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in | |
| 99 | * that particular implementation. | |
| 174 | 100 | * |
| 101 | * @param memory a pointer to the object to destruct | |
| 102 | */ | |
| 324 | 103 | typedef void (*cx_destructor_func)(void *memory); |
| 174 | 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 data an optional pointer to custom data | |
| 114 | * @param memory a pointer to the object to destruct | |
| 115 | */ | |
| 870 | 116 | typedef void (*cx_destructor_func2)(void *data, void *memory); |
| 117 | ||
| 118 | ||
| 119 | /** | |
| 120 | * Function pointer type for clone functions. | |
| 121 | * | |
| 122 | * A clone function is supposed to create a deep copy of the memory pointed to | |
| 123 | * by the @p source pointer. | |
| 124 | * If the @p target pointer is non-null, the clone function is supposed to store | |
| 125 | * the copy into that memory region. | |
| 126 | * Otherwise, the clone function shall use the specified @p allocator to create | |
| 127 | * a new object. | |
| 128 | * | |
| 129 | * The return value of a clone function is always a pointer to the target | |
| 130 | * memory region, or @c NULL if any allocation failed. | |
| 131 | * A clone function SHOULD NOT fail for any other reason than an allocation | |
| 132 | * failure. | |
| 133 | * | |
| 134 | * @param target the target memory or @c NULL, if memory shall be allocated | |
| 135 | * @param source the source memory | |
| 136 | * @param allocator the allocator that shall be used | |
| 137 | * @param data optional additional data | |
| 138 | * @return either the specified @p target, a pointer to the allocated memory, | |
| 139 | * or @c NULL, if any error occurred | |
| 140 | */ | |
| 1016 | 141 | typedef void*(*cx_clone_func)(void *target, const void *source, |
| 870 | 142 | const CxAllocator *allocator, void *data); |
| 174 | 143 | |
| 144 | /** | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
145 | * Returns the system's memory page size. |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
146 | * |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
147 | * If the page size cannot be retrieved from the system, |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
148 | * a default of 4096 bytes is assumed. |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
149 | * |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
150 | * @return the system's memory page size in bytes |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
151 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
152 | CX_EXTERN CX_NODISCARD |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
153 | unsigned long cx_system_page_size(void); |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
154 | |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
155 | /** |
| 1016 | 156 | * Reallocate a previously allocated block. |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
157 | * |
| 1016 | 158 | * Internal function - do not use. |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
159 | * |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
160 | * @param mem pointer to the pointer to allocated block |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
161 | * @param n the new size in bytes |
| 440 | 162 | * @retval zero success |
| 163 | * @retval non-zero failure | |
| 164 | * @see cx_reallocatearray() | |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
165 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
166 | CX_EXTERN CX_NONNULL CX_NODISCARD |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
167 | int cx_reallocate_(void **mem, size_t n); |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
168 | |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
169 | /** |
| 1016 | 170 | * Reallocate a previously allocated block. |
| 440 | 171 | * |
| 1016 | 172 | * Internal function - do not use. |
| 440 | 173 | * |
| 174 | * @param mem pointer to the pointer to allocated block | |
| 175 | * @param nmemb the number of elements | |
| 176 | * @param size the size of each element | |
| 177 | * @retval zero success | |
| 178 | * @retval non-zero failure | |
| 179 | * @see cx_reallocate() | |
| 180 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
181 | CX_EXTERN CX_NONNULL CX_NODISCARD |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
182 | int cx_reallocatearray_(void **mem, size_t nmemb, size_t size); |
| 440 | 183 | |
| 184 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
185 | * Reallocate a previously allocated block and changes the pointer in-place, |
| 440 | 186 | * if necessary. |
| 187 | * | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
188 | * @note This will use stdlib reallocate and @em not the cxDefaultAllocator. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
189 | * |
| 440 | 190 | * @par Error handling |
| 191 | * @c errno will be set by realloc() on failure. | |
| 192 | * | |
| 193 | * @param mem (@c void**) pointer to the pointer to allocated block | |
| 194 | * @param n (@c size_t) the new size in bytes | |
| 195 | * @retval zero success | |
| 196 | * @retval non-zero failure | |
| 197 | * @see cx_reallocatearray() | |
| 198 | */ | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
199 | #define cx_reallocate(mem, n) cx_reallocate_((void**)(mem), n) |
| 440 | 200 | |
| 201 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
202 | * Reallocate a previously allocated block and changes the pointer in-place, |
| 440 | 203 | * if necessary. |
| 204 | * | |
| 205 | * The size is calculated by multiplying @p nemb and @p size. | |
| 206 | * | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
207 | * @note This will use stdlib reallocate and @em not the cxDefaultAllocator. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
208 | * |
| 440 | 209 | * @par Error handling |
| 210 | * @c errno will be set by realloc() on failure or when the multiplication of | |
| 211 | * @p nmemb and @p size overflows. | |
| 212 | * | |
| 213 | * @param mem (@c void**) pointer to the pointer to allocated block | |
| 214 | * @param nmemb (@c size_t) the number of elements | |
| 215 | * @param size (@c size_t) the size of each element | |
| 216 | * @retval zero success | |
| 217 | * @retval non-zero failure | |
| 218 | */ | |
| 219 | #define cx_reallocatearray(mem, nmemb, size) \ | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
220 | cx_reallocatearray_((void**)(mem), nmemb, size) |
| 440 | 221 | |
| 222 | /** | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
223 | * Allocates memory and sets every byte to zero. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
224 | * |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
225 | * @param n (@c size_t) the number of bytes |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
226 | * @return (@c void*) a pointer to the allocated memory |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
227 | */ |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
228 | #define cx_zalloc(n) calloc(1, n) |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
229 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
230 | /** |
| 440 | 231 | * Free a block allocated by this allocator. |
| 232 | * | |
| 233 | * @note Freeing a block of a different allocator is undefined. | |
| 234 | * | |
| 235 | * @param allocator the allocator | |
| 236 | * @param mem a pointer to the block to free | |
| 237 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
238 | CX_EXTERN CX_NONNULL_ARG(1) |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
239 | void cxFree(const CxAllocator *allocator, void *mem); |
| 440 | 240 | |
| 241 | /** | |
| 242 | * Allocate @p n bytes of memory. | |
| 174 | 243 | * |
| 244 | * @param allocator the allocator | |
| 245 | * @param n the number of bytes | |
| 246 | * @return a pointer to the allocated memory | |
| 247 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
248 | CX_EXTERN CX_NODISCARD CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
249 | CX_MALLOC CX_DEALLOC_UCX CX_ALLOCSIZE(2) |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
250 | void *cxMalloc(const CxAllocator *allocator, size_t n); |
| 174 | 251 | |
| 252 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
253 | * Reallocate the previously allocated block in @p mem, making the new block |
| 440 | 254 | * @p n bytes long. |
| 845 | 255 | * This function may return the same pointer passed to it if moving |
| 440 | 256 | * the memory was not necessary. |
| 174 | 257 | * |
| 440 | 258 | * @note Re-allocating a block allocated by a different allocator is undefined. |
| 174 | 259 | * |
| 1016 | 260 | * @attention This function is bug-prone. Consider using cxReallocate(). |
| 261 | * | |
| 174 | 262 | * @param allocator the allocator |
| 263 | * @param mem pointer to the previously allocated block | |
| 264 | * @param n the new size in bytes | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
265 | * @return a pointer to the reallocated memory |
| 174 | 266 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
267 | CX_EXTERN CX_NODISCARD CX_NONNULL_ARG(1) |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
268 | CX_DEALLOC_UCX CX_ALLOCSIZE(3) |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
269 | void *cxRealloc(const CxAllocator *allocator, void *mem, size_t n); |
| 174 | 270 | |
| 271 | /** | |
| 1016 | 272 | * Reallocate the previously allocated block in @p mem. |
| 273 | * | |
| 845 | 274 | * This function may return the same pointer passed to it if moving |
| 440 | 275 | * the memory was not necessary. |
| 276 | * | |
| 277 | * The size is calculated by multiplying @p nemb and @p size. | |
| 845 | 278 | * If that multiplication overflows, this function returns @c NULL, and @c errno |
| 440 | 279 | * will be set. |
| 280 | * | |
| 281 | * @note Re-allocating a block allocated by a different allocator is undefined. | |
| 174 | 282 | * |
| 1016 | 283 | * @attention This function is bug-prone. Consider using cxReallocateArray(). |
| 284 | * | |
| 440 | 285 | * @param allocator the allocator |
| 286 | * @param mem pointer to the previously allocated block | |
| 287 | * @param nmemb the number of elements | |
| 288 | * @param size the size of each element | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
289 | * @return a pointer to the reallocated memory |
| 440 | 290 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
291 | CX_EXTERN CX_NODISCARD CX_NONNULL_ARG(1) |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
292 | CX_DEALLOC_UCX CX_ALLOCSIZE(3, 4) |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
293 | void *cxReallocArray(const CxAllocator *allocator, |
| 870 | 294 | void *mem, size_t nmemb, size_t size); |
| 440 | 295 | |
| 296 | /** | |
| 1016 | 297 | * Reallocate a previously allocated block. |
| 174 | 298 | * |
| 1016 | 299 | * Internal function - do not use. |
| 174 | 300 | * |
| 301 | * @param allocator the allocator | |
| 302 | * @param mem pointer to the pointer to allocated block | |
| 303 | * @param n the new size in bytes | |
| 440 | 304 | * @retval zero success |
| 305 | * @retval non-zero failure | |
| 174 | 306 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
307 | CX_EXTERN CX_NODISCARD CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
308 | int cxReallocate_(const CxAllocator *allocator, void **mem, size_t n); |
| 174 | 309 | |
| 310 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
311 | * Reallocate a previously allocated block and changes the pointer in-place, |
| 440 | 312 | * if necessary. |
| 313 | * This function acts like cxRealloc() using the pointer pointed to by @p mem. | |
| 314 | * | |
| 315 | * @note Re-allocating a block allocated by a different allocator is undefined. | |
| 316 | * | |
| 317 | * @par Error handling | |
| 845 | 318 | * @c errno will be set if the underlying realloc function does so. |
| 440 | 319 | * |
| 320 | * @param allocator (@c CxAllocator*) the allocator | |
| 321 | * @param mem (@c void**) pointer to the pointer to allocated block | |
| 322 | * @param n (@c size_t) the new size in bytes | |
| 323 | * @retval zero success | |
| 324 | * @retval non-zero failure | |
| 325 | */ | |
| 326 | #define cxReallocate(allocator, mem, n) \ | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
327 | cxReallocate_(allocator, (void**)(mem), n) |
| 440 | 328 | |
| 329 | /** | |
| 1016 | 330 | * Reallocate a previously allocated block. |
| 440 | 331 | * |
| 1016 | 332 | * Internal function - do not use. |
| 440 | 333 | * |
| 334 | * @param allocator the allocator | |
| 335 | * @param mem pointer to the pointer to allocated block | |
| 336 | * @param nmemb the number of elements | |
| 337 | * @param size the size of each element | |
| 338 | * @retval zero success | |
| 339 | * @retval non-zero on failure | |
| 340 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
341 | CX_EXTERN CX_NODISCARD CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
342 | int cxReallocateArray_(const CxAllocator *allocator, |
| 870 | 343 | void **mem, size_t nmemb, size_t size); |
| 440 | 344 | |
| 345 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
346 | * Reallocate a previously allocated block and changes the pointer in-place, |
| 440 | 347 | * if necessary. |
| 348 | * This function acts like cxReallocArray() using the pointer pointed to | |
| 349 | * by @p mem. | |
| 350 | * | |
| 351 | * @note Re-allocating a block allocated by a different allocator is undefined. | |
| 352 | * | |
| 353 | * @par Error handling | |
| 354 | * @c errno will be set, if the underlying realloc function does so or the | |
| 355 | * multiplication of @p nmemb and @p size overflows. | |
| 356 | * | |
| 357 | * @param allocator (@c CxAllocator*) the allocator | |
| 358 | * @param mem (@c void**) pointer to the pointer to allocated block | |
| 359 | * @param nmemb (@c size_t) the number of elements | |
| 360 | * @param size (@c size_t) the size of each element | |
| 361 | * @retval zero success | |
| 362 | * @retval non-zero failure | |
| 363 | */ | |
| 364 | #define cxReallocateArray(allocator, mem, nmemb, size) \ | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
365 | cxReallocateArray_(allocator, (void**) (mem), nmemb, size) |
| 440 | 366 | |
| 367 | /** | |
| 1016 | 368 | * Allocate @p nmemb elements of @p size bytes each, all initialized to zero. |
| 174 | 369 | * |
| 370 | * @param allocator the allocator | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
371 | * @param nmemb the number of elements |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
372 | * @param size the size of each element in bytes |
| 174 | 373 | * @return a pointer to the allocated memory |
| 374 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
375 | CX_EXTERN CX_NONNULL_ARG(1) CX_NODISCARD |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
376 | CX_MALLOC CX_DEALLOC_UCX CX_ALLOCSIZE(2, 3) |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
377 | void *cxCalloc(const CxAllocator *allocator, size_t nmemb, size_t size); |
| 174 | 378 | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
379 | /** |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
380 | * Allocate @p n bytes of memory and sets every byte to zero. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
381 | * |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
382 | * @param allocator the allocator |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
383 | * @param n the number of bytes |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
384 | * @return a pointer to the allocated memory |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
385 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
386 | CX_EXTERN CX_NODISCARD CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
387 | CX_MALLOC CX_DEALLOC_UCX CX_ALLOCSIZE(2) |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
388 | void *cxZalloc(const CxAllocator *allocator, size_t n); |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
389 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
390 | /** |
| 1016 | 391 | * Allocate @p n bytes of memory. |
| 392 | * | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
393 | * Convenience macro that invokes cxMalloc() with the cxDefaultAllocator. |
| 1016 | 394 | * |
| 395 | * @param n (@c size_t) the number of bytes | |
| 396 | * @return (@c void*) a pointer to the allocated memory | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
397 | */ |
| 1016 | 398 | #define cxMallocDefault(n) cxMalloc(cxDefaultAllocator, n) |
| 399 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
400 | /** |
| 1016 | 401 | * Allocate @p n bytes of memory and sets every byte to zero. |
| 402 | * | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
403 | * Convenience macro that invokes cxZalloc() with the cxDefaultAllocator. |
| 1016 | 404 | * |
| 405 | * @param n (@c size_t) the number of bytes | |
| 406 | * @return (@c void*) a pointer to the allocated memory | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
407 | */ |
| 1016 | 408 | #define cxZallocDefault(n) cxZalloc(cxDefaultAllocator, n) |
| 409 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
410 | /** |
| 1016 | 411 | * Allocate @p nmemb elements of @p size bytes each, all initialized to zero. |
| 412 | * | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
413 | * Convenience macro that invokes cxCalloc() with the cxDefaultAllocator. |
| 1016 | 414 | * |
| 415 | * @param nmemb (@c size_t) the number of elements | |
| 416 | * @param size (@c size_t) the size of each element in bytes | |
| 417 | * @return (@c void*) a pointer to the allocated memory | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
418 | */ |
| 1016 | 419 | #define cxCallocDefault(nmemb, size) cxCalloc(cxDefaultAllocator, nmemb, size) |
| 420 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
421 | /** |
| 1016 | 422 | * Reallocate the previously allocated block in @p mem. |
| 423 | * | |
| 424 | * This function may return the same pointer passed to it if moving | |
| 425 | * the memory was not necessary. | |
| 426 | * | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
427 | * Convenience macro that invokes cxRealloc() with the cxDefaultAllocator. |
| 1016 | 428 | * |
| 429 | * @attention This function is bug-prone. Consider using cxReallocateDefault(). | |
| 430 | * | |
| 431 | * @param mem (@c void*) pointer to the previously allocated block | |
| 432 | * @param n (@c size_t) the new size in bytes | |
| 433 | * @return (@c void*) a pointer to the reallocated memory | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
434 | */ |
| 1016 | 435 | #define cxReallocDefault(mem, n) cxRealloc(cxDefaultAllocator, mem, n) |
| 436 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
437 | /** |
| 1016 | 438 | * Reallocate a previously allocated block and changes the pointer in-place, |
| 439 | * if necessary. | |
| 440 | * This function acts like cxRealloc() using the pointer pointed to by @p mem. | |
| 441 | * | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
442 | * Convenience macro that invokes cxReallocate() with the cxDefaultAllocator. |
| 1016 | 443 | * |
| 444 | * @note Re-allocating a block allocated by a different allocator is undefined. | |
| 445 | * | |
| 446 | * @par Error handling | |
| 447 | * @c errno will be set if the underlying realloc function does so. | |
| 448 | * | |
| 449 | * @param mem (@c void**) pointer to the pointer to allocated block | |
| 450 | * @param n (@c size_t) the new size in bytes | |
| 451 | * @retval zero success | |
| 452 | * @retval non-zero failure | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
453 | */ |
| 1016 | 454 | #define cxReallocateDefault(mem, n) cxReallocate(cxDefaultAllocator, mem, n) |
| 455 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
456 | /** |
| 1016 | 457 | * Reallocate a previously allocated block and changes the pointer in-place, |
| 458 | * if necessary. | |
| 459 | * This function acts like cxReallocArray() using the pointer pointed to | |
| 460 | * by @p mem. | |
| 461 | * | |
| 462 | * Convenience macro that invokes cxReallocateArray() with the cxDefaultAllocator. | |
| 463 | * | |
| 464 | * @note Re-allocating a block allocated by a different allocator is undefined. | |
| 465 | * | |
| 466 | * @par Error handling | |
| 467 | * @c errno will be set, if the underlying realloc function does so or the | |
| 468 | * multiplication of @p nmemb and @p size overflows. | |
| 469 | * | |
| 470 | * @param mem (@c void**) pointer to the pointer to allocated block | |
| 471 | * @param nmemb (@c size_t) the number of elements | |
| 472 | * @param size (@c size_t) the size of each element | |
| 473 | * @retval zero success | |
| 474 | * @retval non-zero failure | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
475 | */ |
| 1016 | 476 | #define cxReallocateArrayDefault(mem, nmemb, size) \ |
| 477 | cxReallocateArray(cxDefaultAllocator, mem, nmemb, size) | |
| 478 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
479 | /** |
| 1016 | 480 | * Reallocate the previously allocated block in @p mem. |
| 481 | * | |
| 482 | * Convenience macro that invokes cxReallocArray() with the cxDefaultAllocator. | |
| 483 | * | |
| 484 | * This function may return the same pointer passed to it if moving | |
| 485 | * the memory was not necessary. | |
| 486 | * | |
| 487 | * The size is calculated by multiplying @p nemb and @p size. | |
| 488 | * If that multiplication overflows, this function returns @c NULL, and @c errno | |
| 489 | * will be set. | |
| 490 | * | |
| 491 | * @note Re-allocating a block allocated by a different allocator is undefined. | |
| 492 | * | |
| 493 | * @attention This function is bug-prone. Consider using cxReallocateArrayDefault(). | |
| 494 | * | |
| 495 | * @param mem (@c void*) pointer to the previously allocated block | |
| 496 | * @param nmemb (@c size_t) the number of elements | |
| 497 | * @param size (@c size_t) the size of each element | |
| 498 | * @return (@c void*) a pointer to the reallocated memory | |
| 499 | */ | |
| 500 | #define cxReallocArrayDefault(mem, nmemb, size) cxReallocArray(cxDefaultAllocator, mem, nmemb, size) | |
| 501 | ||
| 502 | /** | |
| 503 | * Free a block of memory. | |
| 504 | * | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
505 | * Convenience function that invokes cxFree() with the cxDefaultAllocator. |
| 1016 | 506 | * |
| 507 | * @param mem the memory to deallocate | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
508 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
509 | CX_EXTERN |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
510 | void cxFreeDefault(void *mem); |
| 174 | 511 | |
| 512 | #endif // UCX_ALLOCATOR_H |