| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2003-2006 Thomas Graf <[email protected]> |
| 4 |
*/ |
| 5 |
|
| 6 |
#ifndef NETLINK_LIST_H_ |
| 7 |
#define NETLINK_LIST_H_ |
| 8 |
|
| 9 |
/* For internal uses consider using "third_party/c-list/src/c-list.h" instead. |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <stddef.h> |
| 13 |
|
| 14 |
#ifdef __cplusplus |
| 15 |
extern "C" { |
| 16 |
#endif |
| 17 |
|
| 18 |
struct nl_list_head |
| 19 |
{ |
| 20 |
struct nl_list_head * next; |
| 21 |
struct nl_list_head * prev; |
| 22 |
}; |
| 23 |
|
| 24 |
static inline void NL_INIT_LIST_HEAD(struct nl_list_head *list) |
| 25 |
{ |
| 26 |
list->next = list; |
| 27 |
list->prev = list; |
| 28 |
} |
| 29 |
|
| 30 |
static inline void __nl_list_add(struct nl_list_head *obj, |
| 31 |
struct nl_list_head *prev, |
| 32 |
struct nl_list_head *next) |
| 33 |
{ |
| 34 |
prev->next = obj; |
| 35 |
obj->prev = prev; |
| 36 |
next->prev = obj; |
| 37 |
obj->next = next; |
| 38 |
} |
| 39 |
|
| 40 |
static inline void nl_list_add_tail(struct nl_list_head *obj, |
| 41 |
struct nl_list_head *head) |
| 42 |
{ |
| 43 |
__nl_list_add(obj, head->prev, head); |
| 44 |
} |
| 45 |
|
| 46 |
static inline void nl_list_add_head(struct nl_list_head *obj, |
| 47 |
struct nl_list_head *head) |
| 48 |
{ |
| 49 |
__nl_list_add(obj, head, head->next); |
| 50 |
} |
| 51 |
|
| 52 |
static inline void nl_list_del(struct nl_list_head *obj) |
| 53 |
{ |
| 54 |
obj->next->prev = obj->prev; |
| 55 |
obj->prev->next = obj->next; |
| 56 |
} |
| 57 |
|
| 58 |
static inline int nl_list_empty(struct nl_list_head *head) |
| 59 |
{ |
| 60 |
return head->next == head; |
| 61 |
} |
| 62 |
|
| 63 |
#define nl_container_of(ptr, type, member) ({ \ |
| 64 |
const __typeof__( ((type *)0)->member ) *__mptr = (ptr);\ |
| 65 |
(type *)( (char *)__mptr - (offsetof(type, member)));}) |
| 66 |
|
| 67 |
#define nl_list_entry(ptr, type, member) \ |
| 68 |
nl_container_of(ptr, type, member) |
| 69 |
|
| 70 |
#define nl_list_at_tail(pos, head, member) \ |
| 71 |
((pos)->member.next == (head)) |
| 72 |
|
| 73 |
#define nl_list_at_head(pos, head, member) \ |
| 74 |
((pos)->member.prev == (head)) |
| 75 |
|
| 76 |
#define NL_LIST_HEAD(name) \ |
| 77 |
struct nl_list_head name = { &(name), &(name) } |
| 78 |
|
| 79 |
#define nl_list_first_entry(head, type, member) \ |
| 80 |
nl_list_entry((head)->next, type, member) |
| 81 |
|
| 82 |
#define nl_list_for_each_entry(pos, head, member) \ |
| 83 |
for (pos = nl_list_entry((head)->next, __typeof__(*pos), member); \ |
| 84 |
&(pos)->member != (head); \ |
| 85 |
(pos) = nl_list_entry((pos)->member.next, __typeof__(*(pos)), member)) |
| 86 |
|
| 87 |
#define nl_list_for_each_entry_safe(pos, n, head, member) \ |
| 88 |
for (pos = nl_list_entry((head)->next, __typeof__(*pos), member), \ |
| 89 |
n = nl_list_entry(pos->member.next, __typeof__(*pos), member); \ |
| 90 |
&(pos)->member != (head); \ |
| 91 |
pos = n, n = nl_list_entry(n->member.next, __typeof__(*n), member)) |
| 92 |
|
| 93 |
#define nl_init_list_head(head) \ |
| 94 |
do { (head)->next = (head); (head)->prev = (head); } while (0) |
| 95 |
|
| 96 |
#ifdef __cplusplus |
| 97 |
} |
| 98 |
#endif |
| 99 |
|
| 100 |
#endif |