ucx/stack.h

Mon, 14 Jul 2014 16:54:10 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 14 Jul 2014 16:54:10 +0200
changeset 182
998bf7c643b4
parent 177
ucx/mempool.h@11ad03783baf
child 185
a48428642b4e
permissions
-rw-r--r--

merged sstrcat function

universe@103 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
olaf@13 3 *
universe@177 4 * Copyright 2014 Olaf Wintermann. All rights reserved.
universe@103 5 *
universe@103 6 * Redistribution and use in source and binary forms, with or without
universe@103 7 * modification, are permitted provided that the following conditions are met:
universe@103 8 *
universe@103 9 * 1. Redistributions of source code must retain the above copyright
universe@103 10 * notice, this list of conditions and the following disclaimer.
universe@103 11 *
universe@103 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@103 13 * notice, this list of conditions and the following disclaimer in the
universe@103 14 * documentation and/or other materials provided with the distribution.
universe@103 15 *
universe@103 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@103 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@103 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@103 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@103 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@103 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@103 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@103 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@103 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@103 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@103 26 * POSSIBILITY OF SUCH DAMAGE.
olaf@13 27 */
olaf@13 28
universe@135 29 /**
universe@182 30 * @file stack.h
universe@135 31 *
universe@182 32 * Default stack memory allocation implementation.
universe@135 33 *
universe@135 34 * @author Mike Becker
universe@135 35 * @author Olaf Wintermann
universe@135 36 */
universe@135 37
universe@182 38 #ifndef UCX_STACK_H
universe@182 39 #define UCX_STACK_H
olaf@13 40
universe@69 41 #include "ucx.h"
universe@38 42 #include <stddef.h>
universe@50 43 #include "allocator.h"
universe@38 44
olaf@13 45 #ifdef __cplusplus
olaf@13 46 extern "C" {
olaf@13 47 #endif
olaf@13 48
olaf@13 49
universe@135 50 /**
universe@182 51 * UCX stack structure.
universe@135 52 */
olaf@13 53 typedef struct {
universe@182 54 /** UcxAllocator based on this stack */
universe@182 55 UcxAllocator allocator;
olaf@158 56
universe@182 57 /** Stack size. */
universe@182 58 size_t size;
universe@146 59
universe@182 60 /** Pointer to the bottom of the stack */
universe@182 61 char *space;
universe@146 62
universe@182 63 /** Pointer to the top of the stack */
universe@182 64 char *top;
universe@182 65 } UcxStack;
universe@135 66
universe@135 67 /**
universe@182 68 * Wraps memory in a new UcxStack structure.
universe@135 69 *
universe@182 70 * @param space the memory area that shall be managed
universe@182 71 * @param size size of the memory area
universe@182 72 * @return a new UcxStack structure
universe@135 73 */
universe@182 74 UcxStack ucx_stack_new(char* space, size_t size);
universe@135 75
universe@135 76 /**
universe@182 77 * Allocates stack memory.
universe@135 78 *
universe@182 79 * @param stack a pointer to the stack
universe@135 80 * @param n amount of memory to allocate
universe@135 81 * @return a pointer to the allocated memory
universe@135 82 * @see ucx_allocator_malloc()
universe@135 83 */
universe@182 84 void *ucx_stack_malloc(UcxStack *stack, size_t n);
universe@182 85
universe@135 86 /**
universe@182 87 * Alias for #ucx_stack_malloc().
universe@182 88 * @param stack a pointer to the stack
universe@182 89 * @param n amount of memory to allocate
universe@182 90 * @return a pointer to the allocated memory
universe@182 91 * @see ucx_stack_malloc
universe@182 92 */
universe@182 93 #define ucx_stack_push(s, n) ucx_stack_malloc(s, n)
universe@182 94
universe@182 95 /**
universe@182 96 * Allocates an array of stack memory
universe@135 97 *
universe@182 98 * The content of the allocated memory is set to zero.
universe@135 99 *
universe@182 100 * @param stack a pointer to the stack
universe@135 101 * @param nelem amount of elements to allocate
universe@135 102 * @param elsize amount of memory per element
universe@135 103 * @return a pointer to the allocated memory
universe@135 104 * @see ucx_allocator_calloc()
universe@135 105 */
universe@182 106 void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize);
universe@146 107
universe@135 108 /**
universe@182 109 * Alias for #ucx_stack_calloc().
universe@135 110 *
universe@182 111 * @param stack a pointer to the stack
universe@182 112 * @param nelem amount of elements to allocate
universe@182 113 * @param elsize amount of memory per element
universe@182 114 * @return a pointer to the allocated memory
universe@182 115 * @see ucx_stack_calloc
universe@182 116 */
universe@182 117 #define ucx_stack_pusharr(st,n,es) ucx_stack_calloc(st,n,es)
universe@182 118
universe@182 119 /**
universe@182 120 * Reallocates memory on the stack.
universe@141 121 *
universe@182 122 * Shrinking memory is always safe. Extending memory can be very expensive.
universe@182 123 *
universe@182 124 * @param stack the stack
universe@135 125 * @param ptr a pointer to the memory that shall be reallocated
universe@135 126 * @param n the new size of the memory
universe@182 127 * @return a pointer to the new location of the memory
universe@135 128 * @see ucx_allocator_realloc()
universe@135 129 */
universe@182 130 void *ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n);
universe@146 131
universe@135 132 /**
universe@182 133 * Frees memory on the stack.
universe@135 134 *
universe@182 135 * Freeing stack memory behaves in a special way.
universe@135 136 *
universe@182 137 * If the element, that should be freed, is the top most element of the stack,
universe@182 138 * it is removed from the stack. Otherwise it is marked as freed. Marked
universe@182 139 * elements are removed, when they become the top most elements of the stack.
universe@135 140 *
universe@182 141 * @param stack a pointer to the stack
universe@135 142 * @param ptr a pointer to the memory that shall be freed
universe@135 143 */
universe@182 144 void ucx_stack_free(UcxStack *stack, void *ptr);
universe@182 145
olaf@13 146
universe@135 147 /**
universe@182 148 * Returns the size of the top most element.
universe@182 149 * @param stack a pointer to the stack
universe@182 150 * @return the size of the top most element
universe@135 151 */
universe@182 152 #define ucx_stack_topsize(stack) (*(((size_t*)stack->top - 1))
olaf@13 153
universe@135 154 /**
universe@182 155 * Removes the top most element from the stack and copies the content to <code>
universe@182 156 * dest</code>, if specified.
universe@135 157 *
universe@182 158 * Use #ucx_stack_topsize()# to get the amount of memory that must be available
universe@182 159 * at the location of <code>dest</code>.
universe@135 160 *
universe@182 161 * @param stack a pointer to the stack
universe@182 162 * @param dest the location where the contents shall be written to, or <code>
universe@182 163 * NULL</code>, if the element shall only be removed.
universe@182 164 * @see ucx_stack_free
universe@182 165 * @see ucx_stack_popn
universe@135 166 */
universe@182 167 void ucx_stack_pop(UcxStack *stack, void *dest);
universe@135 168
universe@135 169 /**
universe@182 170 * Removes the top most element from the stack and copies the content to <code>
universe@182 171 * dest</code>.
universe@135 172 *
universe@182 173 * In contrast to #ucx_stack_pop() the <code>dest</code> pointer <code>MUST
universe@182 174 * NOT</code> be <code>NULL</code>.
universe@135 175 *
universe@182 176 * @param stack a pointer to the stack
universe@182 177 * @param dest the location where the contents shall be written to
universe@182 178 * @param n copies at most n elements to <code>dest</code>
universe@182 179 * @see ucx_stack_pop
universe@182 180 */
universe@182 181 void ucx_stack_popn(UcxStack *stack, void *dest, size_t n);
universe@182 182
universe@182 183 /**
universe@182 184 * Returns the remaining available memory on the specified stack.
universe@135 185 *
universe@182 186 * @param stack a pointer to the stack
universe@182 187 * @return the remaining available memory
universe@135 188 */
universe@182 189 #define ucx_stack_avail(stack) ((stack->size) - (s.top - s.space)\
universe@182 190 - sizeof(size_t))
universe@182 191
olaf@13 192
olaf@13 193 #ifdef __cplusplus
olaf@13 194 }
olaf@13 195 #endif
olaf@13 196
universe@182 197 #endif /* UCX_STACK_H */
olaf@13 198

mercurial