2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1988, 1989 by Adam de Boor
5 * Copyright (c) 1989 by Berkeley Softworks
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * from: @(#)lst.h 8.1 (Berkeley) 6/6/93
40 * $FreeBSD: src/usr.bin/make/lst.h,v 1.9 1999/08/28 01:03:32 peter Exp $
41 * $DragonFly: src/usr.bin/make/lst.h,v 1.16 2004/12/17 08:01:40 okumoto Exp $
46 * Header for using the list library
51 #include <sys/param.h>
56 * Structure of a list node.
59 struct LstNode *prevPtr; /* previous element in list */
60 struct LstNode *nextPtr; /* next in list */
61 int useCount:8; /* Count of functions using the node. Node may not
62 * be deleted until count goes to 0 */
63 int flags:8; /* Node status flags */
64 void *datum; /* datum associated with this element */
66 typedef struct LstNode LstNode;
69 * Flags required for synchronization
71 #define LN_DELETED 0x0001 /* List node should be removed when done */
74 LstHead, LstMiddle, LstTail, LstUnknown
81 LstNode *firstPtr; /* first node in list */
82 LstNode *lastPtr; /* last node in list */
84 typedef struct Lst Lst;
86 typedef int CompareProc(const void *, const void *);
87 typedef int DoProc(void *, void *);
88 typedef void *DuplicateProc(void *);
89 typedef void FreeProc(void *);
92 * NOFREE can be used as the freeProc to Lst_Destroy when the elements are
94 * NOCOPY performs similarly when given as the copyProc to Lst_Duplicate.
96 #define NOFREE ((FreeProc *)NULL)
97 #define NOCOPY ((DuplicateProc *)NULL)
99 #define LST_CONCNEW 0 /* create new LstNode's when using Lst_Concat */
100 #define LST_CONCLINK 1 /* relink LstNode's when using Lst_Concat */
103 * Creation/destruction functions
105 /* Create a new list */
107 /* Duplicate an existing list */
108 Lst *Lst_Duplicate(Lst *, DuplicateProc *);
109 /* Destroy an old one */
110 void Lst_Destroy(Lst *, FreeProc *);
113 * Functions to modify a list
115 /* Insert an element before another */
116 ReturnStatus Lst_Insert(Lst *, LstNode *, void *);
117 /* Insert an element after another */
118 ReturnStatus Lst_Append(Lst *, LstNode *, void *);
119 /* Place an element at the front of a lst. */
120 #define Lst_AtFront(LST, D) (Lst_Insert((LST), Lst_First(LST), (D)))
121 /* Place an element at the end of a lst. */
122 #define Lst_AtEnd(LST, D) (Lst_Append((LST), Lst_Last(LST), (D)))
123 /* Remove an element */
124 void Lst_Remove(Lst *, LstNode *);
125 /* Replace a node with a new value */
126 #define Lst_Replace(NODE, D) (((NODE) == NULL) ? FAILURE : \
127 (((NODE)->datum = (D)), SUCCESS))
128 /* Concatenate two lists */
129 void Lst_Concat(Lst *, Lst *, int);
132 * Node-specific functions
134 /* Return first element in list */
135 #define Lst_First(LST) ((Lst_Valid(LST) && !Lst_IsEmpty(LST)) \
136 ? (LST)->firstPtr : NULL)
137 /* Return last element in list */
138 #define Lst_Last(LST) ((Lst_Valid(LST) && !Lst_IsEmpty(LST)) \
139 ? (LST)->lastPtr : NULL)
140 /* Return successor to given element */
141 #define Lst_Succ(NODE) (((NODE) == NULL) ? NULL : (NODE)->nextPtr)
142 /* Get datum from LstNode */
143 #define Lst_Datum(NODE) ((NODE)->datum)
146 * Functions for entire lists
148 /* Find an element in a list */
149 #define Lst_Find(LST, D, FN) (Lst_FindFrom((LST), Lst_First(LST), (D), (FN)))
150 /* Find an element starting from somewhere */
151 LstNode *Lst_FindFrom(Lst *, LstNode *, const void *, CompareProc *);
153 * See if the given datum is on the list. Returns the LstNode containing
156 LstNode *Lst_Member(Lst *, void *);
157 /* Apply a function to all elements of a lst */
158 void Lst_ForEach(Lst *, DoProc *, void *);
159 #define Lst_ForEach(LST, FN, D) (Lst_ForEachFrom((LST), Lst_First(LST), \
162 * Apply a function to all elements of a lst starting from a certain point.
163 * If the list is circular, the application will wrap around to the
164 * beginning of the list again.
166 void Lst_ForEachFrom(Lst *, LstNode *, DoProc *, void *);
169 * for using the list as a queue
171 /* Place an element at tail of queue */
172 #define Lst_EnQueue(LST, D) (Lst_Valid(LST) \
173 ? Lst_Append((LST), Lst_Last(LST), (D)) \
175 /* Remove an element from head of queue */
176 void *Lst_DeQueue(Lst *);
180 * Return TRUE if the list L is valid
182 #define Lst_Valid(L) (((L) == NULL) ? FALSE : TRUE)
185 * LstNodeValid (LN, L) --
186 * Return TRUE if the LstNode LN is valid with respect to L
188 #define Lst_NodeValid(LN, L) (((LN) == NULL) ? FALSE : TRUE)
192 * TRUE if the list L is empty.
194 #define Lst_IsEmpty(L) (!Lst_Valid(L) || (L)->firstPtr == NULL)