-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlist.c
275 lines (238 loc) · 6.15 KB
/
list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license in the file COPYING
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file COPYING.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Generic doubly-linked list implementation
*/
#include "list.h"
#include "list_impl.h"
#include <sys/types.h>
#include <stdlib.h>
#ifdef _KERNEL
#include <sys/debug.h>
#else
#include <assert.h>
#ifdef DEBUG
#define ASSERT(a) assert(a)
#else /* !DEBUG */
#define ASSERT(a)
#endif /* !DEBUG */
#endif
#ifdef lint
extern list_node_t *list_d2l(list_t *list, void *obj);
#else
#define list_d2l(a, obj) ((list_node_t *)(((char *)obj) + (a)->list_offset))
#endif
#define list_object(a, node) ((void *)(((char *)node) - (a)->list_offset))
#define list_empty(a) ((a)->list_head.list_next == &(a)->list_head)
#define list_insert_after_node(list, node, object) { \
list_node_t *lnew = list_d2l(list, object); \
lnew->list_prev = (node); \
lnew->list_next = (node)->list_next; \
(node)->list_next->list_prev = lnew; \
(node)->list_next = lnew; \
(list)->list_count++; \
}
#define list_insert_before_node(list, node, object) { \
list_node_t *lnew = list_d2l(list, object); \
lnew->list_next = (node); \
lnew->list_prev = (node)->list_prev; \
(node)->list_prev->list_next = lnew; \
(node)->list_prev = lnew; \
(list)->list_count++; \
}
#define list_remove_node(node) \
(node)->list_prev->list_next = (node)->list_next; \
(node)->list_next->list_prev = (node)->list_prev; \
(node)->list_next = (node)->list_prev = NULL
void
list_create(list_t *list, size_t size, size_t offset)
{
ASSERT(list);
ASSERT(size > 0);
ASSERT(size >= offset + sizeof (list_node_t));
list->list_size = size;
list->list_offset = offset;
list->list_count = 0;
list->list_head.list_next = list->list_head.list_prev =
&list->list_head;
}
void
list_destroy(list_t *list)
{
list_node_t *node = &list->list_head;
ASSERT(list);
ASSERT(list->list_head.list_next == node);
ASSERT(list->list_head.list_prev == node);
ASSERT(list->list_count == 0);
node->list_next = node->list_prev = NULL;
}
void
list_insert_after(list_t *list, void *object, void *nobject)
{
if (object == NULL) {
list_insert_head(list, nobject);
} else {
list_node_t *lold = list_d2l(list, object);
list_insert_after_node(list, lold, nobject);
}
}
void
list_insert_before(list_t *list, void *object, void *nobject)
{
if (object == NULL) {
list_insert_tail(list, nobject);
} else {
list_node_t *lold = list_d2l(list, object);
list_insert_before_node(list, lold, nobject);
}
}
void
list_insert_head(list_t *list, void *object)
{
list_node_t *lold = &list->list_head;
list_insert_after_node(list, lold, object);
}
void
list_insert_tail(list_t *list, void *object)
{
list_node_t *lold = &list->list_head;
list_insert_before_node(list, lold, object);
}
void
list_remove(list_t *list, void *object)
{
list_node_t *lold = list_d2l(list, object);
ASSERT(!list_empty(list));
ASSERT(lold->list_next != NULL);
list_remove_node(lold);
list->list_count--;
}
void *
list_remove_head(list_t *list)
{
list_node_t *head = list->list_head.list_next;
if (head == &list->list_head)
return (NULL);
list_remove_node(head);
list->list_count--;
return (list_object(list, head));
}
void *
list_remove_tail(list_t *list)
{
list_node_t *tail = list->list_head.list_prev;
if (tail == &list->list_head)
return (NULL);
list_remove_node(tail);
list->list_count--;
return (list_object(list, tail));
}
void *
list_head(const list_t *list)
{
if (list_empty(list))
return (NULL);
return (list_object(list, list->list_head.list_next));
}
void *
list_tail(const list_t *list)
{
if (list_empty(list))
return (NULL);
return (list_object(list, list->list_head.list_prev));
}
void *
list_next(const list_t *list, const void *object)
{
list_node_t *node = list_d2l(list, object);
ASSERT(list_link_active(node));
if (node->list_next != &list->list_head)
return (list_object(list, node->list_next));
return (NULL);
}
void *
list_prev(const list_t *list, const void *object)
{
list_node_t *node = list_d2l(list, object);
ASSERT(list_link_active(node));
if (node->list_prev != &list->list_head)
return (list_object(list, node->list_prev));
return (NULL);
}
/*
* Insert src list after dst list. Empty src list thereafter.
*/
void
list_move_tail(list_t *dst, list_t *src)
{
list_node_t *dstnode = &dst->list_head;
list_node_t *srcnode = &src->list_head;
ASSERT(dst->list_size == src->list_size);
ASSERT(dst->list_offset == src->list_offset);
if (list_empty(src))
return;
dstnode->list_prev->list_next = srcnode->list_next;
srcnode->list_next->list_prev = dstnode->list_prev;
dstnode->list_prev = srcnode->list_prev;
srcnode->list_prev->list_next = dstnode;
dst->list_count += src->list_count;
/* empty src list */
srcnode->list_next = srcnode->list_prev = srcnode;
src->list_count = 0;
}
void
list_link_replace(list_node_t *lold, list_node_t *lnew)
{
ASSERT(list_link_active(lold));
ASSERT(!list_link_active(lnew));
lnew->list_next = lold->list_next;
lnew->list_prev = lold->list_prev;
lold->list_prev->list_next = lnew;
lold->list_next->list_prev = lnew;
lold->list_next = lold->list_prev = NULL;
}
void
list_link_init(list_node_t *link)
{
link->list_next = NULL;
link->list_prev = NULL;
}
int
list_link_active(const list_node_t *link)
{
return (link->list_next != NULL);
}
int
list_is_empty(const list_t *list)
{
return (list_empty(list));
}
/*
* Counts the number of items in the list.
*/
size_t
list_count(const list_t *list)
{
return (list->list_count);
}