dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libipmi / common / ipmi_list.c
blob2c0cae20ab660cad76eb02e84bc44285917ef570
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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.
39 #include <assert.h>
40 #include <ipmi_impl.h>
42 void
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 */
48 lp->l_prev = q;
49 q->l_prev = p;
50 q->l_next = NULL;
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;
61 void
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 */
67 lp->l_next = p;
68 p->l_prev = NULL;
69 p->l_next = q;
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;
80 void
81 ipmi_list_insert_before(ipmi_list_t *lp, void *before_me, void *new)
83 ipmi_list_t *p = before_me;
84 ipmi_list_t *q = new;
86 if (p == NULL || p->l_prev == NULL) {
87 ipmi_list_prepend(lp, new);
88 return;
91 q->l_prev = p->l_prev;
92 q->l_next = p;
93 p->l_prev = q;
94 q->l_prev->l_next = q;
97 void
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);
105 return;
108 q->l_next = p->l_next;
109 q->l_prev = p;
110 p->l_next = q;
111 q->l_next->l_prev = q;
114 void
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;
121 else
122 lp->l_next = p->l_next;
124 if (p->l_next != NULL)
125 p->l_next->l_prev = p->l_prev;
126 else
127 lp->l_prev = p->l_prev;