Sun, 03 Oct 2021 12:02:57 +0200
improves usage of attributes
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@391 | 36 | #include <stdlib.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@429 | 47 | * Allocate \p n bytes of memory. |
universe@429 | 48 | * |
universe@429 | 49 | * @param data the allocator's data |
universe@429 | 50 | * @param n the number of bytes |
universe@429 | 51 | * @return a pointer to the allocated memory |
universe@429 | 52 | */ |
universe@405 | 53 | void *(*malloc)(void *data, size_t n); |
universe@429 | 54 | |
universe@429 | 55 | /** |
universe@429 | 56 | * Re-allocate the previously allocated block in \p mem, making the new block \p n bytes long. |
universe@429 | 57 | * This function may return the same pointer that was passed to it, if moving the memory |
universe@429 | 58 | * was not necessary. |
universe@429 | 59 | * |
universe@429 | 60 | * \note Re-allocating a block allocated by a different allocator is undefined. |
universe@429 | 61 | * |
universe@429 | 62 | * @param data the allocator's data |
universe@429 | 63 | * @param mem pointer to the previously allocated block |
universe@429 | 64 | * @param n the new size in bytes |
universe@429 | 65 | * @return a pointer to the re-allocated memory |
universe@429 | 66 | */ |
universe@450 | 67 | void *(*realloc)(void *data, void *mem, size_t n) |
universe@450 | 68 | __attribute__((__warn_unused_result__)); |
universe@429 | 69 | |
universe@429 | 70 | /** |
universe@429 | 71 | * Allocate \p nelem elements of \p n bytes each, all initialized to zero. |
universe@429 | 72 | * |
universe@429 | 73 | * @param data the allocator's data |
universe@429 | 74 | * @param nelem the number of elements |
universe@429 | 75 | * @param n the size of each element in bytes |
universe@429 | 76 | * @return a pointer to the allocated memory |
universe@429 | 77 | */ |
universe@405 | 78 | void *(*calloc)(void *data, size_t nelem, size_t n); |
universe@429 | 79 | |
universe@429 | 80 | /** |
universe@429 | 81 | * Free a block allocated by this allocator. |
universe@429 | 82 | * |
universe@429 | 83 | * \note Freeing a block of a different allocator is undefined. |
universe@429 | 84 | * |
universe@429 | 85 | * @param data the allocator's data |
universe@429 | 86 | * @param mem a pointer to the block to free |
universe@429 | 87 | */ |
universe@450 | 88 | void (*free)(void *data, void *mem) |
universe@450 | 89 | __attribute__((__nonnull__)); |
universe@396 | 90 | } cx_allocator_class; |
universe@396 | 91 | |
universe@429 | 92 | /** |
universe@429 | 93 | * Structure holding the data for an allocator. |
universe@429 | 94 | */ |
universe@394 | 95 | struct cx_allocator_s { |
universe@429 | 96 | /** |
universe@429 | 97 | * A pointer to the instance of the allocator class. |
universe@429 | 98 | */ |
universe@396 | 99 | cx_allocator_class *cl; |
universe@429 | 100 | /** |
universe@429 | 101 | * A pointer to the data this allocator uses. |
universe@429 | 102 | */ |
universe@397 | 103 | void *data; |
universe@391 | 104 | }; |
universe@429 | 105 | |
universe@429 | 106 | /** |
universe@429 | 107 | * High-Level type alias for the allocator type. |
universe@429 | 108 | */ |
universe@397 | 109 | typedef struct cx_allocator_s *CxAllocator; |
universe@391 | 110 | |
universe@429 | 111 | /** |
universe@429 | 112 | * A default allocator using standard library malloc() etc. |
universe@429 | 113 | */ |
universe@391 | 114 | extern CxAllocator cxDefaultAllocator; |
universe@391 | 115 | |
universe@429 | 116 | /** |
universe@429 | 117 | * Allocate \p n bytes of memory. |
universe@429 | 118 | * |
universe@429 | 119 | * @param allocator the allocator |
universe@429 | 120 | * @param n the number of bytes |
universe@429 | 121 | * @return a pointer to the allocated memory |
universe@429 | 122 | */ |
universe@450 | 123 | void *cxMalloc(CxAllocator allocator, size_t n) |
universe@450 | 124 | __attribute__((__malloc__)); |
universe@397 | 125 | |
universe@429 | 126 | /** |
universe@429 | 127 | * Re-allocate the previously allocated block in \p mem, making the new block \p n bytes long. |
universe@429 | 128 | * This function may return the same pointer that was passed to it, if moving the memory |
universe@429 | 129 | * was not necessary. |
universe@429 | 130 | * |
universe@429 | 131 | * \note Re-allocating a block allocated by a different allocator is undefined. |
universe@429 | 132 | * |
universe@429 | 133 | * @param allocator the allocator |
universe@429 | 134 | * @param mem pointer to the previously allocated block |
universe@429 | 135 | * @param n the new size in bytes |
universe@429 | 136 | * @return a pointer to the re-allocated memory |
universe@429 | 137 | */ |
universe@450 | 138 | void *cxRealloc(CxAllocator allocator, void *mem, size_t n) |
universe@450 | 139 | __attribute__((warn_unused_result)); |
universe@397 | 140 | |
universe@429 | 141 | /** |
universe@429 | 142 | * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. |
universe@429 | 143 | * This function acts like cxRealloc() using the pointer pointed to by \p mem. |
universe@429 | 144 | * On success, the pointer is changed to the new location (in case the |
universe@429 | 145 | * |
universe@429 | 146 | * \note Re-allocating a block allocated by a different allocator is undefined. |
universe@429 | 147 | * |
universe@429 | 148 | * \par Error handling |
universe@429 | 149 | * \c errno will be set, if the underlying realloc function does so. |
universe@429 | 150 | * |
universe@429 | 151 | * @param allocator the allocator |
universe@429 | 152 | * @param mem pointer to the pointer to allocated block |
universe@429 | 153 | * @param n the new size in bytes |
universe@429 | 154 | * @return zero on success, non-zero on failure |
universe@429 | 155 | */ |
universe@450 | 156 | int cxReallocate(CxAllocator allocator, void **mem, size_t n) |
universe@450 | 157 | __attribute__((nonnull)); |
universe@414 | 158 | |
universe@429 | 159 | /** |
universe@429 | 160 | * Allocate \p nelem elements of \p n bytes each, all initialized to zero. |
universe@429 | 161 | * |
universe@429 | 162 | * @param allocator the allocator |
universe@429 | 163 | * @param nelem the number of elements |
universe@429 | 164 | * @param n the size of each element in bytes |
universe@429 | 165 | * @return a pointer to the allocated memory |
universe@429 | 166 | */ |
universe@450 | 167 | void *cxCalloc(CxAllocator allocator, size_t nelem, size_t n) |
universe@450 | 168 | __attribute__((malloc)); |
universe@397 | 169 | |
universe@429 | 170 | /** |
universe@429 | 171 | * Free a block allocated by this allocator. |
universe@429 | 172 | * |
universe@429 | 173 | * \note Freeing a block of a different allocator is undefined. |
universe@429 | 174 | * |
universe@429 | 175 | * @param allocator the allocator |
universe@429 | 176 | * @param mem a pointer to the block to free |
universe@429 | 177 | */ |
universe@450 | 178 | void cxFree(CxAllocator allocator, void *mem) |
universe@450 | 179 | __attribute__((nonnull)); |
universe@391 | 180 | |
universe@415 | 181 | #ifdef __cplusplus |
universe@415 | 182 | } /* extern "C" */ |
universe@415 | 183 | #endif |
universe@415 | 184 | |
universe@393 | 185 | #endif /* UCX_ALLOCATOR_H */ |