blob: 3ff71ae0dcc83f3078378b942ba9b80445e9e22a [file] [log] [blame]
Wedson Almeida Filho1e9c3312018-12-07 16:27:44 +00001/*
2 * Copyright 2018 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <stdbool.h>
20#include <stddef.h>
21
22struct list_entry {
23 struct list_entry *next;
24 struct list_entry *prev;
25};
26
27#define LIST_INIT(l) \
28 { \
29 .next = &l, .prev = &l \
30 }
31#define CONTAINER_OF(ptr, type, field) \
32 ((type *)((char *)ptr - offsetof(type, field)))
33
34static inline void list_init(struct list_entry *e)
35{
36 e->next = e;
37 e->prev = e;
38}
39
40static inline void list_append(struct list_entry *l, struct list_entry *e)
41{
42 e->next = l;
43 e->prev = l->prev;
44
45 e->next->prev = e;
46 e->prev->next = e;
47}
48
49static inline void list_prepend(struct list_entry *l, struct list_entry *e)
50{
51 e->next = l->next;
52 e->prev = l;
53
54 e->next->prev = e;
55 e->prev->next = e;
56}
57
58static inline bool list_empty(struct list_entry *l)
59{
60 return l->next == l;
61}
62
63static inline void list_remove(struct list_entry *e)
64{
65 e->prev->next = e->next;
66 e->next->prev = e->prev;
67 list_init(e);
68}