xref: /titanic_44/usr/src/cmd/mdb/common/mdb/mdb_list.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 (c) 1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <mdb/mdb_list.h>
30 #include <mdb/mdb_debug.h>
31 #include <unistd.h>
32 
33 /*
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.
40  */
41 
42 void
mdb_list_append(mdb_list_t * mlp,void * new)43 mdb_list_append(mdb_list_t *mlp, void *new)
44 {
45 	mdb_list_t *p = mlp->ml_prev;	/* p = tail list element */
46 	mdb_list_t *q = new;		/* q = new list element */
47 
48 	mlp->ml_prev = q;
49 	q->ml_prev = p;
50 	q->ml_next = NULL;
51 
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;
58 	}
59 }
60 
61 void
mdb_list_prepend(mdb_list_t * mlp,void * new)62 mdb_list_prepend(mdb_list_t *mlp, void *new)
63 {
64 	mdb_list_t *p = new;		/* p = new list element */
65 	mdb_list_t *q = mlp->ml_next;	/* q = head list element */
66 
67 	mlp->ml_next = p;
68 	p->ml_prev = NULL;
69 	p->ml_next = q;
70 
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;
77 	}
78 }
79 
80 void
mdb_list_insert(mdb_list_t * mlp,void * after_me,void * new)81 mdb_list_insert(mdb_list_t *mlp, void *after_me, void *new)
82 {
83 	mdb_list_t *p = after_me;
84 	mdb_list_t *q = new;
85 
86 	if (p == NULL || p->ml_next == NULL) {
87 		mdb_list_append(mlp, new);
88 		return;
89 	}
90 
91 	q->ml_next = p->ml_next;
92 	q->ml_prev = p;
93 	p->ml_next = q;
94 	q->ml_next->ml_prev = q;
95 }
96 
97 void
mdb_list_delete(mdb_list_t * mlp,void * existing)98 mdb_list_delete(mdb_list_t *mlp, void *existing)
99 {
100 	mdb_list_t *p = existing;
101 
102 	if (p->ml_prev != NULL)
103 		p->ml_prev->ml_next = p->ml_next;
104 	else
105 		mlp->ml_next = p->ml_next;
106 
107 	if (p->ml_next != NULL)
108 		p->ml_next->ml_prev = p->ml_prev;
109 	else
110 		mlp->ml_prev = p->ml_prev;
111 }
112 
113 void
mdb_list_move(mdb_list_t * src,mdb_list_t * dst)114 mdb_list_move(mdb_list_t *src, mdb_list_t *dst)
115 {
116 	dst->ml_prev = src->ml_prev;
117 	dst->ml_next = src->ml_next;
118 	src->ml_prev = NULL;
119 	src->ml_next = NULL;
120 }
121