lst.c revision 1.70 1 1.70 rillig /* $NetBSD: lst.c,v 1.70 2020/09/24 08:23:29 rillig Exp $ */
2 1.1 rillig
3 1.1 rillig /*
4 1.1 rillig * Copyright (c) 1988, 1989, 1990, 1993
5 1.1 rillig * The Regents of the University of California. All rights reserved.
6 1.1 rillig *
7 1.1 rillig * This code is derived from software contributed to Berkeley by
8 1.1 rillig * Adam de Boor.
9 1.1 rillig *
10 1.1 rillig * Redistribution and use in source and binary forms, with or without
11 1.1 rillig * modification, are permitted provided that the following conditions
12 1.1 rillig * are met:
13 1.1 rillig * 1. Redistributions of source code must retain the above copyright
14 1.1 rillig * notice, this list of conditions and the following disclaimer.
15 1.1 rillig * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rillig * notice, this list of conditions and the following disclaimer in the
17 1.1 rillig * documentation and/or other materials provided with the distribution.
18 1.1 rillig * 3. Neither the name of the University nor the names of its contributors
19 1.1 rillig * may be used to endorse or promote products derived from this software
20 1.1 rillig * without specific prior written permission.
21 1.1 rillig *
22 1.1 rillig * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 rillig * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 rillig * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 rillig * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 rillig * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 rillig * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 rillig * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 rillig * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 rillig * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 rillig * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 rillig * SUCH DAMAGE.
33 1.1 rillig */
34 1.1 rillig
35 1.30 skrll #include <stdint.h>
36 1.8 rillig
37 1.19 rillig #include "make.h"
38 1.1 rillig
39 1.70 rillig MAKE_RCSID("$NetBSD: lst.c,v 1.70 2020/09/24 08:23:29 rillig Exp $");
40 1.1 rillig
41 1.22 rillig /* Allocate and initialize a list node.
42 1.22 rillig *
43 1.22 rillig * The fields 'prev' and 'next' must be initialized by the caller.
44 1.22 rillig */
45 1.65 rillig static ListNode *
46 1.12 rillig LstNodeNew(void *datum)
47 1.12 rillig {
48 1.65 rillig ListNode *node = bmake_malloc(sizeof *node);
49 1.70 rillig node->priv_useCount = 0;
50 1.70 rillig node->priv_deleted = FALSE;
51 1.16 rillig node->datum = datum;
52 1.16 rillig return node;
53 1.12 rillig }
54 1.12 rillig
55 1.2 rillig static Boolean
56 1.65 rillig LstIsEmpty(List *list)
57 1.2 rillig {
58 1.16 rillig return list->first == NULL;
59 1.2 rillig }
60 1.1 rillig
61 1.5 rillig /* Create and initialize a new, empty list. */
62 1.65 rillig List *
63 1.5 rillig Lst_Init(void)
64 1.1 rillig {
65 1.65 rillig List *list = bmake_malloc(sizeof *list);
66 1.1 rillig
67 1.16 rillig list->first = NULL;
68 1.16 rillig list->last = NULL;
69 1.70 rillig list->priv_isOpen = FALSE;
70 1.70 rillig list->priv_lastAccess = Unknown;
71 1.1 rillig
72 1.16 rillig return list;
73 1.1 rillig }
74 1.1 rillig
75 1.14 rillig /* Duplicate an entire list, usually by copying the datum pointers.
76 1.14 rillig * If copyProc is given, that function is used to create the new datum from the
77 1.35 rillig * old datum, usually by creating a copy of it. */
78 1.65 rillig List *
79 1.65 rillig Lst_Copy(List *list, LstCopyProc copyProc)
80 1.1 rillig {
81 1.65 rillig List *newList;
82 1.65 rillig ListNode *node;
83 1.1 rillig
84 1.52 rillig assert(list != NULL);
85 1.1 rillig
86 1.16 rillig newList = Lst_Init();
87 1.1 rillig
88 1.35 rillig for (node = list->first; node != NULL; node = node->next) {
89 1.35 rillig void *datum = copyProc != NULL ? copyProc(node->datum) : node->datum;
90 1.50 rillig Lst_Append(newList, datum);
91 1.1 rillig }
92 1.1 rillig
93 1.16 rillig return newList;
94 1.1 rillig }
95 1.1 rillig
96 1.42 rillig /* Free a list and all its nodes. The list data itself are not freed though. */
97 1.42 rillig void
98 1.65 rillig Lst_Free(List *list)
99 1.42 rillig {
100 1.65 rillig ListNode *node;
101 1.65 rillig ListNode *next;
102 1.42 rillig
103 1.52 rillig assert(list != NULL);
104 1.42 rillig
105 1.42 rillig for (node = list->first; node != NULL; node = next) {
106 1.42 rillig next = node->next;
107 1.42 rillig free(node);
108 1.42 rillig }
109 1.42 rillig
110 1.42 rillig free(list);
111 1.42 rillig }
112 1.42 rillig
113 1.59 rillig /* Destroy a list and free all its resources. The freeProc is called with the
114 1.59 rillig * datum from each node in turn before the node is freed. */
115 1.1 rillig void
116 1.65 rillig Lst_Destroy(List *list, LstFreeProc freeProc)
117 1.1 rillig {
118 1.65 rillig ListNode *node;
119 1.65 rillig ListNode *next;
120 1.1 rillig
121 1.52 rillig assert(list != NULL);
122 1.42 rillig assert(freeProc != NULL);
123 1.1 rillig
124 1.42 rillig for (node = list->first; node != NULL; node = next) {
125 1.42 rillig next = node->next;
126 1.42 rillig freeProc(node->datum);
127 1.42 rillig free(node);
128 1.1 rillig }
129 1.1 rillig
130 1.1 rillig free(list);
131 1.1 rillig }
132 1.1 rillig
133 1.1 rillig /*
134 1.1 rillig * Functions to modify a list
135 1.1 rillig */
136 1.1 rillig
137 1.14 rillig /* Insert a new node with the given piece of data before the given node in the
138 1.14 rillig * given list. */
139 1.26 rillig void
140 1.65 rillig Lst_InsertBefore(List *list, ListNode *node, void *datum)
141 1.26 rillig {
142 1.65 rillig ListNode *newNode;
143 1.26 rillig
144 1.52 rillig assert(list != NULL);
145 1.26 rillig assert(!LstIsEmpty(list));
146 1.52 rillig assert(node != NULL);
147 1.26 rillig assert(datum != NULL);
148 1.26 rillig
149 1.26 rillig newNode = LstNodeNew(datum);
150 1.26 rillig newNode->prev = node->prev;
151 1.26 rillig newNode->next = node;
152 1.26 rillig
153 1.26 rillig if (node->prev != NULL) {
154 1.26 rillig node->prev->next = newNode;
155 1.26 rillig }
156 1.26 rillig node->prev = newNode;
157 1.26 rillig
158 1.26 rillig if (node == list->first) {
159 1.26 rillig list->first = newNode;
160 1.26 rillig }
161 1.26 rillig }
162 1.26 rillig
163 1.22 rillig /* Add a piece of data at the start of the given list. */
164 1.22 rillig void
165 1.65 rillig Lst_Prepend(List *list, void *datum)
166 1.22 rillig {
167 1.65 rillig ListNode *node;
168 1.22 rillig
169 1.52 rillig assert(list != NULL);
170 1.22 rillig assert(datum != NULL);
171 1.22 rillig
172 1.22 rillig node = LstNodeNew(datum);
173 1.22 rillig node->prev = NULL;
174 1.22 rillig node->next = list->first;
175 1.22 rillig
176 1.22 rillig if (list->first == NULL) {
177 1.22 rillig list->first = node;
178 1.22 rillig list->last = node;
179 1.22 rillig } else {
180 1.22 rillig list->first->prev = node;
181 1.22 rillig list->first = node;
182 1.22 rillig }
183 1.22 rillig }
184 1.22 rillig
185 1.21 rillig /* Add a piece of data at the end of the given list. */
186 1.21 rillig void
187 1.65 rillig Lst_Append(List *list, void *datum)
188 1.21 rillig {
189 1.65 rillig ListNode *node;
190 1.21 rillig
191 1.52 rillig assert(list != NULL);
192 1.21 rillig assert(datum != NULL);
193 1.21 rillig
194 1.21 rillig node = LstNodeNew(datum);
195 1.21 rillig node->prev = list->last;
196 1.21 rillig node->next = NULL;
197 1.21 rillig
198 1.21 rillig if (list->last == NULL) {
199 1.21 rillig list->first = node;
200 1.21 rillig list->last = node;
201 1.21 rillig } else {
202 1.21 rillig list->last->next = node;
203 1.21 rillig list->last = node;
204 1.21 rillig }
205 1.21 rillig }
206 1.21 rillig
207 1.8 rillig /* Remove the given node from the given list.
208 1.8 rillig * The datum stored in the node must be freed by the caller, if necessary. */
209 1.8 rillig void
210 1.65 rillig Lst_Remove(List *list, ListNode *node)
211 1.1 rillig {
212 1.52 rillig assert(list != NULL);
213 1.52 rillig assert(node != NULL);
214 1.1 rillig
215 1.1 rillig /*
216 1.1 rillig * unlink it from the list
217 1.1 rillig */
218 1.16 rillig if (node->next != NULL) {
219 1.16 rillig node->next->prev = node->prev;
220 1.1 rillig }
221 1.16 rillig if (node->prev != NULL) {
222 1.16 rillig node->prev->next = node->next;
223 1.1 rillig }
224 1.1 rillig
225 1.1 rillig /*
226 1.15 rillig * if either the first or last of the list point to this node,
227 1.1 rillig * adjust them accordingly
228 1.1 rillig */
229 1.16 rillig if (list->first == node) {
230 1.16 rillig list->first = node->next;
231 1.1 rillig }
232 1.16 rillig if (list->last == node) {
233 1.16 rillig list->last = node->prev;
234 1.1 rillig }
235 1.1 rillig
236 1.1 rillig /*
237 1.1 rillig * Sequential access stuff. If the node we're removing is the current
238 1.1 rillig * node in the list, reset the current node to the previous one. If the
239 1.15 rillig * previous one was non-existent (prev == NULL), we set the
240 1.1 rillig * end to be Unknown, since it is.
241 1.1 rillig */
242 1.70 rillig if (list->priv_isOpen && list->priv_curr == node) {
243 1.70 rillig list->priv_curr = list->priv_prev;
244 1.70 rillig if (list->priv_curr == NULL) {
245 1.70 rillig list->priv_lastAccess = Unknown;
246 1.1 rillig }
247 1.1 rillig }
248 1.1 rillig
249 1.1 rillig /*
250 1.1 rillig * note that the datum is unmolested. The caller must free it as
251 1.1 rillig * necessary and as expected.
252 1.1 rillig */
253 1.70 rillig if (node->priv_useCount == 0) {
254 1.16 rillig free(node);
255 1.1 rillig } else {
256 1.70 rillig node->priv_deleted = TRUE;
257 1.1 rillig }
258 1.1 rillig }
259 1.1 rillig
260 1.8 rillig /* Replace the datum in the given node with the new datum. */
261 1.8 rillig void
262 1.65 rillig LstNode_Set(ListNode *node, void *datum)
263 1.1 rillig {
264 1.52 rillig assert(node != NULL);
265 1.37 rillig assert(datum != NULL);
266 1.37 rillig
267 1.16 rillig node->datum = datum;
268 1.1 rillig }
269 1.1 rillig
270 1.37 rillig /* Replace the datum in the given node to NULL. */
271 1.37 rillig void
272 1.65 rillig LstNode_SetNull(ListNode *node)
273 1.37 rillig {
274 1.52 rillig assert(node != NULL);
275 1.37 rillig
276 1.37 rillig node->datum = NULL;
277 1.37 rillig }
278 1.37 rillig
279 1.1 rillig
280 1.1 rillig /*
281 1.1 rillig * Functions for entire lists
282 1.1 rillig */
283 1.1 rillig
284 1.53 rillig /* Return the first node from the list for which the match function returns
285 1.53 rillig * TRUE, or NULL if none of the nodes matched. */
286 1.65 rillig ListNode *
287 1.65 rillig Lst_Find(List *list, LstFindProc match, const void *matchArgs)
288 1.53 rillig {
289 1.55 rillig return Lst_FindFrom(list, Lst_First(list), match, matchArgs);
290 1.53 rillig }
291 1.53 rillig
292 1.53 rillig /* Return the first node from the list, starting at the given node, for which
293 1.53 rillig * the match function returns TRUE, or NULL if none of the nodes matches.
294 1.53 rillig *
295 1.53 rillig * The start node may be NULL, in which case nothing is found. This allows
296 1.56 rillig * for passing Lst_First or LstNode_Next as the start node. */
297 1.65 rillig ListNode *
298 1.65 rillig Lst_FindFrom(List *list, ListNode *node, LstFindProc match, const void *matchArgs)
299 1.53 rillig {
300 1.65 rillig ListNode *tln;
301 1.53 rillig
302 1.53 rillig assert(list != NULL);
303 1.53 rillig assert(match != NULL);
304 1.53 rillig
305 1.53 rillig for (tln = node; tln != NULL; tln = tln->next) {
306 1.53 rillig if (match(tln->datum, matchArgs))
307 1.53 rillig return tln;
308 1.53 rillig }
309 1.53 rillig
310 1.53 rillig return NULL;
311 1.53 rillig }
312 1.53 rillig
313 1.14 rillig /* Return the first node that contains the given datum, or NULL. */
314 1.65 rillig ListNode *
315 1.65 rillig Lst_FindDatum(List *list, const void *datum)
316 1.1 rillig {
317 1.65 rillig ListNode *node;
318 1.1 rillig
319 1.52 rillig assert(list != NULL);
320 1.29 rillig assert(datum != NULL);
321 1.1 rillig
322 1.29 rillig for (node = list->first; node != NULL; node = node->next) {
323 1.16 rillig if (node->datum == datum) {
324 1.16 rillig return node;
325 1.1 rillig }
326 1.29 rillig }
327 1.1 rillig
328 1.1 rillig return NULL;
329 1.1 rillig }
330 1.1 rillig
331 1.69 rillig void
332 1.69 rillig Lst_ForEach(List *list, LstActionProc proc, void *procData)
333 1.69 rillig {
334 1.69 rillig ListNode *node;
335 1.69 rillig for (node = list->first; node != NULL; node = node->next)
336 1.69 rillig proc(node->datum, procData);
337 1.69 rillig }
338 1.69 rillig
339 1.14 rillig /* Apply the given function to each element of the given list. The function
340 1.14 rillig * should return 0 if traversal should continue and non-zero if it should
341 1.14 rillig * abort. */
342 1.1 rillig int
343 1.67 rillig Lst_ForEachUntil(List *list, LstActionUntilProc proc, void *procData)
344 1.42 rillig {
345 1.68 rillig ListNode *tln = list->first;
346 1.68 rillig int result = 0;
347 1.1 rillig
348 1.68 rillig while (tln != NULL) {
349 1.1 rillig /*
350 1.1 rillig * Take care of having the current element deleted out from under
351 1.1 rillig * us.
352 1.1 rillig */
353 1.68 rillig ListNode *next = tln->next;
354 1.1 rillig
355 1.1 rillig /*
356 1.1 rillig * We're done with the traversal if
357 1.38 rillig * - the next node to examine doesn't exist and
358 1.1 rillig * - nothing's been added after the current node (check this
359 1.1 rillig * after proc() has been called).
360 1.1 rillig */
361 1.68 rillig Boolean done = next == NULL;
362 1.1 rillig
363 1.70 rillig tln->priv_useCount++;
364 1.16 rillig result = (*proc)(tln->datum, procData);
365 1.70 rillig tln->priv_useCount--;
366 1.1 rillig
367 1.1 rillig /*
368 1.1 rillig * Now check whether a node has been added.
369 1.1 rillig * Note: this doesn't work if this node was deleted before
370 1.1 rillig * the new node was added.
371 1.1 rillig */
372 1.15 rillig if (next != tln->next) {
373 1.15 rillig next = tln->next;
374 1.4 rillig done = 0;
375 1.1 rillig }
376 1.1 rillig
377 1.70 rillig if (tln->priv_deleted) {
378 1.1 rillig free((char *)tln);
379 1.1 rillig }
380 1.1 rillig tln = next;
381 1.68 rillig if (result || LstIsEmpty(list) || done)
382 1.68 rillig break;
383 1.68 rillig }
384 1.1 rillig
385 1.1 rillig return result;
386 1.1 rillig }
387 1.1 rillig
388 1.34 rillig /* Move all nodes from list2 to the end of list1.
389 1.34 rillig * List2 is destroyed and freed. */
390 1.34 rillig void
391 1.65 rillig Lst_MoveAll(List *list1, List *list2)
392 1.1 rillig {
393 1.52 rillig assert(list1 != NULL);
394 1.52 rillig assert(list2 != NULL);
395 1.1 rillig
396 1.34 rillig if (list2->first != NULL) {
397 1.34 rillig list2->first->prev = list1->last;
398 1.34 rillig if (list1->last != NULL) {
399 1.34 rillig list1->last->next = list2->first;
400 1.34 rillig } else {
401 1.34 rillig list1->first = list2->first;
402 1.1 rillig }
403 1.34 rillig list1->last = list2->last;
404 1.1 rillig }
405 1.34 rillig free(list2);
406 1.1 rillig }
407 1.1 rillig
408 1.22 rillig /* Copy the element data from src to the start of dst. */
409 1.22 rillig void
410 1.65 rillig Lst_PrependAll(List *dst, List *src)
411 1.22 rillig {
412 1.65 rillig ListNode *node;
413 1.22 rillig for (node = src->last; node != NULL; node = node->prev)
414 1.50 rillig Lst_Prepend(dst, node->datum);
415 1.22 rillig }
416 1.22 rillig
417 1.22 rillig /* Copy the element data from src to the end of dst. */
418 1.22 rillig void
419 1.65 rillig Lst_AppendAll(List *dst, List *src)
420 1.22 rillig {
421 1.65 rillig ListNode *node;
422 1.22 rillig for (node = src->first; node != NULL; node = node->next)
423 1.50 rillig Lst_Append(dst, node->datum);
424 1.22 rillig }
425 1.1 rillig
426 1.1 rillig /*
427 1.1 rillig * these functions are for dealing with a list as a table, of sorts.
428 1.1 rillig * An idea of the "current element" is kept and used by all the functions
429 1.1 rillig * between Lst_Open() and Lst_Close().
430 1.1 rillig *
431 1.1 rillig * The sequential functions access the list in a slightly different way.
432 1.1 rillig * CurPtr points to their idea of the current node in the list and they
433 1.1 rillig * access the list based on it.
434 1.1 rillig */
435 1.1 rillig
436 1.14 rillig /* Open a list for sequential access. A list can still be searched, etc.,
437 1.14 rillig * without confusing these functions. */
438 1.10 rillig void
439 1.65 rillig Lst_Open(List *list)
440 1.10 rillig {
441 1.52 rillig assert(list != NULL);
442 1.70 rillig assert(!list->priv_isOpen);
443 1.10 rillig
444 1.70 rillig list->priv_isOpen = TRUE;
445 1.70 rillig list->priv_lastAccess = LstIsEmpty(list) ? Head : Unknown;
446 1.70 rillig list->priv_curr = NULL;
447 1.10 rillig }
448 1.10 rillig
449 1.10 rillig /* Return the next node for the given list, or NULL if the end has been
450 1.10 rillig * reached. */
451 1.65 rillig ListNode *
452 1.65 rillig Lst_Next(List *list)
453 1.1 rillig {
454 1.65 rillig ListNode *node;
455 1.1 rillig
456 1.52 rillig assert(list != NULL);
457 1.70 rillig assert(list->priv_isOpen);
458 1.1 rillig
459 1.70 rillig list->priv_prev = list->priv_curr;
460 1.1 rillig
461 1.70 rillig if (list->priv_curr == NULL) {
462 1.70 rillig if (list->priv_lastAccess == Unknown) {
463 1.1 rillig /*
464 1.15 rillig * If we're just starting out, lastAccess will be Unknown.
465 1.1 rillig * Then we want to start this thing off in the right
466 1.15 rillig * direction -- at the start with lastAccess being Middle.
467 1.1 rillig */
468 1.70 rillig list->priv_curr = node = list->first;
469 1.70 rillig list->priv_lastAccess = Middle;
470 1.1 rillig } else {
471 1.16 rillig node = NULL;
472 1.70 rillig list->priv_lastAccess = Tail;
473 1.1 rillig }
474 1.1 rillig } else {
475 1.70 rillig node = list->priv_curr->next;
476 1.70 rillig list->priv_curr = node;
477 1.1 rillig
478 1.16 rillig if (node == list->first || node == NULL) {
479 1.1 rillig /*
480 1.1 rillig * If back at the front, then we've hit the end...
481 1.1 rillig */
482 1.70 rillig list->priv_lastAccess = Tail;
483 1.1 rillig } else {
484 1.1 rillig /*
485 1.1 rillig * Reset to Middle if gone past first.
486 1.1 rillig */
487 1.70 rillig list->priv_lastAccess = Middle;
488 1.1 rillig }
489 1.1 rillig }
490 1.1 rillig
491 1.16 rillig return node;
492 1.1 rillig }
493 1.1 rillig
494 1.10 rillig /* Close a list which was opened for sequential access. */
495 1.1 rillig void
496 1.65 rillig Lst_Close(List *list)
497 1.1 rillig {
498 1.52 rillig assert(list != NULL);
499 1.70 rillig assert(list->priv_isOpen);
500 1.1 rillig
501 1.70 rillig list->priv_isOpen = FALSE;
502 1.70 rillig list->priv_lastAccess = Unknown;
503 1.1 rillig }
504 1.1 rillig
505 1.1 rillig
506 1.1 rillig /*
507 1.1 rillig * for using the list as a queue
508 1.1 rillig */
509 1.1 rillig
510 1.14 rillig /* Add the datum to the tail of the given list. */
511 1.25 rillig void
512 1.65 rillig Lst_Enqueue(List *list, void *datum)
513 1.1 rillig {
514 1.50 rillig Lst_Append(list, datum);
515 1.1 rillig }
516 1.1 rillig
517 1.25 rillig /* Remove and return the datum at the head of the given list. */
518 1.1 rillig void *
519 1.65 rillig Lst_Dequeue(List *list)
520 1.1 rillig {
521 1.16 rillig void *datum;
522 1.1 rillig
523 1.52 rillig assert(list != NULL);
524 1.25 rillig assert(!LstIsEmpty(list));
525 1.1 rillig
526 1.25 rillig datum = list->first->datum;
527 1.50 rillig Lst_Remove(list, list->first);
528 1.25 rillig assert(datum != NULL);
529 1.16 rillig return datum;
530 1.1 rillig }
531 1.61 rillig
532 1.61 rillig void
533 1.61 rillig Stack_Init(Stack *stack)
534 1.61 rillig {
535 1.61 rillig stack->len = 0;
536 1.61 rillig stack->cap = 10;
537 1.61 rillig stack->items = bmake_malloc(stack->cap * sizeof stack->items[0]);
538 1.61 rillig }
539 1.61 rillig
540 1.61 rillig Boolean Stack_IsEmpty(Stack *stack)
541 1.61 rillig {
542 1.61 rillig return stack->len == 0;
543 1.61 rillig }
544 1.61 rillig
545 1.61 rillig void Stack_Push(Stack *stack, void *datum)
546 1.61 rillig {
547 1.61 rillig if (stack->len >= stack->cap) {
548 1.62 rillig stack->cap *= 2;
549 1.61 rillig stack->items = bmake_realloc(stack->items,
550 1.61 rillig stack->cap * sizeof stack->items[0]);
551 1.61 rillig }
552 1.61 rillig stack->items[stack->len] = datum;
553 1.61 rillig stack->len++;
554 1.61 rillig }
555 1.61 rillig
556 1.61 rillig void *Stack_Pop(Stack *stack)
557 1.61 rillig {
558 1.64 rillig void *datum;
559 1.64 rillig
560 1.61 rillig assert(stack->len > 0);
561 1.61 rillig stack->len--;
562 1.64 rillig datum = stack->items[stack->len];
563 1.64 rillig #ifdef CLEANUP
564 1.64 rillig stack->items[stack->len] = NULL;
565 1.64 rillig #endif
566 1.64 rillig return datum;
567 1.61 rillig }
568 1.61 rillig
569 1.61 rillig void Stack_Done(Stack *stack)
570 1.61 rillig {
571 1.61 rillig free(stack->items);
572 1.61 rillig }
573