8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libdtrace / common / dt_list.c
blob32279e9bd274dd83d536c64d377d413e5a22fdf6
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Simple doubly-linked list implementation. This implementation assumes that
31 * each list element contains an embedded dt_list_t (previous and next
32 * pointers), which is typically the first member of the element struct.
33 * An additional dt_list_t is used to store the head (dl_next) and tail
34 * (dl_prev) pointers. The current head and tail list elements have their
35 * previous and next pointers set to NULL, respectively.
38 #include <unistd.h>
39 #include <assert.h>
40 #include <dt_list.h>
42 void
43 dt_list_append(dt_list_t *dlp, void *new)
45 dt_list_t *p = dlp->dl_prev; /* p = tail list element */
46 dt_list_t *q = new; /* q = new list element */
48 dlp->dl_prev = q;
49 q->dl_prev = p;
50 q->dl_next = NULL;
52 if (p != NULL) {
53 assert(p->dl_next == NULL);
54 p->dl_next = q;
55 } else {
56 assert(dlp->dl_next == NULL);
57 dlp->dl_next = q;
61 void
62 dt_list_prepend(dt_list_t *dlp, void *new)
64 dt_list_t *p = new; /* p = new list element */
65 dt_list_t *q = dlp->dl_next; /* q = head list element */
67 dlp->dl_next = p;
68 p->dl_prev = NULL;
69 p->dl_next = q;
71 if (q != NULL) {
72 assert(q->dl_prev == NULL);
73 q->dl_prev = p;
74 } else {
75 assert(dlp->dl_prev == NULL);
76 dlp->dl_prev = p;
80 void
81 dt_list_insert(dt_list_t *dlp, void *after_me, void *new)
83 dt_list_t *p = after_me;
84 dt_list_t *q = new;
86 if (p == NULL || p->dl_next == NULL) {
87 dt_list_append(dlp, new);
88 return;
91 q->dl_next = p->dl_next;
92 q->dl_prev = p;
93 p->dl_next = q;
94 q->dl_next->dl_prev = q;
97 void
98 dt_list_delete(dt_list_t *dlp, void *existing)
100 dt_list_t *p = existing;
102 if (p->dl_prev != NULL)
103 p->dl_prev->dl_next = p->dl_next;
104 else
105 dlp->dl_next = p->dl_next;
107 if (p->dl_next != NULL)
108 p->dl_next->dl_prev = p->dl_prev;
109 else
110 dlp->dl_prev = p->dl_prev;