-
Notifications
You must be signed in to change notification settings - Fork 2
/
Lifo.c
225 lines (192 loc) · 5.44 KB
/
Lifo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Lifo.c - LIFO buffer library
*
* DESCRIPTION
* This module contains routines for managing LIFO buffers (i.e., stacks).
* The size of a LIFO buffer is increased automatically when needed, so
* it never becomes full. The size is however never decreased.
*
* INCLUDE FILES
* Lifo.h
*
* COPYRIGHT
* You are free to use, copy or modify this software at your own risk.
*
* AUTHOR
* Cornelis van der Bent. Please let me know if you have comments or find
* flaws: [email protected]. Enjoy!
*
* MODIFICATION HISTORY
* 1999/04/12 vdbent Thorough test and debugging; beta release.
* 1998/12/18 vdbent Conception.
*/
#include <stdlib.h>
#include "Lifo.h"
#include "Assert.h" /* includes "Except.h" which defines return() macro */
#define INIT_SIZE 32 /* initial size */
#define INCR_SIZE 32 /* size increment when not enough space */
/******************************************************************************
*
* LifoCreate - create LIFO buffer of unlimited size
*
* DESCRIPTION
* This routine creates an empty LIFO buffer. The size is increased when
* needed.
*
* SIDE EFFECTS
* None.
*
* RETURNS
* Pointer to LIFO buffer.
*/
Lifo * LifoCreate(void)
{
Lifo * pLifo;
pLifo = malloc(sizeof(Lifo));
pLifo->pObjects = malloc(INIT_SIZE * sizeof(void *));
pLifo->size = INIT_SIZE;
pLifo->pointer = 0;
return pLifo;
}
/******************************************************************************
*
* LifoDestroy - free LIFO buffer
*
* DESCRIPTION
* This routine frees the memory that is occupied by the LIFO buffer.
*
* SIDE EFFECTS
* None.
*
* RETURNS
* N/A.
*/
void LifoDestroy(
Lifo * pLifo) /* pointer to LIFO buffer */
{
assert(pLifo != NULL);
free(pLifo->pObjects);
free(pLifo);
}
/******************************************************************************
*
* LifoDestroyData - free LIFO buffer including user data
*
* DESCRIPTION
* This routine frees the memory that is occupied by the LIFO buffer. The
* user data objects are also freed using free(); the caller is responsi-
* ble that all objects were allocated using routines compatible with
* free().
*
* SIDE EFFECTS
* None.
*
* RETURNS
* N/A.
*/
void LifoDestroyData(
Lifo * pLifo) /* pointer to LIFO buffer */
{
assert(pLifo != NULL);
while(pLifo->pointer > 0)
free(pLifo->pObjects[pLifo->pointer--]);
free(pLifo->pObjects);
free(pLifo);
}
/******************************************************************************
*
* LifoPush - push object to LIFO buffer
*
* DESCRIPTION
* This routine adds an object to the top of specified LIFO buffer.
*
* SIDE EFFECTS
* None.
*
* RETURNS
* N/A.
*/
void LifoPush(
Lifo * pLifo, /* pointer to LIFO buffer */
void * pObject) /* object being added */
{
assert(pLifo != NULL && pObject != NULL);
if (pLifo->pointer == pLifo->size) {
pLifo->size += INCR_SIZE;
pLifo->pObjects = realloc(pLifo->pObjects,
pLifo->size * sizeof(void *));
}
pLifo->pObjects[pLifo->pointer++] = pObject;
}
/******************************************************************************
*
* LifoPop - pop object from LIFO buffer
*
* DESCRIPTION
* This routine removes an object from the top of the specified LIFO
* buffer.
*
* It is illegal to perform this operation on an empty LIFO buffer (will
* result in failed assertion when DEBUG defined, otherwise returns NULL).
*
* SIDE EFFECTS
* None.
*
* RETURNS
* Removed object, or NULL if LIFO buffer is empty.
*/
void * LifoPop(
Lifo * pLifo) /* pointer to LIFO buffer */
{
assert(pLifo != NULL);
validate(pLifo->pointer > 0, NULL);
return pLifo->pObjects[--pLifo->pointer];
}
/******************************************************************************
*
* LifoPeek - get specified LIFO buffer object
*
* DESCRIPTION
* This routine returns the N-th object from the specified LIFO buffer
* counting from the top (i.e., the first/top object is retrieved with
* number equal to 1). The object is not removed.
*
* It is illegal to perform this operation on an empty LIFO buffer (will
* result in failed assertion when DEBUG defined, otherwise returns NULL).
*
* SIDE EFFECTS
* None.
*
* RETURNS
* Specified object, or NULL if LIFO buffer is empty.
*/
void * LifoPeek(
Lifo * pLifo, /* pointer to LIFO buffer */
int number) /* object number to get (1: top) */
{
assert(pLifo != NULL);
validate(pLifo->pointer > 0, NULL);
validate(number > 0 && number <= pLifo->pointer, NULL);
return pLifo->pObjects[pLifo->pointer - number];
}
/******************************************************************************
*
* LifoCount - get number of objects in LIFO buffer
*
* DESCRIPTION
* This routine returns the number of objects in the specified LIFO
* buffer.
*
* SIDE EFFECTS
* None.
*
* RETURNS
* Number of objects in LIFO buffer.
*/
int LifoCount(
Lifo * pLifo) /* pointer to LIFO buffer */
{
assert(pLifo != NULL);
return pLifo->pointer;
}
/* end of Lifo.c */