Commit | Line | Data |
---|---|---|
98fa08a5 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a59e5536 | 2 | /* |
98fa08a5 | 3 | * Copyright (C) 2020-2021 Paul Cercueil <paul@crapouillou.net> |
a59e5536 | 4 | */ |
5 | ||
6 | #ifndef __LIGHTREC_SLIST_H__ | |
7 | #define __LIGHTREC_SLIST_H__ | |
8 | ||
de742fa0 PC |
9 | #include <stddef.h> |
10 | ||
a59e5536 | 11 | #define container_of(ptr, type, member) \ |
12 | ((type *)((void *)(ptr) - offsetof(type, member))) | |
13 | ||
14 | struct slist_elm { | |
15 | struct slist_elm *next; | |
16 | }; | |
17 | ||
18 | static inline void slist_init(struct slist_elm *head) | |
19 | { | |
20 | head->next = NULL; | |
21 | } | |
22 | ||
23 | static inline struct slist_elm * slist_first(struct slist_elm *head) | |
24 | { | |
25 | return head->next; | |
26 | } | |
27 | ||
28 | static inline _Bool slist_empty(const struct slist_elm *head) | |
29 | { | |
30 | return head->next == NULL; | |
31 | } | |
32 | ||
33 | static inline void slist_remove_next(struct slist_elm *elm) | |
34 | { | |
35 | if (elm->next) | |
36 | elm->next = elm->next->next; | |
37 | } | |
38 | ||
39 | static inline void slist_remove(struct slist_elm *head, struct slist_elm *elm) | |
40 | { | |
41 | struct slist_elm *prev; | |
42 | ||
43 | if (head->next == elm) { | |
44 | head->next = elm->next; | |
45 | } else { | |
46 | for (prev = head->next; prev && prev->next != elm; ) | |
47 | prev = prev->next; | |
48 | if (prev) | |
49 | slist_remove_next(prev); | |
50 | } | |
51 | } | |
52 | ||
53 | static inline void slist_append(struct slist_elm *head, struct slist_elm *elm) | |
54 | { | |
55 | elm->next = head->next; | |
56 | head->next = elm; | |
57 | } | |
58 | ||
59 | #endif /* __LIGHTREC_SLIST_H__ */ |