src/ucx/allocator.h

changeset 39
ac35daceb24c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/ucx/allocator.h	Tue Aug 23 13:49:38 2016 +0200
     1.3 @@ -0,0 +1,206 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 2015 Olaf Wintermann. All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions are met:
    1.11 + *
    1.12 + *   1. Redistributions of source code must retain the above copyright
    1.13 + *      notice, this list of conditions and the following disclaimer.
    1.14 + *
    1.15 + *   2. Redistributions in binary form must reproduce the above copyright
    1.16 + *      notice, this list of conditions and the following disclaimer in the
    1.17 + *      documentation and/or other materials provided with the distribution.
    1.18 + *
    1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 + * POSSIBILITY OF SUCH DAMAGE.
    1.30 + */
    1.31 +/**
    1.32 + * Allocator for custom memory management.
    1.33 + * 
    1.34 + * An UCX allocator consists of a pointer to the memory area / pool and four
    1.35 + * function pointers to memory management functions operating on this memory
    1.36 + * area / pool. These functions shall behave equivalent to the standard libc
    1.37 + * functions <code>malloc(), calloc(), realloc()</code> and <code>free()</code>.
    1.38 + * 
    1.39 + * The signature of the memory management functions is based on the signature
    1.40 + * of the respective libc function but each of them takes the pointer to the
    1.41 + * memory area / pool as first argument.
    1.42 + * 
    1.43 + * As the pointer to the memory area / pool can be arbitrarily chosen, any data
    1.44 + * can be provided to the memory management functions. An UcxMempool is just
    1.45 + * one example.
    1.46 + * 
    1.47 + * @see mempool.h
    1.48 + * @see UcxMap
    1.49 + * 
    1.50 + * @file   allocator.h
    1.51 + * @author Mike Becker
    1.52 + * @author Olaf Wintermann
    1.53 + */
    1.54 +
    1.55 +#ifndef UCX_ALLOCATOR_H
    1.56 +#define	UCX_ALLOCATOR_H
    1.57 +
    1.58 +#include "ucx.h"
    1.59 +
    1.60 +#ifdef	__cplusplus
    1.61 +extern "C" {
    1.62 +#endif
    1.63 +
    1.64 +/**
    1.65 + * A function pointer to the allocators <code>malloc()</code> function.
    1.66 + * @see UcxAllocator
    1.67 + */
    1.68 +typedef void*(*ucx_allocator_malloc)(void *pool, size_t n);
    1.69 +
    1.70 +/**
    1.71 + * A function pointer to the allocators <code>calloc()</code> function.
    1.72 + * @see UcxAllocator
    1.73 + */
    1.74 +typedef void*(*ucx_allocator_calloc)(void *pool, size_t n, size_t size);
    1.75 +
    1.76 +/**
    1.77 + * A function pointer to the allocators <code>realloc()</code> function.
    1.78 + * @see UcxAllocator
    1.79 + */
    1.80 +typedef void*(*ucx_allocator_realloc)(void *pool, void *data, size_t n);
    1.81 +
    1.82 +/**
    1.83 + * A function pointer to the allocators <code>free()</code> function.
    1.84 + * @see UcxAllocator
    1.85 + */
    1.86 +typedef void(*ucx_allocator_free)(void *pool, void *data);
    1.87 +
    1.88 +/**
    1.89 + * UCX allocator data structure containing memory management functions.
    1.90 + */
    1.91 +typedef struct {
    1.92 +    /** Pointer to an area of memory or a complex memory pool.
    1.93 +     * This pointer will be passed to any memory management function as first
    1.94 +     * argument.
    1.95 +     */
    1.96 +    void *pool;
    1.97 +    /**
    1.98 +     * The <code>malloc()</code> function for this allocator.
    1.99 +     */
   1.100 +    ucx_allocator_malloc  malloc;
   1.101 +    /**
   1.102 +     * The <code>calloc()</code> function for this allocator.
   1.103 +     */
   1.104 +    ucx_allocator_calloc  calloc;
   1.105 +    /**
   1.106 +     * The <code>realloc()</code> function for this allocator.
   1.107 +     */
   1.108 +    ucx_allocator_realloc realloc;
   1.109 +    /**
   1.110 +     * The <code>free()</code> function for this allocator.
   1.111 +     */
   1.112 +    ucx_allocator_free    free;
   1.113 +} UcxAllocator;
   1.114 +
   1.115 +/**
   1.116 + * Returns a pointer to the default allocator.
   1.117 + * 
   1.118 + * The default allocator contains wrappers to the standard libc memory
   1.119 + * management functions. Use this function to get a pointer to a globally
   1.120 + * available allocator. You may also define an own UcxAllocator by assigning
   1.121 + * #UCX_ALLOCATOR_DEFAULT to a variable and pass the address of this variable
   1.122 + * to any function that takes an UcxAllocator as argument. Note that using
   1.123 + * this function is the recommended way of passing a default allocator, thus
   1.124 + * it never runs out of scope.
   1.125 + * 
   1.126 + * @return a pointer to the default allocator
   1.127 + * 
   1.128 + * @see UCX_ALLOCATOR_DEFAULT
   1.129 + */
   1.130 +UcxAllocator *ucx_default_allocator();
   1.131 +
   1.132 +/**
   1.133 + * A wrapper for the standard libc <code>malloc()</code> function.
   1.134 + * @param ignore ignored (may be used by allocators for pooled memory)
   1.135 + * @param n argument passed to <code>malloc()</code>
   1.136 + * @return return value of <code>malloc()</code>
   1.137 + */
   1.138 +void *ucx_default_malloc(void *ignore, size_t n);
   1.139 +/**
   1.140 + * A wrapper for the standard libc <code>calloc()</code> function.
   1.141 + * @param ignore ignored (may be used by allocators for pooled memory)
   1.142 + * @param n argument passed to <code>calloc()</code>
   1.143 + * @param size  argument passed to <code>calloc()</code>
   1.144 + * @return return value of <code>calloc()</code>
   1.145 + */
   1.146 +void *ucx_default_calloc(void *ignore, size_t n, size_t size);
   1.147 +/**
   1.148 + * A wrapper for the standard libc <code>realloc()</code> function.
   1.149 + * @param ignore ignored (may be used by allocators for pooled memory)
   1.150 + * @param data argumend passed to <code>realloc()</code>
   1.151 + * @param n argument passed to <code>realloc()</code>
   1.152 + * @return return value of <code>realloc()</code>
   1.153 + */
   1.154 +void *ucx_default_realloc(void *ignore, void *data, size_t n);
   1.155 +/**
   1.156 + * A wrapper for the standard libc <code>free()</code> function.
   1.157 + * @param ignore ignored (may be used by allocators for pooled memory)
   1.158 + * @param data argument passed to <code>free()</code>
   1.159 + */
   1.160 +void ucx_default_free(void *ignore, void *data);
   1.161 +
   1.162 +/**
   1.163 + * Shorthand for calling an allocators malloc function.
   1.164 + * @param allocator the allocator to use
   1.165 + * @param n size of space to allocate
   1.166 + * @return a pointer to the allocated memory area
   1.167 + */
   1.168 +#define almalloc(allocator, n) ((allocator)->malloc((allocator)->pool, n))
   1.169 +
   1.170 +/**
   1.171 + * Shorthand for calling an allocators calloc function.
   1.172 + * @param allocator the allocator to use
   1.173 + * @param n the count of elements the space should be allocated for
   1.174 + * @param size the size of each element
   1.175 + * @return a pointer to the allocated memory area
   1.176 + */
   1.177 +#define alcalloc(allocator, n, size) \
   1.178 +        ((allocator)->calloc((allocator)->pool, n, size))
   1.179 +
   1.180 +/**
   1.181 + * Shorthand for calling an allocators realloc function.
   1.182 + * @param allocator the allocator to use
   1.183 + * @param ptr the pointer to the memory area that shall be reallocated
   1.184 + * @param n the new size of the allocated memory area
   1.185 + * @return a pointer to the reallocated memory area
   1.186 + */
   1.187 +#define alrealloc(allocator, ptr, n) \
   1.188 +        ((allocator)->realloc((allocator)->pool, ptr, n))
   1.189 +
   1.190 +/**
   1.191 + * Shorthand for calling an allocators free function.
   1.192 + * @param allocator the allocator to use
   1.193 + * @param ptr the pointer to the memory area that shall be freed
   1.194 + */
   1.195 +#define alfree(allocator, ptr) ((allocator)->free((allocator)->pool, ptr))
   1.196 +
   1.197 +/**
   1.198 + * Convenient macro for a default allocator <code>struct</code> definition.
   1.199 + */
   1.200 +#define UCX_ALLOCATOR_DEFAULT {NULL, \
   1.201 +        ucx_default_malloc, ucx_default_calloc, ucx_default_realloc, \
   1.202 +        ucx_default_free }
   1.203 +
   1.204 +#ifdef	__cplusplus
   1.205 +}
   1.206 +#endif
   1.207 +
   1.208 +#endif	/* UCX_ALLOCATOR_H */
   1.209 +

mercurial