| 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | /* |
| 3 | * Copyright (C) 2020-2021 Paul Cercueil <paul@crapouillou.net> |
| 4 | */ |
| 5 | |
| 6 | #ifndef __LIGHTREC_SLIST_H__ |
| 7 | #define __LIGHTREC_SLIST_H__ |
| 8 | |
| 9 | #include <stddef.h> |
| 10 | |
| 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__ */ |