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