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