Wed, 31 Aug 2016 14:41:56 +0200
adds appropriate public API
universe@39 | 1 | /* |
universe@39 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@39 | 3 | * |
universe@39 | 4 | * Copyright 2015 Olaf Wintermann. All rights reserved. |
universe@39 | 5 | * |
universe@39 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@39 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@39 | 8 | * |
universe@39 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@39 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@39 | 11 | * |
universe@39 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@39 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@39 | 14 | * documentation and/or other materials provided with the distribution. |
universe@39 | 15 | * |
universe@39 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@39 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@39 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@39 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@39 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@39 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@39 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@39 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@39 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@39 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@39 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@39 | 27 | */ |
universe@39 | 28 | /** |
universe@39 | 29 | * Allocator for custom memory management. |
universe@39 | 30 | * |
universe@39 | 31 | * An UCX allocator consists of a pointer to the memory area / pool and four |
universe@39 | 32 | * function pointers to memory management functions operating on this memory |
universe@39 | 33 | * area / pool. These functions shall behave equivalent to the standard libc |
universe@39 | 34 | * functions <code>malloc(), calloc(), realloc()</code> and <code>free()</code>. |
universe@39 | 35 | * |
universe@39 | 36 | * The signature of the memory management functions is based on the signature |
universe@39 | 37 | * of the respective libc function but each of them takes the pointer to the |
universe@39 | 38 | * memory area / pool as first argument. |
universe@39 | 39 | * |
universe@39 | 40 | * As the pointer to the memory area / pool can be arbitrarily chosen, any data |
universe@39 | 41 | * can be provided to the memory management functions. An UcxMempool is just |
universe@39 | 42 | * one example. |
universe@39 | 43 | * |
universe@39 | 44 | * @see mempool.h |
universe@39 | 45 | * @see UcxMap |
universe@39 | 46 | * |
universe@39 | 47 | * @file allocator.h |
universe@39 | 48 | * @author Mike Becker |
universe@39 | 49 | * @author Olaf Wintermann |
universe@39 | 50 | */ |
universe@39 | 51 | |
universe@39 | 52 | #ifndef UCX_ALLOCATOR_H |
universe@39 | 53 | #define UCX_ALLOCATOR_H |
universe@39 | 54 | |
universe@39 | 55 | #include "ucx.h" |
universe@39 | 56 | |
universe@39 | 57 | #ifdef __cplusplus |
universe@39 | 58 | extern "C" { |
universe@39 | 59 | #endif |
universe@39 | 60 | |
universe@39 | 61 | /** |
universe@39 | 62 | * A function pointer to the allocators <code>malloc()</code> function. |
universe@39 | 63 | * @see UcxAllocator |
universe@39 | 64 | */ |
universe@39 | 65 | typedef void*(*ucx_allocator_malloc)(void *pool, size_t n); |
universe@39 | 66 | |
universe@39 | 67 | /** |
universe@39 | 68 | * A function pointer to the allocators <code>calloc()</code> function. |
universe@39 | 69 | * @see UcxAllocator |
universe@39 | 70 | */ |
universe@39 | 71 | typedef void*(*ucx_allocator_calloc)(void *pool, size_t n, size_t size); |
universe@39 | 72 | |
universe@39 | 73 | /** |
universe@39 | 74 | * A function pointer to the allocators <code>realloc()</code> function. |
universe@39 | 75 | * @see UcxAllocator |
universe@39 | 76 | */ |
universe@39 | 77 | typedef void*(*ucx_allocator_realloc)(void *pool, void *data, size_t n); |
universe@39 | 78 | |
universe@39 | 79 | /** |
universe@39 | 80 | * A function pointer to the allocators <code>free()</code> function. |
universe@39 | 81 | * @see UcxAllocator |
universe@39 | 82 | */ |
universe@39 | 83 | typedef void(*ucx_allocator_free)(void *pool, void *data); |
universe@39 | 84 | |
universe@39 | 85 | /** |
universe@39 | 86 | * UCX allocator data structure containing memory management functions. |
universe@39 | 87 | */ |
universe@39 | 88 | typedef struct { |
universe@39 | 89 | /** Pointer to an area of memory or a complex memory pool. |
universe@39 | 90 | * This pointer will be passed to any memory management function as first |
universe@39 | 91 | * argument. |
universe@39 | 92 | */ |
universe@39 | 93 | void *pool; |
universe@39 | 94 | /** |
universe@39 | 95 | * The <code>malloc()</code> function for this allocator. |
universe@39 | 96 | */ |
universe@39 | 97 | ucx_allocator_malloc malloc; |
universe@39 | 98 | /** |
universe@39 | 99 | * The <code>calloc()</code> function for this allocator. |
universe@39 | 100 | */ |
universe@39 | 101 | ucx_allocator_calloc calloc; |
universe@39 | 102 | /** |
universe@39 | 103 | * The <code>realloc()</code> function for this allocator. |
universe@39 | 104 | */ |
universe@39 | 105 | ucx_allocator_realloc realloc; |
universe@39 | 106 | /** |
universe@39 | 107 | * The <code>free()</code> function for this allocator. |
universe@39 | 108 | */ |
universe@39 | 109 | ucx_allocator_free free; |
universe@39 | 110 | } UcxAllocator; |
universe@39 | 111 | |
universe@39 | 112 | /** |
universe@39 | 113 | * Returns a pointer to the default allocator. |
universe@39 | 114 | * |
universe@39 | 115 | * The default allocator contains wrappers to the standard libc memory |
universe@39 | 116 | * management functions. Use this function to get a pointer to a globally |
universe@39 | 117 | * available allocator. You may also define an own UcxAllocator by assigning |
universe@39 | 118 | * #UCX_ALLOCATOR_DEFAULT to a variable and pass the address of this variable |
universe@39 | 119 | * to any function that takes an UcxAllocator as argument. Note that using |
universe@39 | 120 | * this function is the recommended way of passing a default allocator, thus |
universe@39 | 121 | * it never runs out of scope. |
universe@39 | 122 | * |
universe@39 | 123 | * @return a pointer to the default allocator |
universe@39 | 124 | * |
universe@39 | 125 | * @see UCX_ALLOCATOR_DEFAULT |
universe@39 | 126 | */ |
universe@39 | 127 | UcxAllocator *ucx_default_allocator(); |
universe@39 | 128 | |
universe@39 | 129 | /** |
universe@39 | 130 | * A wrapper for the standard libc <code>malloc()</code> function. |
universe@39 | 131 | * @param ignore ignored (may be used by allocators for pooled memory) |
universe@39 | 132 | * @param n argument passed to <code>malloc()</code> |
universe@39 | 133 | * @return return value of <code>malloc()</code> |
universe@39 | 134 | */ |
universe@39 | 135 | void *ucx_default_malloc(void *ignore, size_t n); |
universe@39 | 136 | /** |
universe@39 | 137 | * A wrapper for the standard libc <code>calloc()</code> function. |
universe@39 | 138 | * @param ignore ignored (may be used by allocators for pooled memory) |
universe@39 | 139 | * @param n argument passed to <code>calloc()</code> |
universe@39 | 140 | * @param size argument passed to <code>calloc()</code> |
universe@39 | 141 | * @return return value of <code>calloc()</code> |
universe@39 | 142 | */ |
universe@39 | 143 | void *ucx_default_calloc(void *ignore, size_t n, size_t size); |
universe@39 | 144 | /** |
universe@39 | 145 | * A wrapper for the standard libc <code>realloc()</code> function. |
universe@39 | 146 | * @param ignore ignored (may be used by allocators for pooled memory) |
universe@39 | 147 | * @param data argumend passed to <code>realloc()</code> |
universe@39 | 148 | * @param n argument passed to <code>realloc()</code> |
universe@39 | 149 | * @return return value of <code>realloc()</code> |
universe@39 | 150 | */ |
universe@39 | 151 | void *ucx_default_realloc(void *ignore, void *data, size_t n); |
universe@39 | 152 | /** |
universe@39 | 153 | * A wrapper for the standard libc <code>free()</code> function. |
universe@39 | 154 | * @param ignore ignored (may be used by allocators for pooled memory) |
universe@39 | 155 | * @param data argument passed to <code>free()</code> |
universe@39 | 156 | */ |
universe@39 | 157 | void ucx_default_free(void *ignore, void *data); |
universe@39 | 158 | |
universe@39 | 159 | /** |
universe@39 | 160 | * Shorthand for calling an allocators malloc function. |
universe@39 | 161 | * @param allocator the allocator to use |
universe@39 | 162 | * @param n size of space to allocate |
universe@39 | 163 | * @return a pointer to the allocated memory area |
universe@39 | 164 | */ |
universe@39 | 165 | #define almalloc(allocator, n) ((allocator)->malloc((allocator)->pool, n)) |
universe@39 | 166 | |
universe@39 | 167 | /** |
universe@39 | 168 | * Shorthand for calling an allocators calloc function. |
universe@39 | 169 | * @param allocator the allocator to use |
universe@39 | 170 | * @param n the count of elements the space should be allocated for |
universe@39 | 171 | * @param size the size of each element |
universe@39 | 172 | * @return a pointer to the allocated memory area |
universe@39 | 173 | */ |
universe@39 | 174 | #define alcalloc(allocator, n, size) \ |
universe@39 | 175 | ((allocator)->calloc((allocator)->pool, n, size)) |
universe@39 | 176 | |
universe@39 | 177 | /** |
universe@39 | 178 | * Shorthand for calling an allocators realloc function. |
universe@39 | 179 | * @param allocator the allocator to use |
universe@39 | 180 | * @param ptr the pointer to the memory area that shall be reallocated |
universe@39 | 181 | * @param n the new size of the allocated memory area |
universe@39 | 182 | * @return a pointer to the reallocated memory area |
universe@39 | 183 | */ |
universe@39 | 184 | #define alrealloc(allocator, ptr, n) \ |
universe@39 | 185 | ((allocator)->realloc((allocator)->pool, ptr, n)) |
universe@39 | 186 | |
universe@39 | 187 | /** |
universe@39 | 188 | * Shorthand for calling an allocators free function. |
universe@39 | 189 | * @param allocator the allocator to use |
universe@39 | 190 | * @param ptr the pointer to the memory area that shall be freed |
universe@39 | 191 | */ |
universe@39 | 192 | #define alfree(allocator, ptr) ((allocator)->free((allocator)->pool, ptr)) |
universe@39 | 193 | |
universe@39 | 194 | /** |
universe@39 | 195 | * Convenient macro for a default allocator <code>struct</code> definition. |
universe@39 | 196 | */ |
universe@39 | 197 | #define UCX_ALLOCATOR_DEFAULT {NULL, \ |
universe@39 | 198 | ucx_default_malloc, ucx_default_calloc, ucx_default_realloc, \ |
universe@39 | 199 | ucx_default_free } |
universe@39 | 200 | |
universe@39 | 201 | #ifdef __cplusplus |
universe@39 | 202 | } |
universe@39 | 203 | #endif |
universe@39 | 204 | |
universe@39 | 205 | #endif /* UCX_ALLOCATOR_H */ |
universe@39 | 206 |