ucx/stack.h

Sun, 17 May 2015 17:31:32 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 17 May 2015 17:31:32 +0200
changeset 192
1e51558b9d09
parent 188
63f87e2884c1
child 217
e056e4e0b08e
permissions
-rw-r--r--

updated copyright notice + added files for upcoming AVL tree implementation

universe@103 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
olaf@13 3 *
universe@192 4 * Copyright 2015 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@185 42 #include <stdint.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@185 68 * Metadata for each UCX stack element.
universe@185 69 */
universe@185 70 struct ucx_stack_metadata {
universe@185 71 /**
universe@185 72 * Location of the previous element (<code>NULL</code> if this is the first)
universe@185 73 */
universe@185 74 char *prev;
universe@185 75
universe@185 76 /** Size of this element */
universe@185 77 size_t size;
universe@185 78 };
universe@185 79
universe@185 80 /**
universe@185 81 * Initializes UcxStack structure with memory.
universe@135 82 *
universe@185 83 * @param stack a pointer to an uninitialized stack structure
universe@182 84 * @param space the memory area that shall be managed
universe@182 85 * @param size size of the memory area
universe@182 86 * @return a new UcxStack structure
universe@135 87 */
universe@185 88 void ucx_stack_init(UcxStack *stack, char* space, size_t size);
universe@135 89
universe@135 90 /**
universe@182 91 * Allocates stack memory.
universe@135 92 *
universe@182 93 * @param stack a pointer to the stack
universe@135 94 * @param n amount of memory to allocate
universe@135 95 * @return a pointer to the allocated memory
universe@135 96 * @see ucx_allocator_malloc()
universe@135 97 */
universe@182 98 void *ucx_stack_malloc(UcxStack *stack, size_t n);
universe@182 99
universe@135 100 /**
universe@182 101 * Alias for #ucx_stack_malloc().
universe@182 102 * @param stack a pointer to the stack
universe@182 103 * @param n amount of memory to allocate
universe@182 104 * @return a pointer to the allocated memory
universe@182 105 * @see ucx_stack_malloc
universe@182 106 */
universe@188 107 #define ucx_stack_push(stack, n) ucx_stack_malloc(stack, n)
universe@182 108
universe@182 109 /**
universe@182 110 * Allocates an array of stack memory
universe@135 111 *
universe@182 112 * The content of the allocated memory is set to zero.
universe@135 113 *
universe@182 114 * @param stack a pointer to the stack
universe@135 115 * @param nelem amount of elements to allocate
universe@135 116 * @param elsize amount of memory per element
universe@135 117 * @return a pointer to the allocated memory
universe@135 118 * @see ucx_allocator_calloc()
universe@135 119 */
universe@182 120 void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize);
universe@146 121
universe@135 122 /**
universe@182 123 * Alias for #ucx_stack_calloc().
universe@135 124 *
universe@182 125 * @param stack a pointer to the stack
universe@188 126 * @param n amount of elements to allocate
universe@182 127 * @param elsize amount of memory per element
universe@182 128 * @return a pointer to the allocated memory
universe@182 129 * @see ucx_stack_calloc
universe@182 130 */
universe@188 131 #define ucx_stack_pusharr(stack,n,elsize) ucx_stack_calloc(stack,n,elssize)
universe@182 132
universe@182 133 /**
universe@182 134 * Reallocates memory on the stack.
universe@141 135 *
universe@182 136 * Shrinking memory is always safe. Extending memory can be very expensive.
universe@182 137 *
universe@182 138 * @param stack the stack
universe@135 139 * @param ptr a pointer to the memory that shall be reallocated
universe@135 140 * @param n the new size of the memory
universe@182 141 * @return a pointer to the new location of the memory
universe@135 142 * @see ucx_allocator_realloc()
universe@135 143 */
universe@182 144 void *ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n);
universe@146 145
universe@135 146 /**
universe@182 147 * Frees memory on the stack.
universe@135 148 *
universe@182 149 * Freeing stack memory behaves in a special way.
universe@135 150 *
universe@182 151 * If the element, that should be freed, is the top most element of the stack,
universe@182 152 * it is removed from the stack. Otherwise it is marked as freed. Marked
universe@182 153 * elements are removed, when they become the top most elements of the stack.
universe@135 154 *
universe@182 155 * @param stack a pointer to the stack
universe@135 156 * @param ptr a pointer to the memory that shall be freed
universe@135 157 */
universe@182 158 void ucx_stack_free(UcxStack *stack, void *ptr);
universe@182 159
olaf@13 160
universe@135 161 /**
universe@182 162 * Returns the size of the top most element.
universe@182 163 * @param stack a pointer to the stack
universe@182 164 * @return the size of the top most element
universe@135 165 */
universe@185 166 #define ucx_stack_topsize(stack) ((stack)->top ? ((struct ucx_stack_metadata*)\
universe@185 167 (stack)->top - 1)->size : 0)
olaf@13 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>, if specified.
universe@135 172 *
universe@182 173 * Use #ucx_stack_topsize()# to get the amount of memory that must be available
universe@182 174 * at the location of <code>dest</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, or <code>
universe@182 178 * NULL</code>, if the element shall only be removed.
universe@182 179 * @see ucx_stack_free
universe@182 180 * @see ucx_stack_popn
universe@135 181 */
universe@185 182 #define ucx_stack_pop(stack, dest) ucx_stack_popn(stack, dest, SIZE_MAX)
universe@135 183
universe@135 184 /**
universe@182 185 * Removes the top most element from the stack and copies the content to <code>
universe@182 186 * dest</code>.
universe@135 187 *
universe@182 188 * In contrast to #ucx_stack_pop() the <code>dest</code> pointer <code>MUST
universe@182 189 * NOT</code> be <code>NULL</code>.
universe@135 190 *
universe@182 191 * @param stack a pointer to the stack
universe@182 192 * @param dest the location where the contents shall be written to
universe@182 193 * @param n copies at most n elements to <code>dest</code>
universe@182 194 * @see ucx_stack_pop
universe@182 195 */
universe@182 196 void ucx_stack_popn(UcxStack *stack, void *dest, size_t n);
universe@182 197
universe@182 198 /**
universe@182 199 * Returns the remaining available memory on the specified stack.
universe@135 200 *
universe@182 201 * @param stack a pointer to the stack
universe@182 202 * @return the remaining available memory
universe@135 203 */
universe@185 204 size_t ucx_stack_avail(UcxStack *stack);
universe@185 205
universe@185 206 /**
universe@185 207 * Checks, if the stack is empty.
universe@185 208 *
universe@185 209 * @param stack a pointer to the stack
universe@185 210 * @return nonzero, if the stack is empty, zero otherwise
universe@185 211 */
universe@185 212 #define ucx_stack_empty(stack) (!(stack)->top)
universe@185 213
universe@185 214 /**
universe@185 215 * Computes a recommended size for the stack memory area. Note, that
universe@185 216 * reallocations have not been taken into account, so you might need to reserve
universe@185 217 * twice as much memory to allow many reallocations.
universe@185 218 *
universe@185 219 * @param size the approximate payload
universe@185 220 * @param elems the approximate count of element allocations
universe@185 221 * @return a recommended size for the stack space based on the information
universe@185 222 * provided
universe@185 223 */
universe@185 224 #define ucx_stack_dim(size, elems) (size+sizeof(struct ucx_stack_metadata) * \
universe@185 225 (elems + 1))
universe@182 226
olaf@13 227
olaf@13 228 #ifdef __cplusplus
olaf@13 229 }
olaf@13 230 #endif
olaf@13 231
universe@182 232 #endif /* UCX_STACK_H */
olaf@13 233

mercurial