-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlist.h
67 lines (55 loc) · 1.78 KB
/
list.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* set ts=4 sw=4 enc=utf-8: -*- Mode: c; tab-width: 4; c-basic-offset:4; coding: utf-8 -*- */
/*
* Copyright 2017 Chul-Woong Yang (cwyang@gmail.com)
* Licensed under the Apache License, Version 2.0;
* See LICENSE for details.
*
* list.h
* 14 January 2017, cwyang@gmail.com
*
* Linux compatible doubly linked list.
*
*/
#ifndef SIMPLE_LIST_H
#define SIMPLE_LIST_H
typedef struct list_head {
struct list_head *next, *prev;
} list_t;
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) list_t name = LIST_HEAD_INIT(name)
static inline list_t* init_list_head(list_t *head) {
head->next = head->prev = head;
return head;
}
static inline void list_add(list_t *new, list_t *head) {
new->prev = head;
new->next = head->next;
head->next->prev = new;
head->next = new;
}
static inline void list_add_tail(list_t *new, list_t *head) {
new->prev = head->prev;
new->next = head;
head->prev->next = new;
head->prev = new;
}
static inline void list_del(list_t *entry) {
entry->prev->next = entry->next;
entry->next->prev = entry->prev;
}
static inline int list_empty(list_t *head) {
return head->next == head;
}
#ifdef container_of
#define list_entry container_of
#else
#define list_entry(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
#endif
#define list_for_each(POS, HEAD) \
for ((POS) = (HEAD)->next; (POS) != (HEAD); (POS) = (POS)->next)
#define list_for_each_safe(POS, TEMP, HEAD) \
for ((POS) = (HEAD)->next, (TEMP) = (POS)->next; \
(POS) != (HEAD); \
(POS) = (TEMP), (TEMP) = (POS)->next)
#endif // SIMPLE_LIST_H