xref: /freebsd/contrib/bmake/lst.h (revision d746ab215cc85d8f7ab05c5f866c338782c390ec)
1 /*	$NetBSD: lst.h,v 1.102 2021/12/15 12:24:13 rillig Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from: @(#)lst.h	8.1 (Berkeley) 6/6/93
35  */
36 
37 /*
38  * Copyright (c) 1988, 1989 by Adam de Boor
39  * Copyright (c) 1989 by Berkeley Softworks
40  * All rights reserved.
41  *
42  * This code is derived from software contributed to Berkeley by
43  * Adam de Boor.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. All advertising materials mentioning features or use of this software
54  *    must display the following acknowledgement:
55  *	This product includes software developed by the University of
56  *	California, Berkeley and its contributors.
57  * 4. Neither the name of the University nor the names of its contributors
58  *    may be used to endorse or promote products derived from this software
59  *    without specific prior written permission.
60  *
61  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71  * SUCH DAMAGE.
72  *
73  *	from: @(#)lst.h	8.1 (Berkeley) 6/6/93
74  */
75 
76 /* Doubly-linked lists of arbitrary pointers. */
77 
78 #ifndef MAKE_LST_H
79 #define MAKE_LST_H
80 
81 #ifdef HAVE_INTTYPES_H
82 #include <inttypes.h>
83 #elif defined(HAVE_STDINT_H)
84 #include <stdint.h>
85 #endif
86 #ifdef HAVE_STDLIB_H
87 #include <stdlib.h>
88 #endif
89 
90 /* A doubly-linked list of pointers. */
91 typedef struct List List;
92 /* A single node in the doubly-linked list. */
93 typedef struct ListNode ListNode;
94 
95 struct ListNode {
96 	ListNode *prev;		/* previous node in list, or NULL */
97 	ListNode *next;		/* next node in list, or NULL */
98 	void *datum;		/* datum associated with this element */
99 };
100 
101 struct List {
102 	ListNode *first;
103 	ListNode *last;
104 };
105 
106 /* Free the datum of a node, called before freeing the node itself. */
107 typedef void LstFreeProc(void *);
108 
109 /* Create or destroy a list */
110 
111 /* Create a new list. */
112 List *Lst_New(void) MAKE_ATTR_USE;
113 /* Free the list nodes, but not the list itself. */
114 void Lst_Done(List *);
115 /* Free the list nodes, freeing the node data using the given function. */
116 void Lst_DoneCall(List *, LstFreeProc);
117 /* Free the list, leaving the node data unmodified. */
118 void Lst_Free(List *);
119 
120 #define LST_INIT { NULL, NULL }
121 
122 /* Initialize a list, without memory allocation. */
123 MAKE_INLINE void
124 Lst_Init(List *list)
125 {
126 	list->first = NULL;
127 	list->last = NULL;
128 }
129 
130 /* Get information about a list */
131 
132 MAKE_INLINE bool MAKE_ATTR_USE
133 Lst_IsEmpty(List *list)
134 {
135 	return list->first == NULL;
136 }
137 
138 /* Find the first node that contains the given datum, or NULL. */
139 ListNode *Lst_FindDatum(List *, const void *) MAKE_ATTR_USE;
140 
141 /* Modify a list */
142 
143 /* Insert a datum before the given node. */
144 void Lst_InsertBefore(List *, ListNode *, void *);
145 /* Add a datum at the front of the list. */
146 void Lst_Prepend(List *, void *);
147 /* Add a datum at the end of the list. */
148 void Lst_Append(List *, void *);
149 /* Remove the node from the list. */
150 void Lst_Remove(List *, ListNode *);
151 void Lst_PrependAll(List *, List *);
152 void Lst_AppendAll(List *, List *);
153 void Lst_MoveAll(List *, List *);
154 
155 /* Node-specific functions */
156 
157 /* Replace the value of the node. */
158 void LstNode_Set(ListNode *, void *);
159 /* Set the value of the node to NULL. Having NULL in a list is unusual. */
160 void LstNode_SetNull(ListNode *);
161 
162 /* Using the list as a queue */
163 
164 /* Add a datum at the tail of the queue. */
165 MAKE_INLINE void
166 Lst_Enqueue(List *list, void *datum)
167 {
168 	Lst_Append(list, datum);
169 }
170 
171 /* Remove the head node of the queue and return its datum. */
172 void *Lst_Dequeue(List *) MAKE_ATTR_USE;
173 
174 /*
175  * A vector is an ordered collection of items, allowing for fast indexed
176  * access.
177  */
178 typedef struct Vector {
179 	void *items;		/* memory holding the items */
180 	size_t itemSize;	/* size of a single item */
181 	size_t len;		/* number of actually usable elements */
182 	size_t cap;		/* capacity */
183 } Vector;
184 
185 void Vector_Init(Vector *, size_t);
186 
187 /*
188  * Return the pointer to the given item in the vector.
189  * The returned data is valid until the next modifying operation.
190  */
191 MAKE_INLINE void * MAKE_ATTR_USE
192 Vector_Get(Vector *v, size_t i)
193 {
194 	unsigned char *items = v->items;
195 	return items + i * v->itemSize;
196 }
197 
198 void *Vector_Push(Vector *);
199 void *Vector_Pop(Vector *);
200 
201 MAKE_INLINE void
202 Vector_Done(Vector *v)
203 {
204 	free(v->items);
205 }
206 
207 #endif
208