1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /*
28 * Simple doubly-linked list implementation. This implementation assumes that
29 * each list element contains an embedded dt_list_t (previous and next
30 * pointers), which is typically the first member of the element struct.
31 * An additional dt_list_t is used to store the head (dl_next) and tail
32 * (dl_prev) pointers. The current head and tail list elements have their
33 * previous and next pointers set to NULL, respectively.
34 */
35
36 #include <unistd.h>
37 #include <assert.h>
38 #include <dt_list.h>
39
40 void
dt_list_append(dt_list_t * dlp,void * new)41 dt_list_append(dt_list_t *dlp, void *new)
42 {
43 dt_list_t *p = dlp->dl_prev; /* p = tail list element */
44 dt_list_t *q = new; /* q = new list element */
45
46 dlp->dl_prev = q;
47 q->dl_prev = p;
48 q->dl_next = NULL;
49
50 if (p != NULL) {
51 assert(p->dl_next == NULL);
52 p->dl_next = q;
53 } else {
54 assert(dlp->dl_next == NULL);
55 dlp->dl_next = q;
56 }
57 }
58
59 void
dt_list_prepend(dt_list_t * dlp,void * new)60 dt_list_prepend(dt_list_t *dlp, void *new)
61 {
62 dt_list_t *p = new; /* p = new list element */
63 dt_list_t *q = dlp->dl_next; /* q = head list element */
64
65 dlp->dl_next = p;
66 p->dl_prev = NULL;
67 p->dl_next = q;
68
69 if (q != NULL) {
70 assert(q->dl_prev == NULL);
71 q->dl_prev = p;
72 } else {
73 assert(dlp->dl_prev == NULL);
74 dlp->dl_prev = p;
75 }
76 }
77
78 void
dt_list_insert(dt_list_t * dlp,void * after_me,void * new)79 dt_list_insert(dt_list_t *dlp, void *after_me, void *new)
80 {
81 dt_list_t *p = after_me;
82 dt_list_t *q = new;
83
84 if (p == NULL || p->dl_next == NULL) {
85 dt_list_append(dlp, new);
86 return;
87 }
88
89 q->dl_next = p->dl_next;
90 q->dl_prev = p;
91 p->dl_next = q;
92 q->dl_next->dl_prev = q;
93 }
94
95 void
dt_list_delete(dt_list_t * dlp,void * existing)96 dt_list_delete(dt_list_t *dlp, void *existing)
97 {
98 dt_list_t *p = existing;
99
100 if (p->dl_prev != NULL)
101 p->dl_prev->dl_next = p->dl_next;
102 else
103 dlp->dl_next = p->dl_next;
104
105 if (p->dl_next != NULL)
106 p->dl_next->dl_prev = p->dl_prev;
107 else
108 dlp->dl_prev = p->dl_prev;
109 }
110