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