1 /*
2  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Adam de Boor.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)lstDestroy.c	5.3 (Berkeley) 06/01/90";
13 #endif /* not lint */
14 
15 /*-
16  * LstDestroy.c --
17  *	Nuke a list and all its resources
18  */
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;
40     register void	(*freeProc)();
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     }
53 
54     if (freeProc) {
55 	for (ln = list->firstPtr;
56 	     ln != NilListNode && tln != list->firstPtr;
57 	     ln = tln) {
58 		 tln = ln->nextPtr;
59 		 (*freeProc) (ln->datum);
60 		 free ((Address)ln);
61 	}
62     } else {
63 	for (ln = list->firstPtr;
64 	     ln != NilListNode && tln != list->firstPtr;
65 	     ln = tln) {
66 		 tln = ln->nextPtr;
67 		 free ((Address)ln);
68 	}
69     }
70 
71     free ((Address)l);
72 }
73