Sat, 20 Jul 2013 11:13:26 +0200
some fixes
universe@103 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@103 | 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. |
universe@103 | 27 | */ |
universe@118 | 28 | /** |
universe@118 | 29 | * Allocator for custom memory management. |
universe@118 | 30 | * |
universe@118 | 31 | * An UCX allocator consists of a pointer to the memory area / pool and four |
universe@118 | 32 | * function pointers to memory management functions operating on this memory |
universe@118 | 33 | * area / pool. These functions shall behave equivalent to the standard libc |
universe@118 | 34 | * functions <code>malloc(), calloc(), realloc()</code> and <code>free()</code>. |
universe@118 | 35 | * |
universe@118 | 36 | * The signature of the memory management functions is based on the signature |
universe@118 | 37 | * of the respective libc function but each of them takes the pointer to the |
universe@118 | 38 | * memory area / pool as first argument. |
universe@118 | 39 | * |
universe@118 | 40 | * As the pointer to the memory area / pool can be arbitrarily chosen, any data |
universe@118 | 41 | * can be provided to the memory management functions. An UcxMempool is just |
universe@118 | 42 | * one example. |
universe@118 | 43 | * |
universe@118 | 44 | * @see mempool.h |
universe@118 | 45 | * @see UcxMap |
universe@118 | 46 | * |
universe@118 | 47 | * @file allocator.h |
universe@118 | 48 | * @author Mike Becker |
universe@118 | 49 | * @author Olaf Wintermann |
universe@118 | 50 | */ |
universe@103 | 51 | |
universe@118 | 52 | #ifndef UCX_ALLOCATOR_H |
universe@118 | 53 | #define UCX_ALLOCATOR_H |
olaf@52 | 54 | |
universe@69 | 55 | #include "ucx.h" |
universe@69 | 56 | |
olaf@52 | 57 | #ifdef __cplusplus |
olaf@52 | 58 | extern "C" { |
olaf@52 | 59 | #endif |
olaf@52 | 60 | |
universe@118 | 61 | /** |
universe@118 | 62 | * A function pointer to the allocators <code>malloc()</code> function. |
universe@118 | 63 | * @see UcxAllocator |
universe@118 | 64 | */ |
olaf@52 | 65 | typedef void*(*ucx_allocator_malloc)(void *pool, size_t n); |
universe@118 | 66 | /** |
universe@118 | 67 | * A function pointer to the allocators <code>calloc()</code> function. |
universe@118 | 68 | * @see UcxAllocator |
universe@118 | 69 | */ |
olaf@52 | 70 | typedef void*(*ucx_allocator_calloc)(void *pool, size_t n, size_t size); |
universe@118 | 71 | /** |
universe@118 | 72 | * A function pointer to the allocators <code>realloc()</code> function. |
universe@118 | 73 | * @see UcxAllocator |
universe@118 | 74 | */ |
olaf@52 | 75 | typedef void*(*ucx_allocator_realloc)(void *pool, void *data, size_t n); |
universe@118 | 76 | /** |
universe@118 | 77 | * A function pointer to the allocators <code>free()</code> function. |
universe@118 | 78 | * @see UcxAllocator |
universe@118 | 79 | */ |
olaf@107 | 80 | typedef void(*ucx_allocator_free)(void *pool, void *data); |
olaf@52 | 81 | |
universe@118 | 82 | /** |
universe@118 | 83 | * UCX allocator data structure containing memory management functions. |
universe@118 | 84 | */ |
olaf@52 | 85 | typedef struct { |
universe@118 | 86 | /** Pointer to an area of memory or a complex memory pool. |
universe@118 | 87 | * This pointer will be passed to any memory management function as first |
universe@118 | 88 | * argument. |
universe@118 | 89 | */ |
olaf@52 | 90 | void *pool; |
universe@118 | 91 | /** |
universe@118 | 92 | * The <code>malloc()</code> function for this allocator. |
universe@118 | 93 | */ |
olaf@107 | 94 | ucx_allocator_malloc malloc; |
universe@118 | 95 | /** |
universe@118 | 96 | * The <code>calloc()</code> function for this allocator. |
universe@118 | 97 | */ |
olaf@107 | 98 | ucx_allocator_calloc calloc; |
universe@118 | 99 | /** |
universe@118 | 100 | * The <code>realloc()</code> function for this allocator. |
universe@118 | 101 | */ |
olaf@52 | 102 | ucx_allocator_realloc realloc; |
universe@118 | 103 | /** |
universe@118 | 104 | * The <code>free()</code> function for this allocator. |
universe@118 | 105 | */ |
olaf@107 | 106 | ucx_allocator_free free; |
olaf@52 | 107 | } UcxAllocator; |
olaf@52 | 108 | |
universe@118 | 109 | /** |
universe@118 | 110 | * Returns a pointer to the default allocator. |
universe@118 | 111 | * |
universe@118 | 112 | * The default allocator contains wrappers to the standard libc memory |
universe@118 | 113 | * management functions. Use this function to get a pointer to a globally |
universe@118 | 114 | * available allocator. You may also define an own UcxAllocator by assigning |
universe@118 | 115 | * #UCX_ALLOCATOR_DEFAULT to a variable and pass the address of this variable |
universe@118 | 116 | * to any function that takes an UcxAllocator as argument. Note that using |
universe@118 | 117 | * this function is the recommended way of passing a default allocator, thus |
universe@118 | 118 | * it never runs out of scope. |
universe@118 | 119 | * |
universe@118 | 120 | * @return a pointer to the default allocator |
universe@118 | 121 | * |
universe@118 | 122 | * @see UCX_ALLOCATOR_DEFAULT |
universe@118 | 123 | */ |
olaf@107 | 124 | UcxAllocator *ucx_default_allocator(); |
olaf@107 | 125 | |
universe@118 | 126 | /** |
universe@118 | 127 | * A wrapper for the standard libc <code>malloc()</code> function. |
universe@118 | 128 | * @param ignore ignored (may be used by allocators for pooled memory) |
universe@118 | 129 | * @param n argument passed to <code>malloc()</code> |
universe@118 | 130 | * @return return value of <code>malloc()</code> |
universe@118 | 131 | */ |
olaf@52 | 132 | void *ucx_default_malloc(void *ignore, size_t n); |
universe@118 | 133 | /** |
universe@118 | 134 | * A wrapper for the standard libc <code>calloc()</code> function. |
universe@118 | 135 | * @param ignore ignored (may be used by allocators for pooled memory) |
universe@118 | 136 | * @param n argument passed to <code>calloc()</code> |
universe@118 | 137 | * @param size argument passed to <code>calloc()</code> |
universe@118 | 138 | * @return return value of <code>calloc()</code> |
universe@118 | 139 | */ |
olaf@52 | 140 | void *ucx_default_calloc(void *ignore, size_t n, size_t size); |
universe@118 | 141 | /** |
universe@118 | 142 | * A wrapper for the standard libc <code>realloc()</code> function. |
universe@118 | 143 | * @param ignore ignored (may be used by allocators for pooled memory) |
universe@118 | 144 | * @param data argumend passed to <code>realloc()</code> |
universe@118 | 145 | * @param n argument passed to <code>realloc()</code> |
universe@118 | 146 | * @return return value of <code>realloc()</code> |
universe@118 | 147 | */ |
olaf@52 | 148 | void *ucx_default_realloc(void *ignore, void *data, size_t n); |
universe@118 | 149 | /** |
universe@118 | 150 | * A wrapper for the standard libc <code>free()</code> function. |
universe@118 | 151 | * @param ignore ignored (may be used by allocators for pooled memory) |
universe@118 | 152 | * @param data argument passed to <code>free()</code> |
universe@118 | 153 | */ |
olaf@107 | 154 | void ucx_default_free(void *ignore, void *data); |
olaf@52 | 155 | |
universe@118 | 156 | /** |
universe@118 | 157 | * Convenient macro for a default allocator <code>struct</code> definition. |
universe@118 | 158 | */ |
olaf@52 | 159 | #define UCX_ALLOCATOR_DEFAULT {NULL, \ |
olaf@107 | 160 | ucx_default_malloc, ucx_default_calloc, ucx_default_realloc, \ |
olaf@107 | 161 | ucx_default_free } |
olaf@52 | 162 | |
olaf@52 | 163 | #ifdef __cplusplus |
olaf@52 | 164 | } |
olaf@52 | 165 | #endif |
olaf@52 | 166 | |
universe@118 | 167 | #endif /* UCX_ALLOCATOR_H */ |
olaf@52 | 168 |