Commit | Line | Data |
---|---|---|
bb2109e7 | 1 | /* |
f5ed9d08 KB |
2 | * Copyright (c) 1988, 1989, 1990, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
c65fedcf | 4 | * |
bb2109e7 KB |
5 | * This code is derived from software contributed to Berkeley by |
6 | * Adam de Boor. | |
c65fedcf | 7 | * |
f15db449 | 8 | * %sccs.include.redist.c% |
c65fedcf | 9 | */ |
bb2109e7 | 10 | |
c65fedcf | 11 | #ifndef lint |
bfdbffbb | 12 | static char sccsid[] = "@(#)lstDestroy.c 8.2 (Berkeley) %G%"; |
bb2109e7 KB |
13 | #endif /* not lint */ |
14 | ||
15 | /*- | |
16 | * LstDestroy.c -- | |
17 | * Nuke a list and all its resources | |
18 | */ | |
c65fedcf KB |
19 | |
20 | #include "lstInt.h" | |
21 | ||
22 | /*- | |
23 | *----------------------------------------------------------------------- | |
24 | * Lst_Destroy -- | |
25 | * Destroy a list and free all its resources. If the freeProc is | |
26 | * given, it is called with the datum from each node in turn before | |
27 | * the node is freed. | |
28 | * | |
29 | * Results: | |
30 | * None. | |
31 | * | |
32 | * Side Effects: | |
33 | * The given list is freed in its entirety. | |
34 | * | |
35 | *----------------------------------------------------------------------- | |
36 | */ | |
37 | void | |
38 | Lst_Destroy (l, freeProc) | |
39 | Lst l; | |
bfdbffbb | 40 | register void (*freeProc) __P((ClientData)); |
c65fedcf KB |
41 | { |
42 | register ListNode ln; | |
43 | register ListNode tln = NilListNode; | |
44 | register List list = (List)l; | |
45 | ||
46 | if (l == NILLST || ! l) { | |
47 | /* | |
48 | * Note the check for l == (Lst)0 to catch uninitialized static Lst's. | |
49 | * Gross, but useful. | |
50 | */ | |
51 | return; | |
52 | } | |
bfdbffbb CZ |
53 | |
54 | /* To ease scanning */ | |
55 | if (list->lastPtr != NilListNode) | |
56 | list->lastPtr->nextPtr = NilListNode; | |
57 | else { | |
58 | free ((Address)l); | |
59 | return; | |
60 | } | |
61 | ||
c65fedcf | 62 | if (freeProc) { |
bfdbffbb CZ |
63 | for (ln = list->firstPtr; ln != NilListNode; ln = tln) { |
64 | tln = ln->nextPtr; | |
65 | (*freeProc) (ln->datum); | |
66 | free ((Address)ln); | |
c65fedcf KB |
67 | } |
68 | } else { | |
bfdbffbb CZ |
69 | for (ln = list->firstPtr; ln != NilListNode; ln = tln) { |
70 | tln = ln->nextPtr; | |
71 | free ((Address)ln); | |
c65fedcf KB |
72 | } |
73 | } | |
74 | ||
75 | free ((Address)l); | |
76 | } |