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
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]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Embedded Linked Lists
32 * Simple doubly-linked list implementation. This implementation assumes that
33 * each list element contains an embedded fmd_list_t (previous and next
34 * pointers), which is typically the first member of the element struct.
35 * An additional fmd_list_t is used to store the head (l_next) and tail
36 * (l_prev) pointers. The current head and tail list elements have their
37 * previous and next pointers set to NULL, respectively.
39 * NOTE: The embeddable list code in this file intentionally provides no
40 * locking of any kind. The implementation of any list in fmd must provide
41 * an appropriate locking strategy to protect the list or to protect access
42 * to the embedded fmd_list_t inside of each list element to avoid corruption.
43 * Refer to comments in the source files that use fmd_list_t for lock details.
50 fmd_list_append(fmd_list_t
*lp
, void *new)
52 fmd_list_t
*p
= lp
->l_prev
; /* p = tail list element */
53 fmd_list_t
*q
= new; /* q = new list element */
60 ASSERT(p
->l_next
== NULL
);
63 ASSERT(lp
->l_next
== NULL
);
69 fmd_list_prepend(fmd_list_t
*lp
, void *new)
71 fmd_list_t
*p
= new; /* p = new list element */
72 fmd_list_t
*q
= lp
->l_next
; /* q = head list element */
79 ASSERT(q
->l_prev
== NULL
);
82 ASSERT(lp
->l_prev
== NULL
);
88 fmd_list_insert_before(fmd_list_t
*lp
, void *before_me
, void *new)
90 fmd_list_t
*p
= before_me
;
93 if (p
== NULL
|| p
->l_prev
== NULL
) {
94 fmd_list_prepend(lp
, new);
98 q
->l_prev
= p
->l_prev
;
101 q
->l_prev
->l_next
= q
;
105 fmd_list_insert_after(fmd_list_t
*lp
, void *after_me
, void *new)
107 fmd_list_t
*p
= after_me
;
110 if (p
== NULL
|| p
->l_next
== NULL
) {
111 fmd_list_append(lp
, new);
115 q
->l_next
= p
->l_next
;
118 q
->l_next
->l_prev
= q
;
122 fmd_list_delete(fmd_list_t
*lp
, void *existing
)
124 fmd_list_t
*p
= existing
;
126 if (p
->l_prev
!= NULL
)
127 p
->l_prev
->l_next
= p
->l_next
;
129 lp
->l_next
= p
->l_next
;
131 if (p
->l_next
!= NULL
)
132 p
->l_next
->l_prev
= p
->l_prev
;
134 lp
->l_prev
= p
->l_prev
;