[169] | 1 | /* |
---|
| 2 | * Copyright (C) 2005,2007,2009-2010 Timothy D. Morgan |
---|
[168] | 3 | * |
---|
[31] | 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 253 2011-06-13 02:27:42Z tim $ |
---|
| 18 | */ |
---|
| 19 | |
---|
[169] | 20 | /** |
---|
| 21 | *@file |
---|
| 22 | * |
---|
| 23 | * This is a very simple implementation of a stack which stores chunks of |
---|
| 24 | * memory of any type. |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | |
---|
[150] | 28 | #ifndef _VOID_STACK_H |
---|
| 29 | #define _VOID_STACK_H |
---|
| 30 | |
---|
[31] | 31 | #include <stdlib.h> |
---|
| 32 | #include <stdbool.h> |
---|
| 33 | #include <string.h> |
---|
[201] | 34 | #include <talloc.h> |
---|
[31] | 35 | |
---|
[253] | 36 | #include "compat.h" |
---|
[201] | 37 | |
---|
[169] | 38 | /** XXX: document this. */ |
---|
[31] | 39 | typedef struct _void_stack |
---|
| 40 | { |
---|
| 41 | void** elements; |
---|
| 42 | unsigned short max_size; |
---|
| 43 | unsigned short top; |
---|
| 44 | } void_stack; |
---|
| 45 | |
---|
[169] | 46 | |
---|
| 47 | /** XXX: document this. */ |
---|
[31] | 48 | typedef struct _void_stack_iterator |
---|
| 49 | { |
---|
[89] | 50 | const void_stack* stack; |
---|
[31] | 51 | unsigned short cur; |
---|
| 52 | } void_stack_iterator; |
---|
| 53 | |
---|
| 54 | |
---|
[169] | 55 | /** Allocates a new void_stack. |
---|
[89] | 56 | * |
---|
[169] | 57 | * @param max_size the maxiumum number of elements |
---|
| 58 | * which may be pushed onto the stack. |
---|
[89] | 59 | * |
---|
[169] | 60 | * @return a pointer to the newly allocated void_stack, |
---|
| 61 | * or NULL if an error occurred. |
---|
[89] | 62 | */ |
---|
[253] | 63 | _EXPORT() |
---|
[31] | 64 | void_stack* void_stack_new(unsigned short max_size); |
---|
[89] | 65 | |
---|
| 66 | |
---|
[169] | 67 | /** Makes a shallow copy of void_stack. |
---|
[89] | 68 | * |
---|
[169] | 69 | * @param v the stack to make a copy of. |
---|
[89] | 70 | * |
---|
[169] | 71 | * @return a pointer to the duplicate void_stack, or NULL if an error occurred. |
---|
[89] | 72 | */ |
---|
[253] | 73 | _EXPORT() |
---|
[38] | 74 | void_stack* void_stack_copy(const void_stack* v); |
---|
[89] | 75 | |
---|
| 76 | |
---|
[169] | 77 | /** Makes a shallow copy of void_stack in reverse order. |
---|
[89] | 78 | * |
---|
[169] | 79 | * @param v the stack to make a copy of. |
---|
[89] | 80 | * |
---|
[169] | 81 | * @return a pointer to the duplicate void_stack |
---|
| 82 | * (which will be in reverse order), or NULL if an error occurred. |
---|
[89] | 83 | */ |
---|
[253] | 84 | _EXPORT() |
---|
[38] | 85 | void_stack* void_stack_copy_reverse(const void_stack* v); |
---|
[89] | 86 | |
---|
| 87 | |
---|
[169] | 88 | /** Frees the memory associated with a void_stack, but not the elements held |
---|
[89] | 89 | * on the stack. |
---|
| 90 | * |
---|
[169] | 91 | * @param stack the stack to be free()d. |
---|
[89] | 92 | */ |
---|
[253] | 93 | _EXPORT() |
---|
[80] | 94 | void void_stack_free(void_stack* stack); |
---|
[89] | 95 | |
---|
| 96 | |
---|
[169] | 97 | /** Frees the memory associated with a void_stack and the elements referenced |
---|
| 98 | * by the stack. |
---|
[89] | 99 | * |
---|
[169] | 100 | * Do not use this function if the elements of the stack |
---|
| 101 | * are also free()d elsewhere, or contain pointers to other memory which |
---|
| 102 | * cannot be otherwise free()d. |
---|
| 103 | * |
---|
| 104 | * @param stack the stack to be free()d. |
---|
[89] | 105 | */ |
---|
[253] | 106 | _EXPORT() |
---|
[80] | 107 | void void_stack_free_deep(void_stack* stack); |
---|
[89] | 108 | |
---|
| 109 | |
---|
[169] | 110 | /** Query the current number of elements on a void_stack() |
---|
[89] | 111 | * |
---|
[169] | 112 | * @param stack the void_stack to query |
---|
[89] | 113 | * |
---|
[169] | 114 | * @return the number of elements currently on the stack. |
---|
[89] | 115 | */ |
---|
[253] | 116 | _EXPORT() |
---|
[89] | 117 | unsigned short void_stack_size(const void_stack* stack); |
---|
| 118 | |
---|
| 119 | |
---|
[169] | 120 | /** Removes the top element on a void_stack and returns a reference to it. |
---|
[89] | 121 | * |
---|
[169] | 122 | * @param stack the void_stack to pop |
---|
[89] | 123 | * |
---|
[169] | 124 | * @return a pointer to the popped stack element, or NULL if no elements exist |
---|
| 125 | * on the stack. |
---|
[89] | 126 | */ |
---|
[253] | 127 | _EXPORT() |
---|
[31] | 128 | void* void_stack_pop(void_stack* stack); |
---|
[89] | 129 | |
---|
| 130 | |
---|
[169] | 131 | /** Puts a new element on the top of a void_stack. |
---|
[89] | 132 | * |
---|
[169] | 133 | * @param stack the void_stack being modified. |
---|
| 134 | * @param e the element to be added |
---|
[89] | 135 | * |
---|
[169] | 136 | * @return true if the element was successfully added, false otherwise. |
---|
[89] | 137 | */ |
---|
[253] | 138 | _EXPORT() |
---|
[31] | 139 | bool void_stack_push(void_stack* stack, void* e); |
---|
[89] | 140 | |
---|
| 141 | |
---|
[169] | 142 | /** Returns a pointer to the current element on the top of the stack. |
---|
[89] | 143 | * |
---|
[169] | 144 | * @param stack the void_stack being queried. |
---|
[89] | 145 | * |
---|
[169] | 146 | * @return a pointer to the current element on the top of the stack, or NULL if |
---|
| 147 | * no elements exist in the stack. |
---|
[89] | 148 | */ |
---|
[253] | 149 | _EXPORT() |
---|
[89] | 150 | const void* void_stack_cur(const void_stack* stack); |
---|
| 151 | |
---|
| 152 | |
---|
[169] | 153 | /** Creates a new iterator for the specified void_stack. |
---|
[89] | 154 | * |
---|
[169] | 155 | * @param stack the void_stack to be referenced by the new iterator |
---|
[89] | 156 | * |
---|
[169] | 157 | * @return a new void_stack_iterator, or NULL if an error occurred. |
---|
[89] | 158 | */ |
---|
[253] | 159 | _EXPORT() |
---|
[89] | 160 | void_stack_iterator* void_stack_iterator_new(const void_stack* stack); |
---|
| 161 | |
---|
| 162 | |
---|
[169] | 163 | /** Frees a void_stack_iterator. |
---|
[89] | 164 | * |
---|
[169] | 165 | * Does not affect the void_stack referenced by the iterator. |
---|
| 166 | * |
---|
| 167 | * @param iter the void_stack_iterator to be free()d. |
---|
[89] | 168 | */ |
---|
[253] | 169 | _EXPORT() |
---|
[80] | 170 | void void_stack_iterator_free(void_stack_iterator* iter); |
---|
[89] | 171 | |
---|
| 172 | |
---|
[169] | 173 | /** Returns a pointer to the the next element in the stack. |
---|
[89] | 174 | * |
---|
[169] | 175 | * Iterates over elements starting in order from the oldest element (bottom of the stack). |
---|
[89] | 176 | * |
---|
[169] | 177 | * @param iter the void_stack_iterator used to lookup the next element. |
---|
| 178 | * |
---|
| 179 | * @return a pointer to the next element. |
---|
[89] | 180 | */ |
---|
[253] | 181 | _EXPORT() |
---|
[33] | 182 | const void* void_stack_iterator_next(void_stack_iterator* iter); |
---|
[79] | 183 | |
---|
[89] | 184 | |
---|
| 185 | /* XXX: for completeness, might want to add a void_stack_iterator_first() |
---|
| 186 | * function, to return iterator to first element |
---|
| 187 | */ |
---|
[79] | 188 | #endif |
---|