Package version up to 2.7.1
[platform/core/uifw/libtdm.git] / include / tdm_list.h
1 /*
2  *
3  * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20  * USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  */
27
28 /**
29  * \file
30  * List macros heavily inspired by the Linux kernel
31  * list handling. No list looping yet.
32  *
33  * Is not threadsafe, so common operations need to
34  * be protected using an external mutex.
35  */
36 #ifndef _U_DOUBLE_LIST_H_
37 #define _U_DOUBLE_LIST_H_
38
39 #include <stddef.h>
40
41 struct list_head {
42         struct list_head *prev;
43         struct list_head *next;
44 };
45
46 static inline void list_inithead(struct list_head *item)
47 {
48         item->prev = item;
49         item->next = item;
50 }
51
52 static inline void list_add(struct list_head *item, struct list_head *list)
53 {
54         item->prev = list;
55         item->next = list->next;
56         list->next->prev = item;
57         list->next = item;
58 }
59
60 static inline void list_addtail(struct list_head *item, struct list_head *list)
61 {
62         item->next = list;
63         item->prev = list->prev;
64         list->prev->next = item;
65         list->prev = item;
66 }
67
68 static inline void list_replace(struct list_head *from, struct list_head *to)
69 {
70         to->prev = from->prev;
71         to->next = from->next;
72         from->next->prev = to;
73         from->prev->next = to;
74 }
75
76 static inline void list_del(struct list_head *item)
77 {
78         item->prev->next = item->next;
79         item->next->prev = item->prev;
80 }
81
82 static inline void list_delinit(struct list_head *item)
83 {
84         item->prev->next = item->next;
85         item->next->prev = item->prev;
86         item->next = item;
87         item->prev = item;
88 }
89
90 static inline int list_length(struct list_head *item)
91 {
92         struct list_head *next;
93         int length = 0;
94
95         next = item->next;
96         while (next != item) {
97                 length++;
98                 next = next->next;
99         }
100
101         return length;
102 }
103
104 #define LIST_INITHEAD(__item) list_inithead(__item)
105 #define LIST_ADD(__item, __list) list_add(__item, __list)
106 #define LIST_ADDTAIL(__item, __list) list_addtail(__item, __list)
107 #define LIST_REPLACE(__from, __to) list_replace(__from, __to)
108 #define LIST_DEL(__item) list_del(__item)
109 #define LIST_DELINIT(__item) list_delinit(__item)
110 #define LIST_LENGTH(__item) list_length(__item)
111
112 #define LIST_ENTRY(__type, __item, __field)   \
113         ((__type *)(((char *)(__item)) - offsetof(__type, __field)))
114
115 #define LIST_FIRST_ENTRY(__ptr, __type, __field)   \
116         LIST_ENTRY(__type, (__ptr)->next, __field)
117
118 #define LIST_LAST_ENTRY(__ptr, __type, __field)   \
119         LIST_ENTRY(__type, (__ptr)->prev, __field)
120
121 #define LIST_IS_EMPTY(__list)                   \
122         ((__list)->next == (__list))
123
124 #ifndef container_of
125 #define container_of(ptr, sample, member)                               \
126         (void *)((char *)(ptr)                                          \
127              - ((char *)&(sample)->member - (char *)(sample)))
128 #endif
129
130 #define LIST_FOR_EACH_ENTRY(pos, head, member)                          \
131         for (pos = container_of((head)->next, pos, member);                     \
132         &pos->member != (head);                                         \
133         pos = container_of(pos->member.next, pos, member))
134
135 #define LIST_FOR_EACH_ENTRY_REV(pos, head, member)                              \
136         for (pos = container_of((head)->prev, pos, member);                     \
137         &pos->member != (head);                                         \
138         pos = container_of(pos->member.prev, pos, member))
139
140 #define LIST_FOR_EACH_ENTRY_SAFE(pos, storage, head, member)    \
141         for (pos = container_of((head)->next, pos, member),                     \
142         storage = container_of(pos->member.next, pos, member);  \
143         &pos->member != (head);                                         \
144         pos = storage, storage = container_of(storage->member.next, storage, member))
145
146 #define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member)        \
147         for (pos = container_of((head)->prev, pos, member),                     \
148         storage = container_of(pos->member.prev, pos, member);          \
149         &pos->member != (head);                                         \
150         pos = storage, storage = container_of(storage->member.prev, storage, member))
151
152 #define LIST_FOR_EACH_ENTRY_FROM(pos, start, head, member)              \
153         for (pos = container_of((start), pos, member);                  \
154         &pos->member != (head);                                         \
155         pos = container_of(pos->member.next, pos, member))
156
157 #define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member)          \
158         for (pos = container_of((start), pos, member);                  \
159         &pos->member != (head);                                         \
160         pos = container_of(pos->member.prev, pos, member))
161
162 #define LIST_FIND_ITEM(item, head, type, member, found) \
163         do {    \
164                 type *pos = NULL;       \
165                 found = NULL;   \
166                 LIST_FOR_EACH_ENTRY(pos, head, member)  \
167                         if (pos == item) { found = item; break; }       \
168         } while (0)
169
170 #endif /*_U_DOUBLE_LIST_H_*/