/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /** * @file stack.h * * Default stack memory allocation implementation. * * @author Mike Becker * @author Olaf Wintermann */ #ifndef UCX_STACK_H #define UCX_STACK_H #include "ucx.h" #include "allocator.h" #ifdef __cplusplus extern "C" { #endif /** * UCX stack structure. */ typedef struct { /** UcxAllocator based on this stack */ UcxAllocator allocator; /** Stack size. */ size_t size; /** Pointer to the bottom of the stack */ char *space; /** Pointer to the top of the stack */ char *top; } UcxStack; /** * Metadata for each UCX stack element. */ struct ucx_stack_metadata { /** * Location of the previous element (NULL if this is the first) */ char *prev; /** Size of this element */ size_t size; }; /** * Initializes UcxStack structure with memory. * * @param stack a pointer to an uninitialized stack structure * @param space the memory area that shall be managed * @param size size of the memory area * @return a new UcxStack structure */ void ucx_stack_init(UcxStack *stack, char* space, size_t size); /** * Allocates stack memory. * * @param stack a pointer to the stack * @param n amount of memory to allocate * @return a pointer to the allocated memory or NULL on stack * overflow * @see ucx_allocator_malloc() */ void *ucx_stack_malloc(UcxStack *stack, size_t n); /** * Allocates memory with #ucx_stack_malloc() and copies the specified data if * the allocation was successful. * * @param stack a pointer to the stack * @param n amount of memory to allocate * @param data a pointer to the data to copy * @return a pointer to the allocated memory * @see ucx_stack_malloc */ void *ucx_stack_push(UcxStack *stack, size_t n, const void *data); /** * Allocates an array of stack memory * * The content of the allocated memory is set to zero. * * @param stack a pointer to the stack * @param nelem amount of elements to allocate * @param elsize amount of memory per element * @return a pointer to the allocated memory * @see ucx_allocator_calloc() */ void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize); /** * Allocates memory with #ucx_stack_calloc() and copies the specified data if * the allocation was successful. * * @param stack a pointer to the stack * @param nelem amount of elements to allocate * @param elsize amount of memory per element * @param data a pointer to the data * @return a pointer to the allocated memory * @see ucx_stack_calloc */ void *ucx_stack_pusharr(UcxStack *stack, size_t nelem, size_t elsize, const void *data); /** * Reallocates memory on the stack. * * Shrinking memory is always safe. Extending memory can be very expensive. * * @param stack the stack * @param ptr a pointer to the memory that shall be reallocated * @param n the new size of the memory * @return a pointer to the new location of the memory * @see ucx_allocator_realloc() */ void *ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n); /** * Frees memory on the stack. * * Freeing stack memory behaves in a special way. * * If the element, that should be freed, is the top most element of the stack, * it is removed from the stack. Otherwise it is marked as freed. Marked * elements are removed, when they become the top most elements of the stack. * * @param stack a pointer to the stack * @param ptr a pointer to the memory that shall be freed */ void ucx_stack_free(UcxStack *stack, void *ptr); /** * Returns the size of the top most element. * @param stack a pointer to the stack * @return the size of the top most element */ #define ucx_stack_topsize(stack) ((stack)->top ? ((struct ucx_stack_metadata*)\ (stack)->top - 1)->size : 0) /** * Removes the top most element from the stack and copies the content to * dest, if specified. * * Use #ucx_stack_topsize()# to get the amount of memory that must be available * at the location of dest. * * @param stack a pointer to the stack * @param dest the location where the contents shall be written to, or * NULL, if the element shall only be removed. * @see ucx_stack_free * @see ucx_stack_popn */ #define ucx_stack_pop(stack, dest) ucx_stack_popn(stack, dest, (size_t)-1) /** * Removes the top most element from the stack and copies the content to * dest. * * This function copies at most n bytes to the destination, but * the element is always freed as a whole. * If the element was larger than n, the remaining data is lost. * * @param stack a pointer to the stack * @param dest the location where the contents shall be written to * @param n copies at most n bytes to dest * @see ucx_stack_pop */ void ucx_stack_popn(UcxStack *stack, void *dest, size_t n); /** * Returns the remaining available memory on the specified stack. * * @param stack a pointer to the stack * @return the remaining available memory */ size_t ucx_stack_avail(UcxStack *stack); /** * Checks, if the stack is empty. * * @param stack a pointer to the stack * @return nonzero, if the stack is empty, zero otherwise */ #define ucx_stack_empty(stack) (!(stack)->top) /** * Computes a recommended size for the stack memory area. Note, that * reallocations have not been taken into account, so you might need to reserve * twice as much memory to allow many reallocations. * * @param size the approximate payload * @param elems the approximate count of element allocations * @return a recommended size for the stack space based on the information * provided */ #define ucx_stack_dim(size, elems) (size+sizeof(struct ucx_stack_metadata) * \ (elems + 1)) #ifdef __cplusplus } #endif #endif /* UCX_STACK_H */