src/cx/allocator.h

Mon, 18 Apr 2022 17:26:21 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Apr 2022 17:26:21 +0200
changeset 528
4fbfac557df8
parent 526
b070ef465313
child 628
1e2be40f0cb5
permissions
-rw-r--r--

#179 improve API for list content destruction

universe@391 1 /*
universe@391 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@391 3 *
universe@391 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@391 5 *
universe@391 6 * Redistribution and use in source and binary forms, with or without
universe@391 7 * modification, are permitted provided that the following conditions are met:
universe@391 8 *
universe@391 9 * 1. Redistributions of source code must retain the above copyright
universe@391 10 * notice, this list of conditions and the following disclaimer.
universe@391 11 *
universe@391 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@391 13 * notice, this list of conditions and the following disclaimer in the
universe@391 14 * documentation and/or other materials provided with the distribution.
universe@391 15 *
universe@391 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@391 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@391 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@391 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@391 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@391 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@391 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@391 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@391 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@391 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@391 26 * POSSIBILITY OF SUCH DAMAGE.
universe@391 27 */
universe@429 28 /**
universe@429 29 * \file allocator.h
universe@429 30 * Interface for custom allocators.
universe@429 31 */
universe@391 32
universe@391 33 #ifndef UCX_ALLOCATOR_H
universe@391 34 #define UCX_ALLOCATOR_H
universe@391 35
universe@484 36 #include "common.h"
universe@391 37
universe@415 38 #ifdef __cplusplus
universe@415 39 extern "C" {
universe@415 40 #endif
universe@415 41
universe@429 42 /**
universe@429 43 * The class definition for an allocator.
universe@429 44 */
universe@396 45 typedef struct {
universe@429 46 /**
universe@465 47 * The allocator's malloc() implementation.
universe@429 48 */
universe@503 49 void *(*malloc)(
universe@503 50 void *data,
universe@503 51 size_t n
universe@503 52 );
universe@429 53
universe@429 54 /**
universe@465 55 * The allocator's realloc() implementation.
universe@429 56 */
universe@503 57 void *(*realloc)(
universe@503 58 void *data,
universe@503 59 void *mem,
universe@503 60 size_t n
universe@503 61 )
universe@450 62 __attribute__((__warn_unused_result__));
universe@429 63
universe@429 64 /**
universe@465 65 * The allocator's calloc() implementation.
universe@429 66 */
universe@503 67 void *(*calloc)(
universe@503 68 void *data,
universe@503 69 size_t nelem,
universe@503 70 size_t n
universe@503 71 );
universe@429 72
universe@429 73 /**
universe@465 74 * The allocator's free() implementation.
universe@429 75 */
universe@503 76 void (*free)(
universe@503 77 void *data,
universe@503 78 void *mem
universe@503 79 )
universe@450 80 __attribute__((__nonnull__));
universe@396 81 } cx_allocator_class;
universe@396 82
universe@429 83 /**
universe@429 84 * Structure holding the data for an allocator.
universe@429 85 */
universe@394 86 struct cx_allocator_s {
universe@429 87 /**
universe@429 88 * A pointer to the instance of the allocator class.
universe@429 89 */
universe@396 90 cx_allocator_class *cl;
universe@429 91 /**
universe@429 92 * A pointer to the data this allocator uses.
universe@429 93 */
universe@397 94 void *data;
universe@391 95 };
universe@429 96
universe@429 97 /**
universe@429 98 * High-Level type alias for the allocator type.
universe@429 99 */
universe@500 100 typedef struct cx_allocator_s CxAllocator;
universe@391 101
universe@429 102 /**
universe@429 103 * A default allocator using standard library malloc() etc.
universe@429 104 */
universe@500 105 extern CxAllocator *cxDefaultAllocator;
universe@391 106
universe@429 107 /**
universe@503 108 * Function pointer type for destructor functions.
universe@503 109 *
universe@503 110 * A destructor function deallocates possible contents and MAY free the memory
universe@526 111 * pointed to by \p memory. Read the documentation of the respective function
universe@526 112 * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in that
universe@526 113 * particular implementation.
universe@503 114 *
universe@503 115 * @param memory a pointer to the object to destruct
universe@503 116 */
universe@526 117 typedef void (*cx_destructor_func)(void *memory) __attribute__((__nonnull__));
universe@503 118
universe@503 119 /**
universe@528 120 * Function pointer type for destructor functions.
universe@528 121 *
universe@528 122 * A destructor function deallocates possible contents and MAY free the memory
universe@528 123 * pointed to by \p memory. Read the documentation of the respective function
universe@528 124 * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in that
universe@528 125 * particular implementation.
universe@528 126 *
universe@528 127 * @param data an optional pointer to custom data
universe@528 128 * @param memory a pointer to the object to destruct
universe@528 129 */
universe@528 130 typedef void (*cx_destructor_func2)(
universe@528 131 void *data,
universe@528 132 void *memory
universe@528 133 ) __attribute__((__nonnull__(2)));
universe@528 134
universe@528 135 /**
universe@528 136 * Structure holding an advanced destructor function and the desired payload.
universe@528 137 * Invocations of func should use data as first argument.
universe@528 138 */
universe@528 139 typedef struct {
universe@528 140 /**
universe@528 141 * A pointer to the data that SHALL be used to invoke func.
universe@528 142 */
universe@528 143 void *data;
universe@528 144 /**
universe@528 145 * A pointer to the function to invoke.
universe@528 146 */
universe@528 147 cx_destructor_func2 func;
universe@528 148 } cx_advanced_destructor;
universe@528 149
universe@528 150 /**
universe@528 151 * Specifies the type of destructor to use.
universe@528 152 */
universe@528 153 enum cx_destructor_type {
universe@528 154 /**
universe@528 155 * Do not use a destructor function.
universe@528 156 */
universe@528 157 CX_DESTRUCTOR_NONE,
universe@528 158 /**
universe@528 159 * Use a simple destructor.
universe@528 160 * @see cx_destructor_func
universe@528 161 */
universe@528 162 CX_DESTRUCTOR_SIMPLE,
universe@528 163 /**
universe@528 164 * Use an advanced destructor.
universe@528 165 * @see cx_advanced_destructor
universe@528 166 */
universe@528 167 CX_DESTRUCTOR_ADVANCED
universe@528 168 };
universe@528 169
universe@528 170 /**
universe@429 171 * Allocate \p n bytes of memory.
universe@429 172 *
universe@429 173 * @param allocator the allocator
universe@429 174 * @param n the number of bytes
universe@429 175 * @return a pointer to the allocated memory
universe@429 176 */
universe@500 177 void *cxMalloc(
universe@508 178 CxAllocator const *allocator,
universe@500 179 size_t n
universe@500 180 )
universe@461 181 __attribute__((__malloc__))
universe@461 182 __attribute__((__alloc_size__(2)));
universe@397 183
universe@429 184 /**
universe@429 185 * Re-allocate the previously allocated block in \p mem, making the new block \p n bytes long.
universe@429 186 * This function may return the same pointer that was passed to it, if moving the memory
universe@429 187 * was not necessary.
universe@429 188 *
universe@429 189 * \note Re-allocating a block allocated by a different allocator is undefined.
universe@429 190 *
universe@429 191 * @param allocator the allocator
universe@429 192 * @param mem pointer to the previously allocated block
universe@429 193 * @param n the new size in bytes
universe@429 194 * @return a pointer to the re-allocated memory
universe@429 195 */
universe@500 196 void *cxRealloc(
universe@508 197 CxAllocator const *allocator,
universe@500 198 void *mem,
universe@500 199 size_t n
universe@500 200 )
universe@461 201 __attribute__((__warn_unused_result__))
universe@461 202 __attribute__((__alloc_size__(3)));
universe@397 203
universe@429 204 /**
universe@429 205 * Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
universe@429 206 * This function acts like cxRealloc() using the pointer pointed to by \p mem.
universe@429 207 * On success, the pointer is changed to the new location (in case the
universe@429 208 *
universe@429 209 * \note Re-allocating a block allocated by a different allocator is undefined.
universe@429 210 *
universe@429 211 * \par Error handling
universe@429 212 * \c errno will be set, if the underlying realloc function does so.
universe@429 213 *
universe@429 214 * @param allocator the allocator
universe@429 215 * @param mem pointer to the pointer to allocated block
universe@429 216 * @param n the new size in bytes
universe@429 217 * @return zero on success, non-zero on failure
universe@429 218 */
universe@500 219 int cxReallocate(
universe@508 220 CxAllocator const *allocator,
universe@500 221 void **mem,
universe@500 222 size_t n
universe@500 223 )
universe@452 224 __attribute__((__nonnull__));
universe@414 225
universe@429 226 /**
universe@429 227 * Allocate \p nelem elements of \p n bytes each, all initialized to zero.
universe@429 228 *
universe@429 229 * @param allocator the allocator
universe@429 230 * @param nelem the number of elements
universe@429 231 * @param n the size of each element in bytes
universe@429 232 * @return a pointer to the allocated memory
universe@429 233 */
universe@500 234 void *cxCalloc(
universe@508 235 CxAllocator const *allocator,
universe@500 236 size_t nelem,
universe@500 237 size_t n
universe@500 238 )
universe@461 239 __attribute__((__malloc__))
universe@461 240 __attribute__((__alloc_size__(2, 3)));
universe@397 241
universe@429 242 /**
universe@429 243 * Free a block allocated by this allocator.
universe@429 244 *
universe@429 245 * \note Freeing a block of a different allocator is undefined.
universe@429 246 *
universe@429 247 * @param allocator the allocator
universe@429 248 * @param mem a pointer to the block to free
universe@429 249 */
universe@500 250 void cxFree(
universe@508 251 CxAllocator const *allocator,
universe@500 252 void *mem
universe@500 253 )
universe@452 254 __attribute__((__nonnull__));
universe@391 255
universe@415 256 #ifdef __cplusplus
universe@415 257 } /* extern "C" */
universe@415 258 #endif
universe@415 259
universe@393 260 #endif /* UCX_ALLOCATOR_H */

mercurial