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