[31] | 1 | /* |
---|
[150] | 2 | * Copyright (C) 2005,2007,2009 Timothy D. Morgan |
---|
[31] | 3 | * |
---|
| 4 | * This program is free software; you can redistribute it and/or modify |
---|
| 5 | * it under the terms of the GNU General Public License as published by |
---|
[111] | 6 | * the Free Software Foundation; version 3 of the License. |
---|
[31] | 7 | * |
---|
| 8 | * This program is distributed in the hope that it will be useful, |
---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 11 | * GNU General Public License for more details. |
---|
| 12 | * |
---|
| 13 | * You should have received a copy of the GNU General Public License |
---|
| 14 | * along with this program; if not, write to the Free Software |
---|
| 15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 16 | * |
---|
| 17 | * $Id: void_stack.h 150 2009-03-02 02:17:46Z tim $ |
---|
| 18 | */ |
---|
| 19 | |
---|
[150] | 20 | #ifndef _VOID_STACK_H |
---|
| 21 | #define _VOID_STACK_H |
---|
| 22 | |
---|
[31] | 23 | #include <stdlib.h> |
---|
| 24 | #include <stdbool.h> |
---|
| 25 | #include <string.h> |
---|
[150] | 26 | #include "talloc.h" |
---|
[31] | 27 | |
---|
| 28 | typedef struct _void_stack |
---|
| 29 | { |
---|
| 30 | void** elements; |
---|
| 31 | unsigned short max_size; |
---|
| 32 | unsigned short top; |
---|
| 33 | } void_stack; |
---|
| 34 | |
---|
| 35 | typedef struct _void_stack_iterator |
---|
| 36 | { |
---|
[89] | 37 | const void_stack* stack; |
---|
[31] | 38 | unsigned short cur; |
---|
| 39 | } void_stack_iterator; |
---|
| 40 | |
---|
| 41 | |
---|
[89] | 42 | /* void_stack_new(): |
---|
| 43 | * Allocates a new void_stack. |
---|
| 44 | * |
---|
| 45 | * Arguments: |
---|
| 46 | * max_size -- the maxiumum number of elements |
---|
| 47 | * which may be pushed onto the stack. |
---|
| 48 | * |
---|
| 49 | * Returns: |
---|
| 50 | * a pointer to the newly allocated void_stack, or NULL if an error occurred. |
---|
| 51 | */ |
---|
[31] | 52 | void_stack* void_stack_new(unsigned short max_size); |
---|
[89] | 53 | |
---|
| 54 | |
---|
| 55 | /* void_stack_copy(): |
---|
| 56 | * Makes a shallow copy of void_stack. |
---|
| 57 | * |
---|
| 58 | * Arguments: |
---|
| 59 | * v -- the stack to make a copy of. |
---|
| 60 | * |
---|
| 61 | * Returns: |
---|
| 62 | * a pointer to the duplicate void_stack, or NULL If an error occurred. |
---|
| 63 | */ |
---|
[38] | 64 | void_stack* void_stack_copy(const void_stack* v); |
---|
[89] | 65 | |
---|
| 66 | |
---|
| 67 | /* void_stack_copy_reverse(): |
---|
| 68 | * Makes a shallow copy of void_stack in reverse order. |
---|
| 69 | * |
---|
| 70 | * Arguments: |
---|
| 71 | * v -- the stack to make a copy of. |
---|
| 72 | * |
---|
| 73 | * Returns: |
---|
| 74 | * a pointer to the duplicate void_stack (which will be in reverse order), |
---|
| 75 | * or NULL If an error occurred. |
---|
| 76 | */ |
---|
[38] | 77 | void_stack* void_stack_copy_reverse(const void_stack* v); |
---|
[89] | 78 | |
---|
| 79 | |
---|
| 80 | /* void_stack_free(): |
---|
| 81 | * Frees the memory associated with a void_stack, but not the elements held |
---|
| 82 | * on the stack. |
---|
| 83 | * |
---|
| 84 | * Arguments: |
---|
| 85 | * stack -- the stack to be free()d. |
---|
| 86 | */ |
---|
[80] | 87 | void void_stack_free(void_stack* stack); |
---|
[89] | 88 | |
---|
| 89 | |
---|
| 90 | /* void_stack_free_deep(): |
---|
| 91 | * Frees the memory associated with a void_stack and the elements referenced |
---|
| 92 | * by the stack. Do not use this function if the elements of the stack |
---|
| 93 | * are also free()d elsewhere, or contain pointers to other memory which |
---|
| 94 | * cannot be otherwise free()d. |
---|
| 95 | * |
---|
| 96 | * Arguments: |
---|
| 97 | * stack -- the stack to be free()d. |
---|
| 98 | */ |
---|
[80] | 99 | void void_stack_free_deep(void_stack* stack); |
---|
[89] | 100 | |
---|
| 101 | |
---|
| 102 | /* void_stack_size(): |
---|
| 103 | * Query the current number of elements on a void_stack() |
---|
| 104 | * |
---|
| 105 | * Arguments: |
---|
| 106 | * stack -- the void_stack to query |
---|
| 107 | * |
---|
| 108 | * Returns: |
---|
| 109 | * the number of elements currently on the stack. |
---|
| 110 | */ |
---|
| 111 | unsigned short void_stack_size(const void_stack* stack); |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | /* void_stack_pop(): |
---|
| 115 | * Removes the top element on a void_stack and returns a reference to it. |
---|
| 116 | * |
---|
| 117 | * Arguments: |
---|
| 118 | * stack -- the void_stack to pop |
---|
| 119 | * |
---|
| 120 | * Returns: |
---|
| 121 | * a pointer to the popped stack element, or NULL if no elements exist on |
---|
| 122 | * the stack. |
---|
| 123 | */ |
---|
[31] | 124 | void* void_stack_pop(void_stack* stack); |
---|
[89] | 125 | |
---|
| 126 | |
---|
| 127 | /* void_stack_push(): |
---|
| 128 | * Puts a new element on the top of a void_stack. |
---|
| 129 | * |
---|
| 130 | * Arguments: |
---|
| 131 | * stack -- the void_stack being modified. |
---|
| 132 | * |
---|
| 133 | * e -- the element to be added |
---|
| 134 | * |
---|
| 135 | * Returns: |
---|
| 136 | * true if the element was successfully added, false otherwise. |
---|
| 137 | */ |
---|
[31] | 138 | bool void_stack_push(void_stack* stack, void* e); |
---|
[89] | 139 | |
---|
| 140 | |
---|
| 141 | /* void_stack_cur(): |
---|
| 142 | * Returns a pointer to the current element on the top of the stack. |
---|
| 143 | * |
---|
| 144 | * Arguments: |
---|
| 145 | * stack -- the void_stack being queried. |
---|
| 146 | * |
---|
| 147 | * Returns: |
---|
| 148 | * a pointer to the current element on the top of the stack, or NULL if no |
---|
| 149 | * elements exist in the stack. |
---|
| 150 | */ |
---|
| 151 | const void* void_stack_cur(const void_stack* stack); |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | /* void_stack_iterator_new(): |
---|
| 155 | * Creates a new iterator for the specified void_stack. |
---|
| 156 | * |
---|
| 157 | * Arguments: |
---|
| 158 | * stack -- the void_stack to be referenced by the new iterator |
---|
| 159 | * |
---|
| 160 | * Returns: |
---|
| 161 | * a new void_stack_iterator, or NULL if an error occurred. |
---|
| 162 | */ |
---|
| 163 | void_stack_iterator* void_stack_iterator_new(const void_stack* stack); |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | /* void_stack_iterator_free(): |
---|
| 167 | * Frees a void_stack_iterator. Does not affect the void_stack referenced |
---|
| 168 | * by the iterator. |
---|
| 169 | * |
---|
| 170 | * Arguments: |
---|
| 171 | * iter -- the void_stack_iterator to be free()d. |
---|
| 172 | */ |
---|
[80] | 173 | void void_stack_iterator_free(void_stack_iterator* iter); |
---|
[89] | 174 | |
---|
| 175 | |
---|
| 176 | /* void_stack_iterator_next(): |
---|
| 177 | * Returns a pointer to the the next element in the stack. Iterates over |
---|
| 178 | * elements starting in order from the oldest element (bottom of the stack). |
---|
| 179 | * |
---|
| 180 | * Arguments: |
---|
| 181 | * iter -- the void_stack_iterator used to lookup the next element. |
---|
| 182 | * |
---|
| 183 | * Returns: |
---|
| 184 | * a pointer to the next element. |
---|
| 185 | */ |
---|
[33] | 186 | const void* void_stack_iterator_next(void_stack_iterator* iter); |
---|
[79] | 187 | |
---|
[89] | 188 | |
---|
| 189 | /* XXX: for completeness, might want to add a void_stack_iterator_first() |
---|
| 190 | * function, to return iterator to first element |
---|
| 191 | */ |
---|
[79] | 192 | #endif |
---|