1706f2543Smrg/*
2706f2543Smrg * Copyright © 2010 Intel Corporation
3706f2543Smrg * Copyright © 2010 Francisco Jerez <currojerez@riseup.net>
4706f2543Smrg *
5706f2543Smrg * Permission is hereby granted, free of charge, to any person obtaining a
6706f2543Smrg * copy of this software and associated documentation files (the "Software"),
7706f2543Smrg * to deal in the Software without restriction, including without limitation
8706f2543Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9706f2543Smrg * and/or sell copies of the Software, and to permit persons to whom the
10706f2543Smrg * Software is furnished to do so, subject to the following conditions:
11706f2543Smrg *
12706f2543Smrg * The above copyright notice and this permission notice (including the next
13706f2543Smrg * paragraph) shall be included in all copies or substantial portions of the
14706f2543Smrg * Software.
15706f2543Smrg *
16706f2543Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17706f2543Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18706f2543Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19706f2543Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20706f2543Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21706f2543Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22706f2543Smrg * IN THE SOFTWARE.
23706f2543Smrg *
24706f2543Smrg */
25706f2543Smrg
26706f2543Smrg#ifndef _LIST_H_
27706f2543Smrg#define _LIST_H_
28706f2543Smrg
29706f2543Smrg/* classic doubly-link circular list */
30706f2543Smrgstruct list {
31706f2543Smrg    struct list *next, *prev;
32706f2543Smrg};
33706f2543Smrg
34706f2543Smrgstatic void
35706f2543Smrglist_init(struct list *list)
36706f2543Smrg{
37706f2543Smrg    list->next = list->prev = list;
38706f2543Smrg}
39706f2543Smrg
40706f2543Smrgstatic inline void
41706f2543Smrg__list_add(struct list *entry,
42706f2543Smrg	    struct list *prev,
43706f2543Smrg	    struct list *next)
44706f2543Smrg{
45706f2543Smrg    next->prev = entry;
46706f2543Smrg    entry->next = next;
47706f2543Smrg    entry->prev = prev;
48706f2543Smrg    prev->next = entry;
49706f2543Smrg}
50706f2543Smrg
51706f2543Smrgstatic inline void
52706f2543Smrglist_add(struct list *entry, struct list *head)
53706f2543Smrg{
54706f2543Smrg    __list_add(entry, head, head->next);
55706f2543Smrg}
56706f2543Smrg
57706f2543Smrgstatic inline void
58706f2543Smrg__list_del(struct list *prev, struct list *next)
59706f2543Smrg{
60706f2543Smrg    next->prev = prev;
61706f2543Smrg    prev->next = next;
62706f2543Smrg}
63706f2543Smrg
64706f2543Smrgstatic inline void
65706f2543Smrglist_del(struct list *entry)
66706f2543Smrg{
67706f2543Smrg    __list_del(entry->prev, entry->next);
68706f2543Smrg    list_init(entry);
69706f2543Smrg}
70706f2543Smrg
71706f2543Smrgstatic inline Bool
72706f2543Smrglist_is_empty(const struct list *head)
73706f2543Smrg{
74706f2543Smrg    return head->next == head;
75706f2543Smrg}
76706f2543Smrg
77706f2543Smrg#ifndef container_of
78706f2543Smrg#define container_of(ptr, type, member) \
79706f2543Smrg    (type *)((char *)(ptr) - (char *) &((type *)0)->member)
80706f2543Smrg#endif
81706f2543Smrg
82706f2543Smrg#define list_entry(ptr, type, member) \
83706f2543Smrg    container_of(ptr, type, member)
84706f2543Smrg
85706f2543Smrg#define list_first_entry(ptr, type, member) \
86706f2543Smrg    list_entry((ptr)->next, type, member)
87706f2543Smrg
88706f2543Smrg#define __container_of(ptr, sample, member)				\
89706f2543Smrg    (void *)((char *)(ptr)						\
90706f2543Smrg	     - ((char *)&(sample)->member - (char *)(sample)))
91706f2543Smrg
92706f2543Smrg#define list_for_each_entry(pos, head, member)				\
93706f2543Smrg    for (pos = __container_of((head)->next, pos, member);		\
94706f2543Smrg	 &pos->member != (head);					\
95706f2543Smrg	 pos = __container_of(pos->member.next, pos, member))
96706f2543Smrg
97706f2543Smrg#define list_for_each_entry_safe(pos, tmp, head, member)		\
98706f2543Smrg    for (pos = __container_of((head)->next, pos, member),		\
99706f2543Smrg	 tmp = __container_of(pos->member.next, pos, member);		\
100706f2543Smrg	 &pos->member != (head);					\
101706f2543Smrg	 pos = tmp, tmp = __container_of(pos->member.next, tmp, member))
102706f2543Smrg
103706f2543Smrgstatic inline void
104706f2543Smrglist_append(struct list *entry, struct list *head)
105706f2543Smrg{
106706f2543Smrg    __list_add(entry, head, head->next);
107706f2543Smrg}
108706f2543Smrg
109706f2543Smrg#endif
110