Fri, 20 Jun 2025 12:01:08 +0200
add list initializer
| 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, | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
68 | size_t nmemb, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
69 | size_t size |
| 174 | 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 | /** | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
101 | * A pre-defined allocator using standard library malloc() etc. |
| 174 | 102 | */ |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
103 | cx_attr_export |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
104 | extern const CxAllocator * const cxStdlibAllocator; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
105 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
106 | /** |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
107 | * The default allocator that is used by UCX. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
108 | * Initialized with cxStdlibAllocator, but you may change it. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
109 | */ |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
110 | cx_attr_export |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
111 | extern const CxAllocator * cxDefaultAllocator; |
| 174 | 112 | |
| 113 | /** | |
| 114 | * Function pointer type for destructor functions. | |
| 115 | * | |
| 116 | * A destructor function deallocates possible contents and MAY free the memory | |
| 440 | 117 | * pointed to by @p memory. Read the documentation of the respective function |
| 118 | * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in | |
| 119 | * that particular implementation. | |
| 174 | 120 | * |
| 121 | * @param memory a pointer to the object to destruct | |
| 122 | */ | |
| 324 | 123 | typedef void (*cx_destructor_func)(void *memory); |
| 174 | 124 | |
| 125 | /** | |
| 126 | * Function pointer type for destructor functions. | |
| 127 | * | |
| 128 | * A destructor function deallocates possible contents and MAY free the memory | |
| 440 | 129 | * pointed to by @p memory. Read the documentation of the respective function |
| 130 | * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in | |
| 131 | * that particular implementation. | |
| 174 | 132 | * |
| 133 | * @param data an optional pointer to custom data | |
| 134 | * @param memory a pointer to the object to destruct | |
| 135 | */ | |
| 136 | typedef void (*cx_destructor_func2)( | |
| 137 | void *data, | |
| 138 | void *memory | |
| 324 | 139 | ); |
| 174 | 140 | |
| 141 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
142 | * Reallocate a previously allocated block and changes the pointer in-place, |
| 440 | 143 | * if necessary. |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
144 | * |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
145 | * @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
|
146 | * |
| 440 | 147 | * @par Error handling |
| 148 | * @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
|
149 | * |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
150 | * @param mem pointer to the pointer to allocated block |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
151 | * @param n the new size in bytes |
| 440 | 152 | * @retval zero success |
| 153 | * @retval non-zero failure | |
| 154 | * @see cx_reallocatearray() | |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
155 | */ |
| 440 | 156 | cx_attr_nonnull |
| 157 | cx_attr_nodiscard | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
158 | cx_attr_export |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
159 | int cx_reallocate_( |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
160 | void **mem, |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
161 | size_t n |
| 324 | 162 | ); |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
163 | |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
164 | /** |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
165 | * Reallocate a previously allocated block and changes the pointer in-place, |
| 440 | 166 | * if necessary. |
| 167 | * | |
| 168 | * The size is calculated by multiplying @p nemb and @p size. | |
| 169 | * | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
170 | * @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
|
171 | * |
| 440 | 172 | * @par Error handling |
| 173 | * @c errno will be set by realloc() on failure or when the multiplication of | |
| 174 | * @p nmemb and @p size overflows. | |
| 175 | * | |
| 176 | * @param mem pointer to the pointer to allocated block | |
| 177 | * @param nmemb the number of elements | |
| 178 | * @param size the size of each element | |
| 179 | * @retval zero success | |
| 180 | * @retval non-zero failure | |
| 181 | * @see cx_reallocate() | |
| 182 | */ | |
| 183 | cx_attr_nonnull | |
| 184 | cx_attr_nodiscard | |
|
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 | cx_attr_export |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
186 | int cx_reallocatearray_( |
| 440 | 187 | void **mem, |
| 188 | size_t nmemb, | |
| 189 | size_t size | |
| 190 | ); | |
| 191 | ||
| 192 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
193 | * Reallocate a previously allocated block and changes the pointer in-place, |
| 440 | 194 | * if necessary. |
| 195 | * | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
196 | * @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
|
197 | * |
| 440 | 198 | * @par Error handling |
| 199 | * @c errno will be set by realloc() on failure. | |
| 200 | * | |
| 201 | * @param mem (@c void**) pointer to the pointer to allocated block | |
| 202 | * @param n (@c size_t) the new size in bytes | |
| 203 | * @retval zero success | |
| 204 | * @retval non-zero failure | |
| 205 | * @see cx_reallocatearray() | |
| 206 | */ | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
207 | #define cx_reallocate(mem, n) cx_reallocate_((void**)(mem), n) |
| 440 | 208 | |
| 209 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
210 | * Reallocate a previously allocated block and changes the pointer in-place, |
| 440 | 211 | * if necessary. |
| 212 | * | |
| 213 | * The size is calculated by multiplying @p nemb and @p size. | |
| 214 | * | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
215 | * @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
|
216 | * |
| 440 | 217 | * @par Error handling |
| 218 | * @c errno will be set by realloc() on failure or when the multiplication of | |
| 219 | * @p nmemb and @p size overflows. | |
| 220 | * | |
| 221 | * @param mem (@c void**) pointer to the pointer to allocated block | |
| 222 | * @param nmemb (@c size_t) the number of elements | |
| 223 | * @param size (@c size_t) the size of each element | |
| 224 | * @retval zero success | |
| 225 | * @retval non-zero failure | |
| 226 | */ | |
| 227 | #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
|
228 | cx_reallocatearray_((void**)(mem), nmemb, size) |
| 440 | 229 | |
| 230 | /** | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
231 | * Allocates memory and sets every byte to zero. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
232 | * |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
233 | * @param n (@c size_t) the number of bytes |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
234 | * @return (@c void*) a pointer to the allocated memory |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
235 | */ |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
236 | #define cx_zalloc(n) calloc(1, n) |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
237 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
238 | /** |
| 440 | 239 | * Free a block allocated by this allocator. |
| 240 | * | |
| 241 | * @note Freeing a block of a different allocator is undefined. | |
| 242 | * | |
| 243 | * @param allocator the allocator | |
| 244 | * @param mem a pointer to the block to free | |
| 245 | */ | |
| 246 | cx_attr_nonnull_arg(1) | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
247 | cx_attr_export |
| 440 | 248 | void cxFree( |
| 249 | const CxAllocator *allocator, | |
| 250 | void *mem | |
| 251 | ); | |
| 252 | ||
| 253 | /** | |
| 254 | * Allocate @p n bytes of memory. | |
| 174 | 255 | * |
| 256 | * @param allocator the allocator | |
| 257 | * @param n the number of bytes | |
| 258 | * @return a pointer to the allocated memory | |
| 259 | */ | |
| 440 | 260 | cx_attr_nodiscard |
| 261 | cx_attr_nonnull | |
| 262 | cx_attr_malloc | |
| 263 | cx_attr_dealloc_ucx | |
| 264 | cx_attr_allocsize(2) | |
|
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 | cx_attr_export |
| 174 | 266 | void *cxMalloc( |
| 324 | 267 | const CxAllocator *allocator, |
| 174 | 268 | size_t n |
| 324 | 269 | ); |
| 174 | 270 | |
| 271 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
272 | * Reallocate the previously allocated block in @p mem, making the new block |
| 440 | 273 | * @p n bytes long. |
| 274 | * This function may return the same pointer that was passed to it, if moving | |
| 275 | * the memory was not necessary. | |
| 174 | 276 | * |
| 440 | 277 | * @note Re-allocating a block allocated by a different allocator is undefined. |
| 174 | 278 | * |
| 279 | * @param allocator the allocator | |
| 280 | * @param mem pointer to the previously allocated block | |
| 281 | * @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
|
282 | * @return a pointer to the reallocated memory |
| 174 | 283 | */ |
| 440 | 284 | cx_attr_nodiscard |
| 285 | cx_attr_nonnull_arg(1) | |
| 286 | cx_attr_dealloc_ucx | |
| 287 | cx_attr_allocsize(3) | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
288 | cx_attr_export |
| 174 | 289 | void *cxRealloc( |
| 324 | 290 | const CxAllocator *allocator, |
| 174 | 291 | void *mem, |
| 292 | size_t n | |
| 324 | 293 | ); |
| 174 | 294 | |
| 295 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
296 | * Reallocate the previously allocated block in @p mem, making the new block |
| 440 | 297 | * @p n bytes long. |
| 298 | * This function may return the same pointer that was passed to it, if moving | |
| 299 | * the memory was not necessary. | |
| 300 | * | |
| 301 | * The size is calculated by multiplying @p nemb and @p size. | |
| 302 | * If that multiplication overflows, this function returns @c NULL and @c errno | |
| 303 | * will be set. | |
| 304 | * | |
| 305 | * @note Re-allocating a block allocated by a different allocator is undefined. | |
| 174 | 306 | * |
| 440 | 307 | * @param allocator the allocator |
| 308 | * @param mem pointer to the previously allocated block | |
| 309 | * @param nmemb the number of elements | |
| 310 | * @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
|
311 | * @return a pointer to the reallocated memory |
| 440 | 312 | */ |
| 313 | cx_attr_nodiscard | |
| 314 | cx_attr_nonnull_arg(1) | |
| 315 | cx_attr_dealloc_ucx | |
| 316 | cx_attr_allocsize(3, 4) | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
317 | cx_attr_export |
| 440 | 318 | void *cxReallocArray( |
| 319 | const CxAllocator *allocator, | |
| 320 | void *mem, | |
| 321 | size_t nmemb, | |
| 322 | size_t size | |
| 323 | ); | |
| 324 | ||
| 325 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
326 | * Reallocate a previously allocated block and changes the pointer in-place, |
| 440 | 327 | * if necessary. |
| 328 | * This function acts like cxRealloc() using the pointer pointed to by @p mem. | |
| 174 | 329 | * |
| 440 | 330 | * @note Re-allocating a block allocated by a different allocator is undefined. |
| 331 | * | |
| 332 | * @par Error handling | |
| 333 | * @c errno will be set, if the underlying realloc function does so. | |
| 174 | 334 | * |
| 335 | * @param allocator the allocator | |
| 336 | * @param mem pointer to the pointer to allocated block | |
| 337 | * @param n the new size in bytes | |
| 440 | 338 | * @retval zero success |
| 339 | * @retval non-zero failure | |
| 174 | 340 | */ |
| 440 | 341 | cx_attr_nodiscard |
| 342 | cx_attr_nonnull | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
343 | cx_attr_export |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
344 | int cxReallocate_( |
| 324 | 345 | const CxAllocator *allocator, |
| 174 | 346 | void **mem, |
| 347 | size_t n | |
| 324 | 348 | ); |
| 174 | 349 | |
| 350 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
351 | * Reallocate a previously allocated block and changes the pointer in-place, |
| 440 | 352 | * if necessary. |
| 353 | * This function acts like cxRealloc() using the pointer pointed to by @p mem. | |
| 354 | * | |
| 355 | * @note Re-allocating a block allocated by a different allocator is undefined. | |
| 356 | * | |
| 357 | * @par Error handling | |
| 358 | * @c errno will be set, if the underlying realloc function does so. | |
| 359 | * | |
| 360 | * @param allocator (@c CxAllocator*) the allocator | |
| 361 | * @param mem (@c void**) pointer to the pointer to allocated block | |
| 362 | * @param n (@c size_t) the new size in bytes | |
| 363 | * @retval zero success | |
| 364 | * @retval non-zero failure | |
| 365 | */ | |
| 366 | #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
|
367 | cxReallocate_(allocator, (void**)(mem), n) |
| 440 | 368 | |
| 369 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
370 | * Reallocate a previously allocated block and changes the pointer in-place, |
| 440 | 371 | * if necessary. |
| 372 | * This function acts like cxReallocArray() using the pointer pointed to | |
| 373 | * by @p mem. | |
| 374 | * | |
| 375 | * @note Re-allocating a block allocated by a different allocator is undefined. | |
| 376 | * | |
| 377 | * @par Error handling | |
| 378 | * @c errno will be set, if the underlying realloc function does so or the | |
| 379 | * multiplication of @p nmemb and @p size overflows. | |
| 380 | * | |
| 381 | * @param allocator the allocator | |
| 382 | * @param mem pointer to the pointer to allocated block | |
| 383 | * @param nmemb the number of elements | |
| 384 | * @param size the size of each element | |
| 385 | * @retval zero success | |
| 386 | * @retval non-zero on failure | |
| 387 | */ | |
| 388 | cx_attr_nodiscard | |
| 389 | cx_attr_nonnull | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
390 | cx_attr_export |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
391 | int cxReallocateArray_( |
| 440 | 392 | const CxAllocator *allocator, |
| 393 | void **mem, | |
| 394 | size_t nmemb, | |
| 395 | size_t size | |
| 396 | ); | |
| 397 | ||
| 398 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
399 | * Reallocate a previously allocated block and changes the pointer in-place, |
| 440 | 400 | * if necessary. |
| 401 | * This function acts like cxReallocArray() using the pointer pointed to | |
| 402 | * by @p mem. | |
| 403 | * | |
| 404 | * @note Re-allocating a block allocated by a different allocator is undefined. | |
| 405 | * | |
| 406 | * @par Error handling | |
| 407 | * @c errno will be set, if the underlying realloc function does so or the | |
| 408 | * multiplication of @p nmemb and @p size overflows. | |
| 409 | * | |
| 410 | * @param allocator (@c CxAllocator*) the allocator | |
| 411 | * @param mem (@c void**) pointer to the pointer to allocated block | |
| 412 | * @param nmemb (@c size_t) the number of elements | |
| 413 | * @param size (@c size_t) the size of each element | |
| 414 | * @retval zero success | |
| 415 | * @retval non-zero failure | |
| 416 | */ | |
| 417 | #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
|
418 | cxReallocateArray_(allocator, (void**) (mem), nmemb, size) |
| 440 | 419 | |
| 420 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
421 | * Allocate @p nmemb elements of @p n bytes each, all initialized to zero. |
| 174 | 422 | * |
| 423 | * @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
|
424 | * @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
|
425 | * @param size the size of each element in bytes |
| 174 | 426 | * @return a pointer to the allocated memory |
| 427 | */ | |
| 440 | 428 | cx_attr_nonnull_arg(1) |
| 429 | cx_attr_nodiscard | |
| 430 | cx_attr_malloc | |
| 431 | cx_attr_dealloc_ucx | |
| 432 | cx_attr_allocsize(2, 3) | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
433 | cx_attr_export |
| 174 | 434 | void *cxCalloc( |
| 324 | 435 | const CxAllocator *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
|
436 | size_t nmemb, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
437 | size_t size |
| 324 | 438 | ); |
| 174 | 439 | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
440 | /** |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
441 | * 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
|
442 | * |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
443 | * @param allocator the allocator |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
444 | * @param n the number of bytes |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
445 | * @return a pointer to the allocated memory |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
446 | */ |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
447 | cx_attr_nodiscard |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
448 | cx_attr_nonnull |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
449 | cx_attr_malloc |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
450 | cx_attr_dealloc_ucx |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
451 | cx_attr_allocsize(2) |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
452 | cx_attr_export |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
453 | void *cxZalloc( |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
454 | const CxAllocator *allocator, |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
455 | size_t n |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
456 | ); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
457 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
458 | /** |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
459 | * Convenience macro that invokes cxMalloc() with the cxDefaultAllocator. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
460 | */ |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
461 | #define cxMallocDefault(...) cxMalloc(cxDefaultAllocator, __VA_ARGS__) |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
462 | /** |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
463 | * Convenience macro that invokes cxZalloc() with the cxDefaultAllocator. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
464 | */ |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
465 | #define cxZallocDefault(...) cxZalloc(cxDefaultAllocator, __VA_ARGS__) |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
466 | /** |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
467 | * Convenience macro that invokes cxCalloc() with the cxDefaultAllocator. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
468 | */ |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
469 | #define cxCallocDefault(...) cxCalloc(cxDefaultAllocator, __VA_ARGS__) |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
470 | /** |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
471 | * Convenience macro that invokes cxRealloc() with the cxDefaultAllocator. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
472 | */ |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
473 | #define cxReallocDefault(...) cxRealloc(cxDefaultAllocator, __VA_ARGS__) |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
474 | /** |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
475 | * Convenience macro that invokes cxReallocate() with the cxDefaultAllocator. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
476 | */ |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
477 | #define cxReallocateDefault(...) cxReallocate(cxDefaultAllocator, __VA_ARGS__) |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
478 | /** |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
479 | * Convenience macro that invokes cxReallocateArray() with the cxDefaultAllocator. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
480 | */ |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
481 | #define cxReallocateArrayDefault(...) cxReallocateArray(cxDefaultAllocator, __VA_ARGS__) |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
482 | /** |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
483 | * Convenience macro that invokes cxReallocArray() with the cxDefaultAllocator. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
484 | */ |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
485 | #define cxReallocArrayDefault(...) cxReallocArray(cxDefaultAllocator, __VA_ARGS__) |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
486 | /** |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
487 | * Convenience macro that invokes cxFree() with the cxDefaultAllocator. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
488 | */ |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
489 | #define cxFreeDefault(...) cxFree(cxDefaultAllocator, __VA_ARGS__) |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
490 | |
| 174 | 491 | #ifdef __cplusplus |
| 492 | } // extern "C" | |
| 493 | #endif | |
| 494 | ||
| 495 | #endif // UCX_ALLOCATOR_H |