blob: db0248c55199e285dabe8944f966d4254896df34 [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
Wedson Almeida Filho1e9c3312018-12-07 16:27:44 +000011#include <stddef.h>
12
13struct list_entry {
14 struct list_entry *next;
15 struct list_entry *prev;
16};
17
Karl Meakin2ad6b662024-07-29 20:45:40 +010018#define LIST_INIT(l) {.next = &(l), .prev = &(l)}
19
Wedson Almeida Filho1e9c3312018-12-07 16:27:44 +000020#define CONTAINER_OF(ptr, type, field) \
Karl Meakin2ad6b662024-07-29 20:45:40 +010021 ((type *)((char *)(ptr) - offsetof(type, field)))
Wedson Almeida Filho1e9c3312018-12-07 16:27:44 +000022
23static inline void list_init(struct list_entry *e)
24{
25 e->next = e;
26 e->prev = e;
27}
28
J-Alves8e021862024-10-03 11:28:46 +010029static inline void list_prepend(struct list_entry *l, struct list_entry *e)
Wedson Almeida Filho1e9c3312018-12-07 16:27:44 +000030{
31 e->next = l;
32 e->prev = l->prev;
33
34 e->next->prev = e;
35 e->prev->next = e;
36}
37
J-Alves8e021862024-10-03 11:28:46 +010038static inline void list_append(struct list_entry *l, struct list_entry *e)
Wedson Almeida Filho1e9c3312018-12-07 16:27:44 +000039{
40 e->next = l->next;
41 e->prev = l;
42
43 e->next->prev = e;
44 e->prev->next = e;
45}
46
47static inline bool list_empty(struct list_entry *l)
48{
49 return l->next == l;
50}
51
52static 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}