Fri, 09 Aug 2013 14:36:54 +0200
documented mempool + some fixes
universe@103 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
olaf@13 | 3 | * |
universe@103 | 4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
universe@103 | 5 | * |
universe@103 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@103 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@103 | 8 | * |
universe@103 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@103 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@103 | 11 | * |
universe@103 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@103 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@103 | 14 | * documentation and/or other materials provided with the distribution. |
universe@103 | 15 | * |
universe@103 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@103 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@103 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@103 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@103 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@103 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@103 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@103 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@103 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@103 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@103 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
olaf@13 | 27 | */ |
olaf@13 | 28 | |
universe@135 | 29 | /** |
universe@135 | 30 | * @file mempool.h |
universe@135 | 31 | * |
universe@135 | 32 | * Memory pool implementation. |
universe@135 | 33 | * |
universe@135 | 34 | * @author Mike Becker |
universe@135 | 35 | * @author Olaf Wintermann |
universe@135 | 36 | */ |
universe@135 | 37 | |
olaf@120 | 38 | #ifndef UCX_MEMPOOL_H |
olaf@120 | 39 | #define UCX_MEMPOOL_H |
olaf@13 | 40 | |
universe@69 | 41 | #include "ucx.h" |
universe@38 | 42 | #include <stddef.h> |
universe@50 | 43 | #include "allocator.h" |
universe@38 | 44 | |
olaf@13 | 45 | #ifdef __cplusplus |
olaf@13 | 46 | extern "C" { |
olaf@13 | 47 | #endif |
olaf@13 | 48 | |
universe@135 | 49 | /** |
universe@135 | 50 | * A function pointer to a destructor function. |
universe@135 | 51 | * @see ucx_mempool_setdestr() |
universe@135 | 52 | * @see ucx_mempool_regdestr() |
universe@135 | 53 | */ |
olaf@13 | 54 | typedef void(*ucx_destructor)(void*); |
olaf@13 | 55 | |
universe@135 | 56 | /** |
universe@135 | 57 | * UCX mempool structure. |
universe@135 | 58 | */ |
olaf@13 | 59 | typedef struct { |
universe@135 | 60 | /** List of pointers to pooled memory. */ |
olaf@13 | 61 | void **data; |
universe@135 | 62 | /** Count of pooled memory items. */ |
olaf@13 | 63 | size_t ndata; |
universe@135 | 64 | /** Memory pool size. */ |
olaf@13 | 65 | size_t size; |
olaf@13 | 66 | } UcxMempool; |
olaf@13 | 67 | |
universe@135 | 68 | /** Shorthand for a new default memory pool with a capacity of 16 elements. */ |
universe@135 | 69 | #define ucx_mempool_new_default() ucx_mempool_new(16) |
universe@48 | 70 | |
universe@135 | 71 | |
universe@135 | 72 | /** |
universe@135 | 73 | * Creates a memory pool with the specified initial size. |
universe@135 | 74 | * |
universe@135 | 75 | * As the created memory pool automatically grows in size by 16 elements, when |
universe@135 | 76 | * trying to allocate memory on a full pool, it is recommended that you use |
universe@135 | 77 | * a multiple of 16 for the initial size. |
universe@135 | 78 | * |
universe@135 | 79 | * @param n initial pool size (should be a multiple of 16) |
universe@135 | 80 | * @return a pointer to the new memory pool |
universe@135 | 81 | */ |
olaf@13 | 82 | UcxMempool *ucx_mempool_new(size_t n); |
universe@135 | 83 | |
universe@135 | 84 | /** |
universe@135 | 85 | * Resizes a memory pool. |
universe@135 | 86 | * |
universe@135 | 87 | * @param pool the pool to resize |
universe@135 | 88 | * @param newcap the new capacity |
universe@135 | 89 | * @return <code>EXIT_SUCCESS</code> on success or |
universe@135 | 90 | * <code>EXIT_FAILURE</code> on failure |
universe@135 | 91 | */ |
universe@15 | 92 | int ucx_mempool_chcap(UcxMempool *pool, size_t newcap); |
olaf@13 | 93 | |
universe@135 | 94 | /** |
universe@135 | 95 | * Allocates pooled memory. |
universe@135 | 96 | * |
universe@135 | 97 | * @param pool the memory pool |
universe@135 | 98 | * @param n amount of memory to allocate |
universe@135 | 99 | * @return a pointer to the allocated memory |
universe@135 | 100 | * @see ucx_allocator_malloc() |
universe@135 | 101 | */ |
olaf@13 | 102 | void *ucx_mempool_malloc(UcxMempool *pool, size_t n); |
universe@135 | 103 | /** |
universe@135 | 104 | * Allocates a pooled memory array. |
universe@135 | 105 | * |
universe@135 | 106 | * The contents of the allocated memory is set to zero. |
universe@135 | 107 | * |
universe@135 | 108 | * @param pool the memory pool |
universe@135 | 109 | * @param nelem amount of elements to allocate |
universe@135 | 110 | * @param elsize amount of memory per element |
universe@135 | 111 | * @return a pointer to the allocated memory |
universe@135 | 112 | * @see ucx_allocator_calloc() |
universe@135 | 113 | */ |
olaf@13 | 114 | void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize); |
universe@135 | 115 | /** |
universe@135 | 116 | * Reallocates pooled memory. |
universe@135 | 117 | * |
universe@135 | 118 | * @param pool the memory pool |
universe@135 | 119 | * @param ptr a pointer to the memory that shall be reallocated |
universe@135 | 120 | * @param n the new size of the memory |
universe@135 | 121 | * @return a pointer to the the location of the memory |
universe@135 | 122 | * @see ucx_allocator_realloc() |
universe@135 | 123 | */ |
olaf@13 | 124 | void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n); |
universe@135 | 125 | /** |
universe@135 | 126 | * Frees pooled memory. |
universe@135 | 127 | * |
universe@135 | 128 | * Before freeing the memory, the specified destructor function (if any) |
universe@135 | 129 | * is called. |
universe@135 | 130 | * |
universe@135 | 131 | * If you specify memory, that is not pooled by the specified memory pool, this |
universe@135 | 132 | * is considered as a severe error and an error message is written to |
universe@135 | 133 | * <code>stderr</code> and the application is shut down with code |
universe@135 | 134 | * <code>EXIT_FAILURE</code>. |
universe@135 | 135 | * |
universe@135 | 136 | * @param pool the memory pool |
universe@135 | 137 | * @param ptr a pointer to the memory that shall be freed |
universe@135 | 138 | * @see ucx_mempool_set_destr() |
universe@135 | 139 | */ |
olaf@113 | 140 | void ucx_mempool_free(UcxMempool *pool, void *ptr); |
olaf@13 | 141 | |
universe@135 | 142 | /** |
universe@135 | 143 | * Destroys a memory pool. |
universe@135 | 144 | * |
universe@135 | 145 | * For each element the destructor function (if any) is called and the element |
universe@135 | 146 | * is freed. |
universe@135 | 147 | * |
universe@135 | 148 | * Each of the registered destructor function that has no corresponding element |
universe@135 | 149 | * within the pool (namely those registered by ucx_mempool_reg_destr) is |
universe@135 | 150 | * called interleaving with the element destruction, but with guarantee to the |
universe@135 | 151 | * order in which they were registered (FIFO order). |
universe@135 | 152 | * |
universe@135 | 153 | * |
universe@135 | 154 | * @param pool the mempool to destroy |
universe@135 | 155 | */ |
olaf@113 | 156 | void ucx_mempool_destroy(UcxMempool *pool); |
olaf@13 | 157 | |
universe@135 | 158 | /** |
universe@135 | 159 | * Sets a destructor function for the specified memory. |
universe@135 | 160 | * |
universe@135 | 161 | * The destructor is automatically called when the memory is freed or the |
universe@135 | 162 | * pool is destroyed. |
universe@135 | 163 | * |
universe@135 | 164 | * The only requirement for the specified memory is, that it <b>MUST</b> be |
universe@135 | 165 | * pooled memory by an UcxMempool or an element-compatible mempool. The pointer |
universe@135 | 166 | * to the destructor function is saved in a reserved area before the actual |
universe@135 | 167 | * memory. |
universe@135 | 168 | * |
universe@135 | 169 | * @param ptr pooled memory |
universe@135 | 170 | * @param func a pointer to the destructor function |
universe@135 | 171 | * @see ucx_mempool_free() |
universe@135 | 172 | * @see ucx_mempool_destroy() |
universe@135 | 173 | */ |
olaf@13 | 174 | void ucx_mempool_set_destr(void *ptr, ucx_destructor func); |
universe@135 | 175 | |
universe@135 | 176 | /** |
universe@135 | 177 | * Registers a destructor function for the specified (non-pooled) memory. |
universe@135 | 178 | * |
universe@135 | 179 | * This is useful, if you have memory that has not been allocated by a mempool, |
universe@135 | 180 | * but shall be managed by a mempool. |
universe@135 | 181 | * |
universe@135 | 182 | * This function creates an entry in the specified mempool and the memory will |
universe@135 | 183 | * therefore (logically) convert to pooled memory. |
universe@135 | 184 | * |
universe@135 | 185 | * @param pool the memory pool |
universe@135 | 186 | * @param ptr data the destructor is registered for |
universe@135 | 187 | * @param destr a pointer to the destructor function |
universe@135 | 188 | */ |
olaf@13 | 189 | void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr); |
olaf@13 | 190 | |
universe@135 | 191 | /** |
universe@135 | 192 | * Creates an UcxAllocator based on an UcxMempool. |
universe@135 | 193 | * |
universe@135 | 194 | * @param pool the mempool to create the UcxAllocator for |
universe@135 | 195 | * @return a new UcxAllocator based on the specified pool |
universe@135 | 196 | */ |
olaf@113 | 197 | UcxAllocator* ucx_mempool_allocator(UcxMempool *pool); |
olaf@13 | 198 | |
olaf@13 | 199 | #ifdef __cplusplus |
olaf@13 | 200 | } |
olaf@13 | 201 | #endif |
olaf@13 | 202 | |
olaf@120 | 203 | #endif /* UCX_MEMPOOL_H */ |
olaf@13 | 204 |