universe@103: /*
universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
olaf@13: *
universe@259: * Copyright 2017 Mike Becker, 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.
olaf@13: */
olaf@13:
universe@135: /**
universe@182: * @file stack.h
universe@135: *
universe@182: * Default stack memory allocation implementation.
universe@135: *
universe@135: * @author Mike Becker
universe@135: * @author Olaf Wintermann
universe@135: */
universe@135:
universe@182: #ifndef UCX_STACK_H
universe@182: #define UCX_STACK_H
olaf@13:
universe@259: #include "ucx.h"
universe@259: #include "allocator.h"
universe@38:
olaf@13: #ifdef __cplusplus
olaf@13: extern "C" {
olaf@13: #endif
olaf@13:
olaf@13:
universe@135: /**
universe@182: * UCX stack structure.
universe@135: */
olaf@13: typedef struct {
universe@182: /** UcxAllocator based on this stack */
universe@182: UcxAllocator allocator;
olaf@158:
universe@182: /** Stack size. */
universe@182: size_t size;
universe@146:
universe@182: /** Pointer to the bottom of the stack */
universe@182: char *space;
universe@146:
universe@182: /** Pointer to the top of the stack */
universe@182: char *top;
universe@182: } UcxStack;
universe@135:
universe@135: /**
universe@185: * Metadata for each UCX stack element.
universe@185: */
universe@185: struct ucx_stack_metadata {
universe@185: /**
universe@185: * Location of the previous element (NULL
if this is the first)
universe@185: */
universe@185: char *prev;
universe@185:
universe@185: /** Size of this element */
universe@185: size_t size;
universe@185: };
universe@185:
universe@185: /**
universe@185: * Initializes UcxStack structure with memory.
universe@135: *
universe@185: * @param stack a pointer to an uninitialized stack structure
universe@182: * @param space the memory area that shall be managed
universe@182: * @param size size of the memory area
universe@182: * @return a new UcxStack structure
universe@135: */
universe@185: void ucx_stack_init(UcxStack *stack, char* space, size_t size);
universe@135:
universe@135: /**
universe@182: * Allocates stack memory.
universe@135: *
universe@182: * @param stack a pointer to the stack
universe@135: * @param n amount of memory to allocate
universe@135: * @return a pointer to the allocated memory
universe@135: * @see ucx_allocator_malloc()
universe@135: */
universe@182: void *ucx_stack_malloc(UcxStack *stack, size_t n);
universe@182:
universe@135: /**
universe@182: * Alias for #ucx_stack_malloc().
universe@182: * @param stack a pointer to the stack
universe@182: * @param n amount of memory to allocate
universe@182: * @return a pointer to the allocated memory
universe@182: * @see ucx_stack_malloc
universe@182: */
universe@188: #define ucx_stack_push(stack, n) ucx_stack_malloc(stack, n)
universe@182:
universe@182: /**
universe@182: * Allocates an array of stack memory
universe@135: *
universe@182: * The content of the allocated memory is set to zero.
universe@135: *
universe@182: * @param stack a pointer to the stack
universe@135: * @param nelem amount of elements to allocate
universe@135: * @param elsize amount of memory per element
universe@135: * @return a pointer to the allocated memory
universe@135: * @see ucx_allocator_calloc()
universe@135: */
universe@182: void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize);
universe@146:
universe@135: /**
universe@182: * Alias for #ucx_stack_calloc().
universe@135: *
universe@182: * @param stack a pointer to the stack
universe@188: * @param n amount of elements to allocate
universe@182: * @param elsize amount of memory per element
universe@182: * @return a pointer to the allocated memory
universe@182: * @see ucx_stack_calloc
universe@182: */
universe@188: #define ucx_stack_pusharr(stack,n,elsize) ucx_stack_calloc(stack,n,elssize)
universe@182:
universe@182: /**
universe@182: * Reallocates memory on the stack.
universe@141: *
universe@182: * Shrinking memory is always safe. Extending memory can be very expensive.
universe@182: *
universe@182: * @param stack the stack
universe@135: * @param ptr a pointer to the memory that shall be reallocated
universe@135: * @param n the new size of the memory
universe@182: * @return a pointer to the new location of the memory
universe@135: * @see ucx_allocator_realloc()
universe@135: */
universe@182: void *ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n);
universe@146:
universe@135: /**
universe@182: * Frees memory on the stack.
universe@135: *
universe@182: * Freeing stack memory behaves in a special way.
universe@135: *
universe@182: * If the element, that should be freed, is the top most element of the stack,
universe@182: * it is removed from the stack. Otherwise it is marked as freed. Marked
universe@182: * elements are removed, when they become the top most elements of the stack.
universe@135: *
universe@182: * @param stack a pointer to the stack
universe@135: * @param ptr a pointer to the memory that shall be freed
universe@135: */
universe@182: void ucx_stack_free(UcxStack *stack, void *ptr);
universe@182:
olaf@13:
universe@135: /**
universe@182: * Returns the size of the top most element.
universe@182: * @param stack a pointer to the stack
universe@182: * @return the size of the top most element
universe@135: */
universe@185: #define ucx_stack_topsize(stack) ((stack)->top ? ((struct ucx_stack_metadata*)\
universe@185: (stack)->top - 1)->size : 0)
olaf@13:
universe@135: /**
universe@182: * Removes the top most element from the stack and copies the content to
universe@182: * dest
, if specified.
universe@135: *
universe@182: * Use #ucx_stack_topsize()# to get the amount of memory that must be available
universe@182: * at the location of dest
.
universe@135: *
universe@182: * @param stack a pointer to the stack
universe@182: * @param dest the location where the contents shall be written to, or
universe@182: * NULL
, if the element shall only be removed.
universe@182: * @see ucx_stack_free
universe@182: * @see ucx_stack_popn
universe@135: */
universe@217: #define ucx_stack_pop(stack, dest) ucx_stack_popn(stack, dest, (size_t)-1)
universe@135:
universe@135: /**
universe@182: * Removes the top most element from the stack and copies the content to
universe@182: * dest
.
universe@135: *
universe@182: * In contrast to #ucx_stack_pop() the dest
pointer MUST
universe@182: * NOT
be NULL
.
universe@135: *
universe@182: * @param stack a pointer to the stack
universe@182: * @param dest the location where the contents shall be written to
universe@182: * @param n copies at most n elements to dest
universe@182: * @see ucx_stack_pop
universe@182: */
universe@182: void ucx_stack_popn(UcxStack *stack, void *dest, size_t n);
universe@182:
universe@182: /**
universe@182: * Returns the remaining available memory on the specified stack.
universe@135: *
universe@182: * @param stack a pointer to the stack
universe@182: * @return the remaining available memory
universe@135: */
universe@185: size_t ucx_stack_avail(UcxStack *stack);
universe@185:
universe@185: /**
universe@185: * Checks, if the stack is empty.
universe@185: *
universe@185: * @param stack a pointer to the stack
universe@185: * @return nonzero, if the stack is empty, zero otherwise
universe@185: */
universe@185: #define ucx_stack_empty(stack) (!(stack)->top)
universe@185:
universe@185: /**
universe@185: * Computes a recommended size for the stack memory area. Note, that
universe@185: * reallocations have not been taken into account, so you might need to reserve
universe@185: * twice as much memory to allow many reallocations.
universe@185: *
universe@185: * @param size the approximate payload
universe@185: * @param elems the approximate count of element allocations
universe@185: * @return a recommended size for the stack space based on the information
universe@185: * provided
universe@185: */
universe@185: #define ucx_stack_dim(size, elems) (size+sizeof(struct ucx_stack_metadata) * \
universe@185: (elems + 1))
universe@182:
olaf@13:
olaf@13: #ifdef __cplusplus
olaf@13: }
olaf@13: #endif
olaf@13:
universe@182: #endif /* UCX_STACK_H */
olaf@13: