blob: e5791c7b5ec5290282b3580156f6e101fc6776e3 [file] [log] [blame]
Wedson Almeida Filho1e9c3312018-12-07 16:27:44 +00001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Wedson Almeida Filho1e9c3312018-12-07 16:27:44 +00003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * 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 Filho1e9c3312018-12-07 16:27:44 +00007 */
8
9#pragma once
10
11#include <stdbool.h>
12#include <stddef.h>
13
14struct list_entry {
15 struct list_entry *next;
16 struct list_entry *prev;
17};
18
Karl Meakin2ad6b662024-07-29 20:45:40 +010019#define LIST_INIT(l) {.next = &(l), .prev = &(l)}
20
Wedson Almeida Filho1e9c3312018-12-07 16:27:44 +000021#define CONTAINER_OF(ptr, type, field) \
Karl Meakin2ad6b662024-07-29 20:45:40 +010022 ((type *)((char *)(ptr) - offsetof(type, field)))
Wedson Almeida Filho1e9c3312018-12-07 16:27:44 +000023
24static inline void list_init(struct list_entry *e)
25{
26 e->next = e;
27 e->prev = e;
28}
29
30static inline void list_append(struct list_entry *l, struct list_entry *e)
31{
32 e->next = l;
33 e->prev = l->prev;
34
35 e->next->prev = e;
36 e->prev->next = e;
37}
38
39static inline void list_prepend(struct list_entry *l, struct list_entry *e)
40{
41 e->next = l->next;
42 e->prev = l;
43
44 e->next->prev = e;
45 e->prev->next = e;
46}
47
48static inline bool list_empty(struct list_entry *l)
49{
50 return l->next == l;
51}
52
53static inline void list_remove(struct list_entry *e)
54{
55 e->prev->next = e->next;
56 e->next->prev = e->prev;
57 list_init(e);
58}