-
Notifications
You must be signed in to change notification settings - Fork 0
/
llist.h
138 lines (119 loc) · 4.34 KB
/
llist.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Copyright (c) 2016 Wei-Lun Hsu. All Rights Reserved.
*/
/** @file llist.h
*
* @author Wei-Lun Hsu
* @version 0.1
* @date 2016/06/20
* @license
* @description
*/
#ifndef __llist_H_wx6xkQRJ_lqol_Hq8w_sveL_u0Oi6Z8lMaDL__
#define __llist_H_wx6xkQRJ_lqol_Hq8w_sveL_u0Oi6Z8lMaDL__
#ifdef __cplusplus
extern "C" {
#endif
#include "stddef.h"
//=============================================================================
// Constant Definition
//=============================================================================
/*
* These are non-NULL pointers that will result in page faults
* under normal circumstances, used to verify that nobody uses
* non-initialized list entries.
*/
#define LIST_POISON1 ((void *) 0x00100100)
#define LIST_POISON2 ((void *) 0x00200200)
//=============================================================================
// Macro Definition
//=============================================================================
/**
* Get offset of a member
*/
#define OFFSET_OF(TYPE, MEMBER) ((size_t) &((TYPE*)0)->MEMBER)
/**
* Casts a member of a structure out to the containing structure
* @param ptr the pointer to the member.
* @param TYPE the type of the container struct this is embedded in.
* @param MEMBER the name of the member within the struct.
*
*/
#define CONTAINER_OF(TYPE, MEMBER, ptr) \
(TYPE*)((size_t)(ptr) - OFFSET_OF(TYPE, MEMBER))
/**
* declare the first node
*/
#define DECLARE_LIST_HEAD(var) \
static struct llist var = {.next = &var, .prev = &var}
/**
* init node
*/
#define LLIST_NODE_INIT(pllst) \
do{ (pllst)->next = (pllst)->prev = (pllst); }while(0)
/**
* new node insert to head of list
*/
#define LLIST_NODE_INSERT_HEAD(pllst, pNewListNode) \
do{ (pllst)->next->prev = (pNewListNode); \
(pNewListNode)->next = (pllst)->next; \
(pllst)->next = pNewListNode; \
(pNewListNode)->prev = (pllst); \
}while(0)
/**
* new node insert to tail of list
*/
#define LLIST_NODE_INSERT_TAIL(pllst, pNewListNode) \
do{ (pllst)->prev->next = (pNewListNode); \
(pNewListNode)->prev = (pllst)->prev; \
(pllst)->prev = (pNewListNode); \
(pNewListNode)->next = (pllst); \
}while(0)
/**
* check list is empty or not
*/
#define LLIST_IS_EMPTY(pllst) ((pllst)->next == pllst)
/**
* deletes node from list.
* @pllst: the element to delete from the list.
* Note: list_empty on entry does not return true after this, the entry is
* in an undefined state.
*/
#define LLIST_NODE_DEL(pllst) \
do{ (pllst)->next->prev = (pllst)->prev; \
(pllst)->prev->next = (pllst)->next; \
(pllst)->prev = LIST_POISON2; \
(pllst)->next = LIST_POISON1; \
}while(0)
/**
* iterate over a list
* @pllstCur: the &struct list_head to use as a loop counter.
* @pllstTmp: another &struct list_head to use as temporary storage
* @pllstLst: the head for your list.
*/
#define LLIST_FOR_EACH(pllstCur, pllstTmp, pllstLst) \
for( pllstCur = (pllstLst)->next, pllstTmp = pllstCur->next; \
pllstCur != (pllstLst); \
pllstCur = pllstTmp, pllstTmp = pllstCur->next ) \
//=============================================================================
// Structure Definition
//=============================================================================
/**
* Simple doubly linked list implementation.
*/
typedef struct llist {
struct llist *next, *prev;
} llist_t;
//=============================================================================
// Global Data Definition
//=============================================================================
//=============================================================================
// Private Function Definition
//=============================================================================
//=============================================================================
// Public Function Definition
//=============================================================================
#ifdef __cplusplus
}
#endif
#endif