1 /*
2  * Copyright (c) 2002-2004 MontaVista Software, Inc.
3  *
4  * All rights reserved.
5  *
6  * Author: Steven Dake (sdake@redhat.com)
7  *
8  * This software licensed under BSD license, the text of which follows:
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * - Redistributions of source code must retain the above copyright notice,
14  *   this list of conditions and the following disclaimer.
15  * - Redistributions in binary form must reproduce the above copyright notice,
16  *   this list of conditions and the following disclaimer in the documentation
17  *   and/or other materials provided with the distribution.
18  * - Neither the name of the MontaVista Software, Inc. nor the names of its
19  *   contributors may be used to endorse or promote products derived from this
20  *   software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /**
36  * @file
37  * Linked list API
38  *
39  * This implementation uses the same API as the linux kernel to
40  * help us kernel developers easily use the list primatives
41  */
42 
43 #ifndef LIST_H_DEFINED
44 #define LIST_H_DEFINED
45 
46 struct list_head {
47 	struct list_head *next;
48 	struct list_head *prev;
49 };
50 
51 #define DECLARE_LIST_INIT(name) \
52     struct list_head name = { &(name), &(name) }
53 
list_init(struct list_head * head)54 static void inline list_init (struct list_head *head)
55 {
56 	head->next = head;
57 	head->prev = head;
58 }
59 
list_add(struct list_head * element,struct list_head * head)60 static void inline list_add (struct list_head *element, struct list_head *head)
61 {
62 	head->next->prev = element;
63 	element->next = head->next;
64 	element->prev = head;
65 	head->next = element;
66 }
list_add_tail(struct list_head * element,struct list_head * head)67 static void inline list_add_tail (struct list_head *element, struct list_head *head)
68 {
69 	head->prev->next = element;
70 	element->next = head;
71 	element->prev = head->prev;
72 	head->prev = element;
73 }
list_del(struct list_head * _remove)74 static void inline list_del (struct list_head *_remove)
75 {
76 	_remove->next->prev = _remove->prev;
77 	_remove->prev->next = _remove->next;
78 #ifdef DEBUG
79 	_remove->next = (struct list_head *)0xdeadb33f;
80 	_remove->prev = (struct list_head *)0xdeadb33f;
81 #endif
82 }
83 
84 #define list_entry(ptr,type,member)\
85 	((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
86 
list_empty(const struct list_head * l)87 static inline int list_empty(const struct list_head *l)
88 {
89 	return l->next == l;
90 }
91 
list_splice(struct list_head * list,struct list_head * head)92 static inline void list_splice (struct list_head *list, struct list_head *head)
93 {
94 	struct list_head *first;
95 	struct list_head *last;
96 	struct list_head *current;
97 
98 	first = list->next;
99 	last = list->prev;
100 	current = head->next;
101 
102 	first->prev = head;
103 	head->next = first;
104 	last->next = current;
105 	current->prev = last;
106 }
107 
108 #endif /* LIST_H_DEFINED */
109