140412Sbostic /* 262085Sbostic * Copyright (c) 1988, 1989, 1990, 1993 362085Sbostic * The Regents of the University of California. All rights reserved. 440411Sbostic * 540412Sbostic * This code is derived from software contributed to Berkeley by 640412Sbostic * Adam de Boor. 740411Sbostic * 842741Sbostic * %sccs.include.redist.c% 940411Sbostic */ 1040412Sbostic 1140411Sbostic #ifndef lint 12*69094Schristos static char sccsid[] = "@(#)lstDestroy.c 8.2 (Berkeley) 04/28/95"; 1340412Sbostic #endif /* not lint */ 1440411Sbostic 1540412Sbostic /*- 1640412Sbostic * LstDestroy.c -- 1740412Sbostic * Nuke a list and all its resources 1840412Sbostic */ 1940412Sbostic 2040411Sbostic #include "lstInt.h" 2140411Sbostic 2240411Sbostic /*- 2340411Sbostic *----------------------------------------------------------------------- 2440411Sbostic * Lst_Destroy -- 2540411Sbostic * Destroy a list and free all its resources. If the freeProc is 2640411Sbostic * given, it is called with the datum from each node in turn before 2740411Sbostic * the node is freed. 2840411Sbostic * 2940411Sbostic * Results: 3040411Sbostic * None. 3140411Sbostic * 3240411Sbostic * Side Effects: 3340411Sbostic * The given list is freed in its entirety. 3440411Sbostic * 3540411Sbostic *----------------------------------------------------------------------- 3640411Sbostic */ 3740411Sbostic void Lst_Destroy(l,freeProc)3840411SbosticLst_Destroy (l, freeProc) 3940411Sbostic Lst l; 40*69094Schristos register void (*freeProc) __P((ClientData)); 4140411Sbostic { 4240411Sbostic register ListNode ln; 4340411Sbostic register ListNode tln = NilListNode; 4440411Sbostic register List list = (List)l; 4540411Sbostic 4640411Sbostic if (l == NILLST || ! l) { 4740411Sbostic /* 4840411Sbostic * Note the check for l == (Lst)0 to catch uninitialized static Lst's. 4940411Sbostic * Gross, but useful. 5040411Sbostic */ 5140411Sbostic return; 5240411Sbostic } 53*69094Schristos 54*69094Schristos /* To ease scanning */ 55*69094Schristos if (list->lastPtr != NilListNode) 56*69094Schristos list->lastPtr->nextPtr = NilListNode; 57*69094Schristos else { 58*69094Schristos free ((Address)l); 59*69094Schristos return; 60*69094Schristos } 61*69094Schristos 6240411Sbostic if (freeProc) { 63*69094Schristos for (ln = list->firstPtr; ln != NilListNode; ln = tln) { 64*69094Schristos tln = ln->nextPtr; 65*69094Schristos (*freeProc) (ln->datum); 66*69094Schristos free ((Address)ln); 6740411Sbostic } 6840411Sbostic } else { 69*69094Schristos for (ln = list->firstPtr; ln != NilListNode; ln = tln) { 70*69094Schristos tln = ln->nextPtr; 71*69094Schristos free ((Address)ln); 7240411Sbostic } 7340411Sbostic } 7440411Sbostic 7540411Sbostic free ((Address)l); 7640411Sbostic } 77