universe@39: /*
universe@39: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@39: *
universe@39: * Copyright 2015 Olaf Wintermann. All rights reserved.
universe@39: *
universe@39: * Redistribution and use in source and binary forms, with or without
universe@39: * modification, are permitted provided that the following conditions are met:
universe@39: *
universe@39: * 1. Redistributions of source code must retain the above copyright
universe@39: * notice, this list of conditions and the following disclaimer.
universe@39: *
universe@39: * 2. Redistributions in binary form must reproduce the above copyright
universe@39: * notice, this list of conditions and the following disclaimer in the
universe@39: * documentation and/or other materials provided with the distribution.
universe@39: *
universe@39: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@39: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@39: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@39: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@39: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@39: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@39: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@39: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@39: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@39: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@39: * POSSIBILITY OF SUCH DAMAGE.
universe@39: */
universe@39: /**
universe@39: * Allocator for custom memory management.
universe@39: *
universe@39: * An UCX allocator consists of a pointer to the memory area / pool and four
universe@39: * function pointers to memory management functions operating on this memory
universe@39: * area / pool. These functions shall behave equivalent to the standard libc
universe@39: * functions malloc(), calloc(), realloc()
and free()
.
universe@39: *
universe@39: * The signature of the memory management functions is based on the signature
universe@39: * of the respective libc function but each of them takes the pointer to the
universe@39: * memory area / pool as first argument.
universe@39: *
universe@39: * As the pointer to the memory area / pool can be arbitrarily chosen, any data
universe@39: * can be provided to the memory management functions. An UcxMempool is just
universe@39: * one example.
universe@39: *
universe@39: * @see mempool.h
universe@39: * @see UcxMap
universe@39: *
universe@39: * @file allocator.h
universe@39: * @author Mike Becker
universe@39: * @author Olaf Wintermann
universe@39: */
universe@39:
universe@39: #ifndef UCX_ALLOCATOR_H
universe@39: #define UCX_ALLOCATOR_H
universe@39:
universe@39: #include "ucx.h"
universe@39:
universe@39: #ifdef __cplusplus
universe@39: extern "C" {
universe@39: #endif
universe@39:
universe@39: /**
universe@39: * A function pointer to the allocators malloc()
function.
universe@39: * @see UcxAllocator
universe@39: */
universe@39: typedef void*(*ucx_allocator_malloc)(void *pool, size_t n);
universe@39:
universe@39: /**
universe@39: * A function pointer to the allocators calloc()
function.
universe@39: * @see UcxAllocator
universe@39: */
universe@39: typedef void*(*ucx_allocator_calloc)(void *pool, size_t n, size_t size);
universe@39:
universe@39: /**
universe@39: * A function pointer to the allocators realloc()
function.
universe@39: * @see UcxAllocator
universe@39: */
universe@39: typedef void*(*ucx_allocator_realloc)(void *pool, void *data, size_t n);
universe@39:
universe@39: /**
universe@39: * A function pointer to the allocators free()
function.
universe@39: * @see UcxAllocator
universe@39: */
universe@39: typedef void(*ucx_allocator_free)(void *pool, void *data);
universe@39:
universe@39: /**
universe@39: * UCX allocator data structure containing memory management functions.
universe@39: */
universe@39: typedef struct {
universe@39: /** Pointer to an area of memory or a complex memory pool.
universe@39: * This pointer will be passed to any memory management function as first
universe@39: * argument.
universe@39: */
universe@39: void *pool;
universe@39: /**
universe@39: * The malloc()
function for this allocator.
universe@39: */
universe@39: ucx_allocator_malloc malloc;
universe@39: /**
universe@39: * The calloc()
function for this allocator.
universe@39: */
universe@39: ucx_allocator_calloc calloc;
universe@39: /**
universe@39: * The realloc()
function for this allocator.
universe@39: */
universe@39: ucx_allocator_realloc realloc;
universe@39: /**
universe@39: * The free()
function for this allocator.
universe@39: */
universe@39: ucx_allocator_free free;
universe@39: } UcxAllocator;
universe@39:
universe@39: /**
universe@39: * Returns a pointer to the default allocator.
universe@39: *
universe@39: * The default allocator contains wrappers to the standard libc memory
universe@39: * management functions. Use this function to get a pointer to a globally
universe@39: * available allocator. You may also define an own UcxAllocator by assigning
universe@39: * #UCX_ALLOCATOR_DEFAULT to a variable and pass the address of this variable
universe@39: * to any function that takes an UcxAllocator as argument. Note that using
universe@39: * this function is the recommended way of passing a default allocator, thus
universe@39: * it never runs out of scope.
universe@39: *
universe@39: * @return a pointer to the default allocator
universe@39: *
universe@39: * @see UCX_ALLOCATOR_DEFAULT
universe@39: */
universe@39: UcxAllocator *ucx_default_allocator();
universe@39:
universe@39: /**
universe@39: * A wrapper for the standard libc malloc()
function.
universe@39: * @param ignore ignored (may be used by allocators for pooled memory)
universe@39: * @param n argument passed to malloc()
universe@39: * @return return value of malloc()
universe@39: */
universe@39: void *ucx_default_malloc(void *ignore, size_t n);
universe@39: /**
universe@39: * A wrapper for the standard libc calloc()
function.
universe@39: * @param ignore ignored (may be used by allocators for pooled memory)
universe@39: * @param n argument passed to calloc()
universe@39: * @param size argument passed to calloc()
universe@39: * @return return value of calloc()
universe@39: */
universe@39: void *ucx_default_calloc(void *ignore, size_t n, size_t size);
universe@39: /**
universe@39: * A wrapper for the standard libc realloc()
function.
universe@39: * @param ignore ignored (may be used by allocators for pooled memory)
universe@39: * @param data argumend passed to realloc()
universe@39: * @param n argument passed to realloc()
universe@39: * @return return value of realloc()
universe@39: */
universe@39: void *ucx_default_realloc(void *ignore, void *data, size_t n);
universe@39: /**
universe@39: * A wrapper for the standard libc free()
function.
universe@39: * @param ignore ignored (may be used by allocators for pooled memory)
universe@39: * @param data argument passed to free()
universe@39: */
universe@39: void ucx_default_free(void *ignore, void *data);
universe@39:
universe@39: /**
universe@39: * Shorthand for calling an allocators malloc function.
universe@39: * @param allocator the allocator to use
universe@39: * @param n size of space to allocate
universe@39: * @return a pointer to the allocated memory area
universe@39: */
universe@39: #define almalloc(allocator, n) ((allocator)->malloc((allocator)->pool, n))
universe@39:
universe@39: /**
universe@39: * Shorthand for calling an allocators calloc function.
universe@39: * @param allocator the allocator to use
universe@39: * @param n the count of elements the space should be allocated for
universe@39: * @param size the size of each element
universe@39: * @return a pointer to the allocated memory area
universe@39: */
universe@39: #define alcalloc(allocator, n, size) \
universe@39: ((allocator)->calloc((allocator)->pool, n, size))
universe@39:
universe@39: /**
universe@39: * Shorthand for calling an allocators realloc function.
universe@39: * @param allocator the allocator to use
universe@39: * @param ptr the pointer to the memory area that shall be reallocated
universe@39: * @param n the new size of the allocated memory area
universe@39: * @return a pointer to the reallocated memory area
universe@39: */
universe@39: #define alrealloc(allocator, ptr, n) \
universe@39: ((allocator)->realloc((allocator)->pool, ptr, n))
universe@39:
universe@39: /**
universe@39: * Shorthand for calling an allocators free function.
universe@39: * @param allocator the allocator to use
universe@39: * @param ptr the pointer to the memory area that shall be freed
universe@39: */
universe@39: #define alfree(allocator, ptr) ((allocator)->free((allocator)->pool, ptr))
universe@39:
universe@39: /**
universe@39: * Convenient macro for a default allocator struct
definition.
universe@39: */
universe@39: #define UCX_ALLOCATOR_DEFAULT {NULL, \
universe@39: ucx_default_malloc, ucx_default_calloc, ucx_default_realloc, \
universe@39: ucx_default_free }
universe@39:
universe@39: #ifdef __cplusplus
universe@39: }
universe@39: #endif
universe@39:
universe@39: #endif /* UCX_ALLOCATOR_H */
universe@39: