ucx/cx/allocator.h

changeset 852
83fdf679df99
parent 775
e5909dff0dbf
equal deleted inserted replaced
850:bbe2925eb590 852:83fdf679df99
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 /** 28 /**
29 * \file allocator.h 29 * @file allocator.h
30 * Interface for custom allocators. 30 * Interface for custom allocators.
31 */ 31 */
32 32
33 #ifndef UCX_ALLOCATOR_H 33 #ifndef UCX_ALLOCATOR_H
34 #define UCX_ALLOCATOR_H 34 #define UCX_ALLOCATOR_H
56 */ 56 */
57 void *(*realloc)( 57 void *(*realloc)(
58 void *data, 58 void *data,
59 void *mem, 59 void *mem,
60 size_t n 60 size_t n
61 ) 61 );
62 __attribute__((__warn_unused_result__));
63 62
64 /** 63 /**
65 * The allocator's calloc() implementation. 64 * The allocator's calloc() implementation.
66 */ 65 */
67 void *(*calloc)( 66 void *(*calloc)(
74 * The allocator's free() implementation. 73 * The allocator's free() implementation.
75 */ 74 */
76 void (*free)( 75 void (*free)(
77 void *data, 76 void *data,
78 void *mem 77 void *mem
79 ) 78 );
80 __attribute__((__nonnull__));
81 } cx_allocator_class; 79 } cx_allocator_class;
82 80
83 /** 81 /**
84 * Structure holding the data for an allocator. 82 * Structure holding the data for an allocator.
85 */ 83 */
106 104
107 /** 105 /**
108 * Function pointer type for destructor functions. 106 * Function pointer type for destructor functions.
109 * 107 *
110 * A destructor function deallocates possible contents and MAY free the memory 108 * A destructor function deallocates possible contents and MAY free the memory
111 * pointed to by \p memory. Read the documentation of the respective function 109 * pointed to by @p memory. Read the documentation of the respective function
112 * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in that 110 * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in
113 * particular implementation. 111 * that particular implementation.
114 * 112 *
115 * @param memory a pointer to the object to destruct 113 * @param memory a pointer to the object to destruct
116 */ 114 */
117 typedef void (*cx_destructor_func)(void *memory) __attribute__((__nonnull__)); 115 typedef void (*cx_destructor_func)(void *memory);
118 116
119 /** 117 /**
120 * Function pointer type for destructor functions. 118 * Function pointer type for destructor functions.
121 * 119 *
122 * A destructor function deallocates possible contents and MAY free the memory 120 * A destructor function deallocates possible contents and MAY free the memory
123 * pointed to by \p memory. Read the documentation of the respective function 121 * pointed to by @p memory. Read the documentation of the respective function
124 * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in that 122 * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in
125 * particular implementation. 123 * that particular implementation.
126 * 124 *
127 * @param data an optional pointer to custom data 125 * @param data an optional pointer to custom data
128 * @param memory a pointer to the object to destruct 126 * @param memory a pointer to the object to destruct
129 */ 127 */
130 typedef void (*cx_destructor_func2)( 128 typedef void (*cx_destructor_func2)(
131 void *data, 129 void *data,
132 void *memory 130 void *memory
133 ) __attribute__((__nonnull__(2))); 131 );
134 132
135 /** 133 /**
136 * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. 134 * Re-allocate a previously allocated block and changes the pointer in-place,
137 * 135 * if necessary.
138 * \par Error handling 136 *
139 * \c errno will be set by realloc() on failure. 137 * @par Error handling
138 * @c errno will be set by realloc() on failure.
140 * 139 *
141 * @param mem pointer to the pointer to allocated block 140 * @param mem pointer to the pointer to allocated block
142 * @param n the new size in bytes 141 * @param n the new size in bytes
143 * @return zero on success, non-zero on failure 142 * @retval zero success
144 */ 143 * @retval non-zero failure
144 * @see cx_reallocatearray()
145 */
146 cx_attr_nonnull
147 cx_attr_nodiscard
145 int cx_reallocate( 148 int cx_reallocate(
146 void **mem, 149 void **mem,
147 size_t n 150 size_t n
148 ) 151 );
149 __attribute__((__nonnull__)); 152
150 153 /**
151 /** 154 * Re-allocate a previously allocated block and changes the pointer in-place,
152 * Allocate \p n bytes of memory. 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.
153 * 228 *
154 * @param allocator the allocator 229 * @param allocator the allocator
155 * @param n the number of bytes 230 * @param n the number of bytes
156 * @return a pointer to the allocated memory 231 * @return a pointer to the allocated memory
157 */ 232 */
233 cx_attr_nodiscard
234 cx_attr_nonnull
235 cx_attr_malloc
236 cx_attr_dealloc_ucx
237 cx_attr_allocsize(2)
158 void *cxMalloc( 238 void *cxMalloc(
159 CxAllocator const *allocator, 239 const CxAllocator *allocator,
160 size_t n 240 size_t n
161 ) 241 );
162 __attribute__((__malloc__)) 242
163 __attribute__((__alloc_size__(2))); 243 /**
164 244 * Re-allocate the previously allocated block in @p mem, making the new block
165 /** 245 * @p n bytes long.
166 * Re-allocate the previously allocated block in \p mem, making the new block \p n bytes long. 246 * This function may return the same pointer that was passed to it, if moving
167 * This function may return the same pointer that was passed to it, if moving the memory 247 * the memory was not necessary.
168 * was not necessary. 248 *
169 * 249 * @note Re-allocating a block allocated by a different allocator is undefined.
170 * \note Re-allocating a block allocated by a different allocator is undefined.
171 * 250 *
172 * @param allocator the allocator 251 * @param allocator the allocator
173 * @param mem pointer to the previously allocated block 252 * @param mem pointer to the previously allocated block
174 * @param n the new size in bytes 253 * @param n the new size in bytes
175 * @return a pointer to the re-allocated memory 254 * @return a pointer to the re-allocated memory
176 */ 255 */
256 cx_attr_nodiscard
257 cx_attr_nonnull_arg(1)
258 cx_attr_dealloc_ucx
259 cx_attr_allocsize(3)
177 void *cxRealloc( 260 void *cxRealloc(
178 CxAllocator const *allocator, 261 const CxAllocator *allocator,
179 void *mem, 262 void *mem,
180 size_t n 263 size_t n
181 ) 264 );
182 __attribute__((__warn_unused_result__)) 265
183 __attribute__((__alloc_size__(3))); 266 /**
184 267 * Re-allocate the previously allocated block in @p mem, making the new block
185 /** 268 * @p n bytes long.
186 * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. 269 * This function may return the same pointer that was passed to it, if moving
187 * This function acts like cxRealloc() using the pointer pointed to by \p mem. 270 * the memory was not necessary.
188 * 271 *
189 * \note Re-allocating a block allocated by a different allocator is undefined. 272 * The size is calculated by multiplying @p nemb and @p size.
190 * 273 * If that multiplication overflows, this function returns @c NULL and @c errno
191 * \par Error handling 274 * will be set.
192 * \c errno will be set, if the underlying realloc function does so. 275 *
276 * @note Re-allocating a block allocated by a different allocator is undefined.
277 *
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.
299 *
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.
193 * 304 *
194 * @param allocator the allocator 305 * @param allocator the allocator
195 * @param mem pointer to the pointer to allocated block 306 * @param mem pointer to the pointer to allocated block
196 * @param n the new size in bytes 307 * @param n the new size in bytes
197 * @return zero on success, non-zero on failure 308 * @retval zero success
198 */ 309 * @retval non-zero failure
310 */
311 cx_attr_nodiscard
312 cx_attr_nonnull
199 int cxReallocate( 313 int cxReallocate(
200 CxAllocator const *allocator, 314 const CxAllocator *allocator,
201 void **mem, 315 void **mem,
202 size_t n 316 size_t n
203 ) 317 );
204 __attribute__((__nonnull__)); 318
205 319 /**
206 /** 320 * Re-allocate a previously allocated block and changes the pointer in-place,
207 * Allocate \p nelem elements of \p n bytes each, all initialized to zero. 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.
208 * 390 *
209 * @param allocator the allocator 391 * @param allocator the allocator
210 * @param nelem the number of elements 392 * @param nelem the number of elements
211 * @param n the size of each element in bytes 393 * @param n the size of each element in bytes
212 * @return a pointer to the allocated memory 394 * @return a pointer to the allocated memory
213 */ 395 */
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)
214 void *cxCalloc( 401 void *cxCalloc(
215 CxAllocator const *allocator, 402 const CxAllocator *allocator,
216 size_t nelem, 403 size_t nelem,
217 size_t n 404 size_t n
218 ) 405 );
219 __attribute__((__malloc__))
220 __attribute__((__alloc_size__(2, 3)));
221
222 /**
223 * Free a block allocated by this allocator.
224 *
225 * \note Freeing a block of a different allocator is undefined.
226 *
227 * @param allocator the allocator
228 * @param mem a pointer to the block to free
229 */
230 void cxFree(
231 CxAllocator const *allocator,
232 void *mem
233 )
234 __attribute__((__nonnull__));
235 406
236 #ifdef __cplusplus 407 #ifdef __cplusplus
237 } // extern "C" 408 } // extern "C"
238 #endif 409 #endif
239 410

mercurial