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.
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.
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]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Embedded Linked Lists
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.
40 #include <ipmi_impl.h>
43 ipmi_list_append(ipmi_list_t
*lp
, void *new)
45 ipmi_list_t
*p
= lp
->l_prev
; /* p = tail list element */
46 ipmi_list_t
*q
= new; /* q = new list element */
53 assert(p
->l_next
== NULL
);
56 assert(lp
->l_next
== NULL
);
62 ipmi_list_prepend(ipmi_list_t
*lp
, void *new)
64 ipmi_list_t
*p
= new; /* p = new list element */
65 ipmi_list_t
*q
= lp
->l_next
; /* q = head list element */
72 assert(q
->l_prev
== NULL
);
75 assert(lp
->l_prev
== NULL
);
81 ipmi_list_insert_before(ipmi_list_t
*lp
, void *before_me
, void *new)
83 ipmi_list_t
*p
= before_me
;
86 if (p
== NULL
|| p
->l_prev
== NULL
) {
87 ipmi_list_prepend(lp
, new);
91 q
->l_prev
= p
->l_prev
;
94 q
->l_prev
->l_next
= q
;
98 ipmi_list_insert_after(ipmi_list_t
*lp
, void *after_me
, void *new)
100 ipmi_list_t
*p
= after_me
;
101 ipmi_list_t
*q
= new;
103 if (p
== NULL
|| p
->l_next
== NULL
) {
104 ipmi_list_append(lp
, new);
108 q
->l_next
= p
->l_next
;
111 q
->l_next
->l_prev
= q
;
115 ipmi_list_delete(ipmi_list_t
*lp
, void *existing
)
117 ipmi_list_t
*p
= existing
;
119 if (p
->l_prev
!= NULL
)
120 p
->l_prev
->l_next
= p
->l_next
;
122 lp
->l_next
= p
->l_next
;
124 if (p
->l_next
!= NULL
)
125 p
->l_next
->l_prev
= p
->l_prev
;
127 lp
->l_prev
= p
->l_prev
;