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