blob: c3488e3a3ec4302d3f69bdb7bd7221235f1f8147 [file] [log] [blame]
Amit Pundird477f822020-02-07 22:26:08 +05301/*
2 * Copyright (c) 2016, Linaro Ltd.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31#ifndef __LIST_H__
32#define __LIST_H__
33
34#include <stdbool.h>
35#include <stddef.h>
36
37#ifndef container_of
38#define container_of(ptr, type, member) ({ \
39 const typeof(((type *)0)->member)*__mptr = (ptr); \
40 (type *)((char *)__mptr - offsetof(type, member)); \
41 })
42#endif
43
44struct list_head {
45 struct list_head *prev;
46 struct list_head *next;
47};
48
49#define LIST_INIT(list) { &(list), &(list) }
50
51static inline void list_init(struct list_head *list)
52{
53 list->prev = list->next = list;
54}
55
56static inline bool list_empty(struct list_head *list)
57{
58 return list->next == list;
59}
60
61static inline void list_add(struct list_head *list, struct list_head *item)
62{
63 struct list_head *prev = list->prev;
64
65 item->next = list;
66 item->prev = prev;
67
68 prev->next = list->prev = item;
69}
70
71static inline void list_del(struct list_head *item)
72{
73 item->prev->next = item->next;
74 item->next->prev = item->prev;
75}
76
77#define list_for_each(item, list) \
78 for (item = (list)->next; item != list; item = item->next)
79
80#define list_for_each_safe(item, next, list) \
81 for (item = (list)->next, next = item->next; item != list; item = next, next = item->next)
82
83#define list_entry(item, type, member) \
84 container_of(item, type, member)
85
86#define list_entry_first(list, type, member) \
87 container_of((list)->next, type, member)
88
89#define list_entry_next(item, member) \
90 container_of((item)->member.next, typeof(*(item)), member)
91
92#define list_for_each_entry(item, list, member) \
93 for (item = list_entry_first(list, typeof(*(item)), member); \
94 &item->member != list; \
95 item = list_entry_next(item, member))
96
97#define list_for_each_entry_safe(item, next, list, member) \
98 for (item = list_entry_first(list, typeof(*(item)), member), \
99 next = list_entry_next(item, member); \
100 &item->member != list; \
101 item = next, \
102 next = list_entry_next(item, member)) \
103
104#endif