src/ucx/allocator.h

Sat, 28 Oct 2017 15:43:51 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 28 Oct 2017 15:43:51 +0200
changeset 259
2f5dea574a75
parent 251
fae240d633fc
permissions
-rw-r--r--

modules documentation

universe@103 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@103 3 *
universe@259 4 * Copyright 2017 Mike Becker, 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@225 31 * A 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@225 41 * can be provided to the memory management functions. A 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@259 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@146 66
universe@118 67 /**
universe@118 68 * A function pointer to the allocators <code>calloc()</code> function.
universe@118 69 * @see UcxAllocator
universe@118 70 */
olaf@52 71 typedef void*(*ucx_allocator_calloc)(void *pool, size_t n, size_t size);
universe@146 72
universe@118 73 /**
universe@118 74 * A function pointer to the allocators <code>realloc()</code> function.
universe@118 75 * @see UcxAllocator
universe@118 76 */
olaf@52 77 typedef void*(*ucx_allocator_realloc)(void *pool, void *data, size_t n);
universe@146 78
universe@118 79 /**
universe@118 80 * A function pointer to the allocators <code>free()</code> function.
universe@118 81 * @see UcxAllocator
universe@118 82 */
olaf@107 83 typedef void(*ucx_allocator_free)(void *pool, void *data);
olaf@52 84
universe@118 85 /**
universe@118 86 * UCX allocator data structure containing memory management functions.
universe@118 87 */
olaf@52 88 typedef struct {
universe@118 89 /** Pointer to an area of memory or a complex memory pool.
universe@118 90 * This pointer will be passed to any memory management function as first
universe@118 91 * argument.
universe@118 92 */
olaf@52 93 void *pool;
universe@118 94 /**
universe@118 95 * The <code>malloc()</code> function for this allocator.
universe@118 96 */
olaf@107 97 ucx_allocator_malloc malloc;
universe@118 98 /**
universe@118 99 * The <code>calloc()</code> function for this allocator.
universe@118 100 */
olaf@107 101 ucx_allocator_calloc calloc;
universe@118 102 /**
universe@118 103 * The <code>realloc()</code> function for this allocator.
universe@118 104 */
olaf@52 105 ucx_allocator_realloc realloc;
universe@118 106 /**
universe@118 107 * The <code>free()</code> function for this allocator.
universe@118 108 */
olaf@107 109 ucx_allocator_free free;
olaf@52 110 } UcxAllocator;
olaf@52 111
universe@118 112 /**
universe@118 113 * Returns a pointer to the default allocator.
universe@118 114 *
universe@118 115 * The default allocator contains wrappers to the standard libc memory
universe@118 116 * management functions. Use this function to get a pointer to a globally
universe@118 117 * available allocator. You may also define an own UcxAllocator by assigning
universe@118 118 * #UCX_ALLOCATOR_DEFAULT to a variable and pass the address of this variable
universe@225 119 * to any function that takes a UcxAllocator as argument. Note that using
universe@118 120 * this function is the recommended way of passing a default allocator, thus
universe@118 121 * it never runs out of scope.
universe@118 122 *
universe@118 123 * @return a pointer to the default allocator
universe@118 124 *
universe@118 125 * @see UCX_ALLOCATOR_DEFAULT
universe@118 126 */
olaf@107 127 UcxAllocator *ucx_default_allocator();
olaf@107 128
universe@118 129 /**
universe@118 130 * A wrapper for the standard libc <code>malloc()</code> function.
universe@118 131 * @param ignore ignored (may be used by allocators for pooled memory)
universe@118 132 * @param n argument passed to <code>malloc()</code>
universe@118 133 * @return return value of <code>malloc()</code>
universe@118 134 */
olaf@52 135 void *ucx_default_malloc(void *ignore, size_t n);
universe@118 136 /**
universe@118 137 * A wrapper for the standard libc <code>calloc()</code> function.
universe@118 138 * @param ignore ignored (may be used by allocators for pooled memory)
universe@118 139 * @param n argument passed to <code>calloc()</code>
universe@118 140 * @param size argument passed to <code>calloc()</code>
universe@118 141 * @return return value of <code>calloc()</code>
universe@118 142 */
olaf@52 143 void *ucx_default_calloc(void *ignore, size_t n, size_t size);
universe@118 144 /**
universe@118 145 * A wrapper for the standard libc <code>realloc()</code> function.
universe@118 146 * @param ignore ignored (may be used by allocators for pooled memory)
universe@118 147 * @param data argumend passed to <code>realloc()</code>
universe@118 148 * @param n argument passed to <code>realloc()</code>
universe@118 149 * @return return value of <code>realloc()</code>
universe@118 150 */
olaf@52 151 void *ucx_default_realloc(void *ignore, void *data, size_t n);
universe@118 152 /**
universe@118 153 * A wrapper for the standard libc <code>free()</code> function.
universe@118 154 * @param ignore ignored (may be used by allocators for pooled memory)
universe@118 155 * @param data argument passed to <code>free()</code>
universe@118 156 */
olaf@107 157 void ucx_default_free(void *ignore, void *data);
olaf@52 158
universe@118 159 /**
universe@173 160 * Shorthand for calling an allocators malloc function.
universe@173 161 * @param allocator the allocator to use
universe@173 162 * @param n size of space to allocate
universe@173 163 * @return a pointer to the allocated memory area
universe@173 164 */
universe@174 165 #define almalloc(allocator, n) ((allocator)->malloc((allocator)->pool, n))
universe@173 166
universe@173 167 /**
universe@173 168 * Shorthand for calling an allocators calloc function.
universe@173 169 * @param allocator the allocator to use
universe@173 170 * @param n the count of elements the space should be allocated for
universe@173 171 * @param size the size of each element
universe@173 172 * @return a pointer to the allocated memory area
universe@173 173 */
universe@173 174 #define alcalloc(allocator, n, size) \
universe@174 175 ((allocator)->calloc((allocator)->pool, n, size))
universe@173 176
universe@173 177 /**
universe@173 178 * Shorthand for calling an allocators realloc function.
universe@173 179 * @param allocator the allocator to use
universe@173 180 * @param ptr the pointer to the memory area that shall be reallocated
universe@173 181 * @param n the new size of the allocated memory area
universe@173 182 * @return a pointer to the reallocated memory area
universe@173 183 */
universe@173 184 #define alrealloc(allocator, ptr, n) \
universe@174 185 ((allocator)->realloc((allocator)->pool, ptr, n))
universe@173 186
universe@173 187 /**
universe@173 188 * Shorthand for calling an allocators free function.
universe@173 189 * @param allocator the allocator to use
universe@173 190 * @param ptr the pointer to the memory area that shall be freed
universe@173 191 */
universe@174 192 #define alfree(allocator, ptr) ((allocator)->free((allocator)->pool, ptr))
universe@173 193
universe@173 194 /**
universe@118 195 * Convenient macro for a default allocator <code>struct</code> definition.
universe@118 196 */
olaf@52 197 #define UCX_ALLOCATOR_DEFAULT {NULL, \
olaf@107 198 ucx_default_malloc, ucx_default_calloc, ucx_default_realloc, \
olaf@107 199 ucx_default_free }
olaf@52 200
olaf@52 201 #ifdef __cplusplus
olaf@52 202 }
olaf@52 203 #endif
olaf@52 204
universe@118 205 #endif /* UCX_ALLOCATOR_H */
olaf@52 206

mercurial