| 1 | /* |
| 2 | * Copyright (C) 2020 Paul Cercueil <paul@crapouillou.net> |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #ifndef __LIGHTREC_SLIST_H__ |
| 16 | #define __LIGHTREC_SLIST_H__ |
| 17 | |
| 18 | #define container_of(ptr, type, member) \ |
| 19 | ((type *)((void *)(ptr) - offsetof(type, member))) |
| 20 | |
| 21 | struct slist_elm { |
| 22 | struct slist_elm *next; |
| 23 | }; |
| 24 | |
| 25 | static inline void slist_init(struct slist_elm *head) |
| 26 | { |
| 27 | head->next = NULL; |
| 28 | } |
| 29 | |
| 30 | static inline struct slist_elm * slist_first(struct slist_elm *head) |
| 31 | { |
| 32 | return head->next; |
| 33 | } |
| 34 | |
| 35 | static inline _Bool slist_empty(const struct slist_elm *head) |
| 36 | { |
| 37 | return head->next == NULL; |
| 38 | } |
| 39 | |
| 40 | static inline void slist_remove_next(struct slist_elm *elm) |
| 41 | { |
| 42 | if (elm->next) |
| 43 | elm->next = elm->next->next; |
| 44 | } |
| 45 | |
| 46 | static inline void slist_remove(struct slist_elm *head, struct slist_elm *elm) |
| 47 | { |
| 48 | struct slist_elm *prev; |
| 49 | |
| 50 | if (head->next == elm) { |
| 51 | head->next = elm->next; |
| 52 | } else { |
| 53 | for (prev = head->next; prev && prev->next != elm; ) |
| 54 | prev = prev->next; |
| 55 | if (prev) |
| 56 | slist_remove_next(prev); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static inline void slist_append(struct slist_elm *head, struct slist_elm *elm) |
| 61 | { |
| 62 | elm->next = head->next; |
| 63 | head->next = elm; |
| 64 | } |
| 65 | |
| 66 | #endif /* __LIGHTREC_SLIST_H__ */ |