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