Wedson Almeida Filho | 1e9c331 | 2018-12-07 16:27:44 +0000 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Wedson Almeida Filho | 1e9c331 | 2018-12-07 16:27:44 +0000 | [diff] [blame] | 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
Wedson Almeida Filho | 1e9c331 | 2018-12-07 16:27:44 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | #include <stdbool.h> |
| 12 | #include <stddef.h> |
| 13 | |
| 14 | struct list_entry { |
| 15 | struct list_entry *next; |
| 16 | struct list_entry *prev; |
| 17 | }; |
| 18 | |
Karl Meakin | 66a38bd | 2024-05-28 16:00:56 +0100 | [diff] [blame] | 19 | #define LIST_INIT(l) {.next = &l, .prev = &l} |
Wedson Almeida Filho | 1e9c331 | 2018-12-07 16:27:44 +0000 | [diff] [blame] | 20 | #define CONTAINER_OF(ptr, type, field) \ |
| 21 | ((type *)((char *)ptr - offsetof(type, field))) |
| 22 | |
| 23 | static inline void list_init(struct list_entry *e) |
| 24 | { |
| 25 | e->next = e; |
| 26 | e->prev = e; |
| 27 | } |
| 28 | |
| 29 | static inline void list_append(struct list_entry *l, struct list_entry *e) |
| 30 | { |
| 31 | e->next = l; |
| 32 | e->prev = l->prev; |
| 33 | |
| 34 | e->next->prev = e; |
| 35 | e->prev->next = e; |
| 36 | } |
| 37 | |
| 38 | static inline void list_prepend(struct list_entry *l, struct list_entry *e) |
| 39 | { |
| 40 | e->next = l->next; |
| 41 | e->prev = l; |
| 42 | |
| 43 | e->next->prev = e; |
| 44 | e->prev->next = e; |
| 45 | } |
| 46 | |
| 47 | static inline bool list_empty(struct list_entry *l) |
| 48 | { |
| 49 | return l->next == l; |
| 50 | } |
| 51 | |
| 52 | static inline void list_remove(struct list_entry *e) |
| 53 | { |
| 54 | e->prev->next = e->next; |
| 55 | e->next->prev = e->prev; |
| 56 | list_init(e); |
| 57 | } |