Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/utils/list.h
1476 views
1
/*
2
* Copyright (c) 2018 naehrwert
3
* Copyright (c) 2020 CTCaer
4
*
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms and conditions of the GNU General Public License,
7
* version 2, as published by the Free Software Foundation.
8
*
9
* This program is distributed in the hope it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
* more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#ifndef _LIST_H_
19
#define _LIST_H_
20
21
#include <utils/types.h>
22
23
/*! Initialize list. */
24
#define LIST_INIT(name) link_t name = {&name, &name}
25
26
/*! Initialize static list. */
27
#define LIST_INIT_STATIC(name) static link_t name = {&name, &name}
28
29
/*! Iterate over all list links. */
30
#define LIST_FOREACH(iter, list) \
31
for (link_t *iter = (list)->next; iter != (list); iter = iter->next)
32
33
/*! Iterate over all list links backwards. */
34
#define LIST_FOREACH_INVERSE(iter, list) \
35
for (link_t *iter = (list)->prev; iter != (list); iter = iter->prev)
36
37
/*! Safely iterate over all list links. */
38
#define LIST_FOREACH_SAFE(iter, list) \
39
for (link_t *iter = (list)->next, *safe = iter->next; iter != (list); iter = safe, safe = iter->next)
40
41
/*! Iterate over all list members and make sure that the list has at least one entry. */
42
#define LIST_FOREACH_ENTRY(etype, iter, list, mn) \
43
if ((list)->next != (list)) \
44
for (etype *iter = CONTAINER_OF((list)->next, etype, mn); &iter->mn != (list); iter = CONTAINER_OF(iter->mn.next, etype, mn))
45
46
/* Iterate over all list members backwards and make sure that the list has at least one entry. */
47
#define LIST_FOREACH_ENTRY_INVERSE(type, iter, list, mn) \
48
if ((list)->prev != (list)) \
49
for (type *iter = CONTAINER_OF((list)->prev, type, mn); &iter->mn != (list); iter = CONTAINER_OF(iter->mn.prev, type, mn))
50
51
typedef struct _link_t
52
{
53
struct _link_t *prev;
54
struct _link_t *next;
55
} link_t;
56
57
static inline void link_init(link_t *l)
58
{
59
l->prev = NULL;
60
l->next = NULL;
61
}
62
63
static inline int link_used(link_t *l)
64
{
65
if (l->next == NULL)
66
return 1;
67
return 0;
68
}
69
70
static inline void list_init(link_t *lh)
71
{
72
lh->prev = lh;
73
lh->next = lh;
74
}
75
76
static inline void list_prepend(link_t *lh, link_t *l)
77
{
78
l->next = lh->next;
79
l->prev = lh;
80
lh->next->prev = l;
81
lh->next = l;
82
}
83
84
static inline void list_append(link_t *lh, link_t *l)
85
{
86
l->prev = lh->prev;
87
l->next = lh;
88
lh->prev->next = l;
89
lh->prev = l;
90
}
91
92
static inline void list_remove(link_t *l)
93
{
94
l->next->prev = l->prev;
95
l->prev->next = l->next;
96
link_init(l);
97
}
98
99
static inline int list_empty(link_t *lh)
100
{
101
if (lh->next == lh)
102
return 1;
103
return 0;
104
}
105
106
#endif
107
108