blob: 0f200ce69048f80926f5849b4dec6c78126f9b92 [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
19#define LIST_INIT(l) \
20 { \
21 .next = &l, .prev = &l \
22 }
23#define CONTAINER_OF(ptr, type, field) \
24 ((type *)((char *)ptr - offsetof(type, field)))
25
26static inline void list_init(struct list_entry *e)
27{
28 e->next = e;
29 e->prev = e;
30}
31
32static inline void list_append(struct list_entry *l, struct list_entry *e)
33{
34 e->next = l;
35 e->prev = l->prev;
36
37 e->next->prev = e;
38 e->prev->next = e;
39}
40
41static inline void list_prepend(struct list_entry *l, struct list_entry *e)
42{
43 e->next = l->next;
44 e->prev = l;
45
46 e->next->prev = e;
47 e->prev->next = e;
48}
49
50static inline bool list_empty(struct list_entry *l)
51{
52 return l->next == l;
53}
54
55static inline void list_remove(struct list_entry *e)
56{
57 e->prev->next = e->next;
58 e->next->prev = e->prev;
59 list_init(e);
60}