| 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 | #define container_of(ptr, type, member) \ |
| 10 | ((type *)((void *)(ptr) - offsetof(type, member))) |
| 11 | |
| 12 | struct slist_elm { |
| 13 | struct slist_elm *next; |
| 14 | }; |
| 15 | |
| 16 | static inline void slist_init(struct slist_elm *head) |
| 17 | { |
| 18 | head->next = NULL; |
| 19 | } |
| 20 | |
| 21 | static inline struct slist_elm * slist_first(struct slist_elm *head) |
| 22 | { |
| 23 | return head->next; |
| 24 | } |
| 25 | |
| 26 | static inline _Bool slist_empty(const struct slist_elm *head) |
| 27 | { |
| 28 | return head->next == NULL; |
| 29 | } |
| 30 | |
| 31 | static inline void slist_remove_next(struct slist_elm *elm) |
| 32 | { |
| 33 | if (elm->next) |
| 34 | elm->next = elm->next->next; |
| 35 | } |
| 36 | |
| 37 | static inline void slist_remove(struct slist_elm *head, struct slist_elm *elm) |
| 38 | { |
| 39 | struct slist_elm *prev; |
| 40 | |
| 41 | if (head->next == elm) { |
| 42 | head->next = elm->next; |
| 43 | } else { |
| 44 | for (prev = head->next; prev && prev->next != elm; ) |
| 45 | prev = prev->next; |
| 46 | if (prev) |
| 47 | slist_remove_next(prev); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static inline void slist_append(struct slist_elm *head, struct slist_elm *elm) |
| 52 | { |
| 53 | elm->next = head->next; |
| 54 | head->next = elm; |
| 55 | } |
| 56 | |
| 57 | #endif /* __LIGHTREC_SLIST_H__ */ |