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