Sun, 13 May 2018 17:34:06 +0200
documentation for the UcxStack
universe@103 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
olaf@13 | 3 | * |
universe@259 | 4 | * Copyright 2017 Mike Becker, 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@259 | 41 | #include "ucx.h" |
universe@259 | 42 | #include "allocator.h" |
universe@38 | 43 | |
olaf@13 | 44 | #ifdef __cplusplus |
olaf@13 | 45 | extern "C" { |
olaf@13 | 46 | #endif |
olaf@13 | 47 | |
olaf@13 | 48 | |
universe@135 | 49 | /** |
universe@182 | 50 | * UCX stack structure. |
universe@135 | 51 | */ |
olaf@13 | 52 | typedef struct { |
universe@182 | 53 | /** UcxAllocator based on this stack */ |
universe@182 | 54 | UcxAllocator allocator; |
olaf@158 | 55 | |
universe@182 | 56 | /** Stack size. */ |
universe@182 | 57 | size_t size; |
universe@146 | 58 | |
universe@182 | 59 | /** Pointer to the bottom of the stack */ |
universe@182 | 60 | char *space; |
universe@146 | 61 | |
universe@182 | 62 | /** Pointer to the top of the stack */ |
universe@182 | 63 | char *top; |
universe@182 | 64 | } UcxStack; |
universe@135 | 65 | |
universe@135 | 66 | /** |
universe@185 | 67 | * Metadata for each UCX stack element. |
universe@185 | 68 | */ |
universe@185 | 69 | struct ucx_stack_metadata { |
universe@185 | 70 | /** |
universe@185 | 71 | * Location of the previous element (<code>NULL</code> if this is the first) |
universe@185 | 72 | */ |
universe@185 | 73 | char *prev; |
universe@185 | 74 | |
universe@185 | 75 | /** Size of this element */ |
universe@185 | 76 | size_t size; |
universe@185 | 77 | }; |
universe@185 | 78 | |
universe@185 | 79 | /** |
universe@185 | 80 | * Initializes UcxStack structure with memory. |
universe@135 | 81 | * |
universe@185 | 82 | * @param stack a pointer to an uninitialized stack structure |
universe@182 | 83 | * @param space the memory area that shall be managed |
universe@182 | 84 | * @param size size of the memory area |
universe@182 | 85 | * @return a new UcxStack structure |
universe@135 | 86 | */ |
universe@185 | 87 | void ucx_stack_init(UcxStack *stack, char* space, size_t size); |
universe@135 | 88 | |
universe@135 | 89 | /** |
universe@182 | 90 | * Allocates stack memory. |
universe@135 | 91 | * |
universe@182 | 92 | * @param stack a pointer to the stack |
universe@135 | 93 | * @param n amount of memory to allocate |
universe@301 | 94 | * @return a pointer to the allocated memory or <code>NULL</code> on stack |
universe@301 | 95 | * overflow |
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@301 | 101 | * Allocates memory with #ucx_stack_malloc() and copies the specified data if |
universe@301 | 102 | * the allocation was successful. |
universe@301 | 103 | * |
universe@182 | 104 | * @param stack a pointer to the stack |
universe@182 | 105 | * @param n amount of memory to allocate |
universe@301 | 106 | * @param data a pointer to the data to copy |
universe@182 | 107 | * @return a pointer to the allocated memory |
universe@182 | 108 | * @see ucx_stack_malloc |
universe@182 | 109 | */ |
universe@301 | 110 | void *ucx_stack_push(UcxStack *stack, size_t n, const void *data); |
universe@182 | 111 | |
universe@182 | 112 | /** |
universe@182 | 113 | * Allocates an array of stack memory |
universe@135 | 114 | * |
universe@182 | 115 | * The content of the allocated memory is set to zero. |
universe@135 | 116 | * |
universe@182 | 117 | * @param stack a pointer to the stack |
universe@135 | 118 | * @param nelem amount of elements to allocate |
universe@135 | 119 | * @param elsize amount of memory per element |
universe@135 | 120 | * @return a pointer to the allocated memory |
universe@135 | 121 | * @see ucx_allocator_calloc() |
universe@135 | 122 | */ |
universe@182 | 123 | void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize); |
universe@146 | 124 | |
universe@135 | 125 | /** |
universe@301 | 126 | * Allocates memory with #ucx_stack_calloc() and copies the specified data if |
universe@301 | 127 | * the allocation was successful. |
universe@135 | 128 | * |
universe@182 | 129 | * @param stack a pointer to the stack |
universe@301 | 130 | * @param nelem amount of elements to allocate |
universe@182 | 131 | * @param elsize amount of memory per element |
universe@301 | 132 | * @param data a pointer to the data |
universe@182 | 133 | * @return a pointer to the allocated memory |
universe@182 | 134 | * @see ucx_stack_calloc |
universe@182 | 135 | */ |
universe@301 | 136 | void *ucx_stack_pusharr(UcxStack *stack, |
universe@301 | 137 | size_t nelem, size_t elsize, const void *data); |
universe@182 | 138 | |
universe@182 | 139 | /** |
universe@182 | 140 | * Reallocates memory on the stack. |
universe@141 | 141 | * |
universe@182 | 142 | * Shrinking memory is always safe. Extending memory can be very expensive. |
universe@182 | 143 | * |
universe@182 | 144 | * @param stack the stack |
universe@135 | 145 | * @param ptr a pointer to the memory that shall be reallocated |
universe@135 | 146 | * @param n the new size of the memory |
universe@182 | 147 | * @return a pointer to the new location of the memory |
universe@135 | 148 | * @see ucx_allocator_realloc() |
universe@135 | 149 | */ |
universe@182 | 150 | void *ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n); |
universe@146 | 151 | |
universe@135 | 152 | /** |
universe@182 | 153 | * Frees memory on the stack. |
universe@135 | 154 | * |
universe@182 | 155 | * Freeing stack memory behaves in a special way. |
universe@135 | 156 | * |
universe@182 | 157 | * If the element, that should be freed, is the top most element of the stack, |
universe@182 | 158 | * it is removed from the stack. Otherwise it is marked as freed. Marked |
universe@182 | 159 | * elements are removed, when they become the top most elements of the stack. |
universe@135 | 160 | * |
universe@182 | 161 | * @param stack a pointer to the stack |
universe@135 | 162 | * @param ptr a pointer to the memory that shall be freed |
universe@135 | 163 | */ |
universe@182 | 164 | void ucx_stack_free(UcxStack *stack, void *ptr); |
universe@182 | 165 | |
olaf@13 | 166 | |
universe@135 | 167 | /** |
universe@182 | 168 | * Returns the size of the top most element. |
universe@182 | 169 | * @param stack a pointer to the stack |
universe@182 | 170 | * @return the size of the top most element |
universe@135 | 171 | */ |
universe@185 | 172 | #define ucx_stack_topsize(stack) ((stack)->top ? ((struct ucx_stack_metadata*)\ |
universe@185 | 173 | (stack)->top - 1)->size : 0) |
olaf@13 | 174 | |
universe@135 | 175 | /** |
universe@182 | 176 | * Removes the top most element from the stack and copies the content to <code> |
universe@182 | 177 | * dest</code>, if specified. |
universe@135 | 178 | * |
universe@182 | 179 | * Use #ucx_stack_topsize()# to get the amount of memory that must be available |
universe@182 | 180 | * at the location of <code>dest</code>. |
universe@135 | 181 | * |
universe@182 | 182 | * @param stack a pointer to the stack |
universe@182 | 183 | * @param dest the location where the contents shall be written to, or <code> |
universe@182 | 184 | * NULL</code>, if the element shall only be removed. |
universe@182 | 185 | * @see ucx_stack_free |
universe@182 | 186 | * @see ucx_stack_popn |
universe@135 | 187 | */ |
universe@217 | 188 | #define ucx_stack_pop(stack, dest) ucx_stack_popn(stack, dest, (size_t)-1) |
universe@135 | 189 | |
universe@135 | 190 | /** |
universe@182 | 191 | * Removes the top most element from the stack and copies the content to <code> |
universe@182 | 192 | * dest</code>. |
universe@135 | 193 | * |
universe@301 | 194 | * This function copies at most <code>n</code> bytes to the destination, but |
universe@301 | 195 | * the element is always freed as a whole. |
universe@301 | 196 | * If the element was larger than <code>n</code>, the remaining data is lost. |
universe@135 | 197 | * |
universe@182 | 198 | * @param stack a pointer to the stack |
universe@182 | 199 | * @param dest the location where the contents shall be written to |
universe@301 | 200 | * @param n copies at most n bytes to <code>dest</code> |
universe@182 | 201 | * @see ucx_stack_pop |
universe@182 | 202 | */ |
universe@182 | 203 | void ucx_stack_popn(UcxStack *stack, void *dest, size_t n); |
universe@182 | 204 | |
universe@182 | 205 | /** |
universe@182 | 206 | * Returns the remaining available memory on the specified stack. |
universe@135 | 207 | * |
universe@182 | 208 | * @param stack a pointer to the stack |
universe@182 | 209 | * @return the remaining available memory |
universe@135 | 210 | */ |
universe@185 | 211 | size_t ucx_stack_avail(UcxStack *stack); |
universe@185 | 212 | |
universe@185 | 213 | /** |
universe@185 | 214 | * Checks, if the stack is empty. |
universe@185 | 215 | * |
universe@185 | 216 | * @param stack a pointer to the stack |
universe@185 | 217 | * @return nonzero, if the stack is empty, zero otherwise |
universe@185 | 218 | */ |
universe@185 | 219 | #define ucx_stack_empty(stack) (!(stack)->top) |
universe@185 | 220 | |
universe@185 | 221 | /** |
universe@185 | 222 | * Computes a recommended size for the stack memory area. Note, that |
universe@185 | 223 | * reallocations have not been taken into account, so you might need to reserve |
universe@185 | 224 | * twice as much memory to allow many reallocations. |
universe@185 | 225 | * |
universe@185 | 226 | * @param size the approximate payload |
universe@185 | 227 | * @param elems the approximate count of element allocations |
universe@185 | 228 | * @return a recommended size for the stack space based on the information |
universe@185 | 229 | * provided |
universe@185 | 230 | */ |
universe@185 | 231 | #define ucx_stack_dim(size, elems) (size+sizeof(struct ucx_stack_metadata) * \ |
universe@185 | 232 | (elems + 1)) |
universe@182 | 233 | |
olaf@13 | 234 | |
olaf@13 | 235 | #ifdef __cplusplus |
olaf@13 | 236 | } |
olaf@13 | 237 | #endif |
olaf@13 | 238 | |
universe@182 | 239 | #endif /* UCX_STACK_H */ |
olaf@13 | 240 |