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