8158 Want named threads API
[unleashed.git] / usr / src / cmd / mdb / common / mdb / mdb_list.c
blob7e5e274314e00c0faab13b7a1f9f80b563f8bcb8
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 (c) 1999 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <mdb/mdb_list.h>
30 #include <mdb/mdb_debug.h>
31 #include <unistd.h>
34 * Simple doubly-linked list implementation. This implementation assumes that
35 * each list element contains an embedded mdb_list_t (previous and next
36 * pointers), which is typically the first member of the element struct.
37 * An additional mdb_list_t is used to store the head (ml_next) and tail
38 * (ml_prev) pointers. The current head and tail list elements have their
39 * previous and next pointers set to NULL, respectively.
42 void
43 mdb_list_append(mdb_list_t *mlp, void *new)
45 mdb_list_t *p = mlp->ml_prev; /* p = tail list element */
46 mdb_list_t *q = new; /* q = new list element */
48 mlp->ml_prev = q;
49 q->ml_prev = p;
50 q->ml_next = NULL;
52 if (p != NULL) {
53 ASSERT(p->ml_next == NULL);
54 p->ml_next = q;
55 } else {
56 ASSERT(mlp->ml_next == NULL);
57 mlp->ml_next = q;
61 void
62 mdb_list_prepend(mdb_list_t *mlp, void *new)
64 mdb_list_t *p = new; /* p = new list element */
65 mdb_list_t *q = mlp->ml_next; /* q = head list element */
67 mlp->ml_next = p;
68 p->ml_prev = NULL;
69 p->ml_next = q;
71 if (q != NULL) {
72 ASSERT(q->ml_prev == NULL);
73 q->ml_prev = p;
74 } else {
75 ASSERT(mlp->ml_prev == NULL);
76 mlp->ml_prev = p;
80 void
81 mdb_list_insert(mdb_list_t *mlp, void *after_me, void *new)
83 mdb_list_t *p = after_me;
84 mdb_list_t *q = new;
86 if (p == NULL || p->ml_next == NULL) {
87 mdb_list_append(mlp, new);
88 return;
91 q->ml_next = p->ml_next;
92 q->ml_prev = p;
93 p->ml_next = q;
94 q->ml_next->ml_prev = q;
97 void
98 mdb_list_delete(mdb_list_t *mlp, void *existing)
100 mdb_list_t *p = existing;
102 if (p->ml_prev != NULL)
103 p->ml_prev->ml_next = p->ml_next;
104 else
105 mlp->ml_next = p->ml_next;
107 if (p->ml_next != NULL)
108 p->ml_next->ml_prev = p->ml_prev;
109 else
110 mlp->ml_prev = p->ml_prev;
113 void
114 mdb_list_move(mdb_list_t *src, mdb_list_t *dst)
116 dst->ml_prev = src->ml_prev;
117 dst->ml_next = src->ml_next;
118 src->ml_prev = NULL;
119 src->ml_next = NULL;