xref: /illumos-gate/usr/src/lib/libipmi/common/ipmi_list.c (revision c65ebfc7045424bd04a6c7719a27b0ad3399ad54)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Embedded Linked Lists
30  *
31  * Simple doubly-linked list implementation.  This implementation assumes that
32  * each list element contains an embedded ipmi_list_t (previous and next
33  * pointers), which is typically the first member of the element struct.
34  * An additional ipmi_list_t is used to store the head (l_next) and tail
35  * (l_prev) pointers.  The current head and tail list elements have their
36  * previous and next pointers set to NULL, respectively.
37  */
38 
39 #include <assert.h>
40 #include <ipmi_impl.h>
41 
42 void
43 ipmi_list_append(ipmi_list_t *lp, void *new)
44 {
45 	ipmi_list_t *p = lp->l_prev;	/* p = tail list element */
46 	ipmi_list_t *q = new;		/* q = new list element */
47 
48 	lp->l_prev = q;
49 	q->l_prev = p;
50 	q->l_next = NULL;
51 
52 	if (p != NULL) {
53 		assert(p->l_next == NULL);
54 		p->l_next = q;
55 	} else {
56 		assert(lp->l_next == NULL);
57 		lp->l_next = q;
58 	}
59 }
60 
61 void
62 ipmi_list_prepend(ipmi_list_t *lp, void *new)
63 {
64 	ipmi_list_t *p = new;		/* p = new list element */
65 	ipmi_list_t *q = lp->l_next;	/* q = head list element */
66 
67 	lp->l_next = p;
68 	p->l_prev = NULL;
69 	p->l_next = q;
70 
71 	if (q != NULL) {
72 		assert(q->l_prev == NULL);
73 		q->l_prev = p;
74 	} else {
75 		assert(lp->l_prev == NULL);
76 		lp->l_prev = p;
77 	}
78 }
79 
80 void
81 ipmi_list_insert_before(ipmi_list_t *lp, void *before_me, void *new)
82 {
83 	ipmi_list_t *p = before_me;
84 	ipmi_list_t *q = new;
85 
86 	if (p == NULL || p->l_prev == NULL) {
87 		ipmi_list_prepend(lp, new);
88 		return;
89 	}
90 
91 	q->l_prev = p->l_prev;
92 	q->l_next = p;
93 	p->l_prev = q;
94 	q->l_prev->l_next = q;
95 }
96 
97 void
98 ipmi_list_insert_after(ipmi_list_t *lp, void *after_me, void *new)
99 {
100 	ipmi_list_t *p = after_me;
101 	ipmi_list_t *q = new;
102 
103 	if (p == NULL || p->l_next == NULL) {
104 		ipmi_list_append(lp, new);
105 		return;
106 	}
107 
108 	q->l_next = p->l_next;
109 	q->l_prev = p;
110 	p->l_next = q;
111 	q->l_next->l_prev = q;
112 }
113 
114 void
115 ipmi_list_delete(ipmi_list_t *lp, void *existing)
116 {
117 	ipmi_list_t *p = existing;
118 
119 	if (p->l_prev != NULL)
120 		p->l_prev->l_next = p->l_next;
121 	else
122 		lp->l_next = p->l_next;
123 
124 	if (p->l_next != NULL)
125 		p->l_next->l_prev = p->l_prev;
126 	else
127 		lp->l_prev = p->l_prev;
128 }
129