1 /*- 2 * LstDestroy.c -- 3 * Nuke a list and all its resources 4 * 5 * Copyright (c) 1988 by University of California Regents 6 * 7 * Permission to use, copy, modify, and distribute this 8 * software and its documentation for any purpose and without 9 * fee is hereby granted, provided that the above copyright 10 * notice appears in all copies. Neither the University of California nor 11 * Adam de Boor makes any representations about the suitability of this 12 * software for any purpose. It is provided "as is" without 13 * express or implied warranty. 14 */ 15 #ifndef lint 16 static char *rcsid = 17 "$Id: lstDestroy.c,v 1.5 88/11/17 20:52:15 adam Exp $ SPRITE (Berkeley)"; 18 #endif lint 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