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