forked from coreyrobins/Bittorrent-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.h
More file actions
148 lines (143 loc) · 6.26 KB
/
Copy pathlist.h
File metadata and controls
148 lines (143 loc) · 6.26 KB
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
139
140
141
142
143
144
145
146
147
148
/*
* Generic list data type
* Copyright (c) 2001,2004 David H. Hovemeyer <daveho@cs.umd.edu>
* $Revision: 1.1 $
*
* This is free software. You are permitted to use,
* redistribute, and modify it as specified in the file "COPYING".
*/
#ifndef GEEKOS_LIST_H
#define GEEKOS_LIST_H
#define KASSERT assert
#include <stdbool.h>
/*
* Define a list type.
*/
#define DEFINE_LIST(listTypeName, nodeTypeName) \
struct listTypeName { \
/*@null@*/ /*@dependent@*/ struct nodeTypeName *head, *tail; \
};
/*
* Define members of a struct to be used as link fields for
* membership in given list type.
*/
#define DEFINE_LINK(listTypeName, nodeTypeName) \
/*@null@*/ /*@dependent@*/ struct nodeTypeName * prev##listTypeName, * next##listTypeName
/*
* Define inline list manipulation and access functions.
*/
#define IMPLEMENT_LIST(LType, NType) \
static __inline__ void Clear_##LType(struct LType *listPtr) { \
listPtr->head = listPtr->tail = 0; \
} \
static __inline__ bool Is_Member_Of_##LType(struct LType *listPtr, struct NType *nodePtr) { \
struct NType *cur = listPtr->head; \
while (cur != 0) { \
if (cur == nodePtr) \
return true; \
cur = cur->next##LType; \
} \
return false; \
} \
static __inline__ /*@null@*/ /*@dependent@*/ struct NType * Get_Front_Of_##LType(struct LType *listPtr) { \
return listPtr->head; \
} \
static __inline__ /*@null@*/ /*@dependent@*/ struct NType * Get_Back_Of_##LType(struct LType *listPtr) { \
return listPtr->tail; \
} \
static __inline__ /*@null@*/ /*@dependent@*/ struct NType * Get_Next_In_##LType(struct NType *nodePtr) { \
return nodePtr->next##LType; \
} \
static __inline__ void Set_Next_In_##LType(struct NType *nodePtr, /*@dependent@*/ struct NType *value) { \
nodePtr->next##LType = value; \
} \
static __inline__ /*@null@*/ /*@dependent@*/ struct NType * Get_Prev_In_##LType(struct NType *nodePtr) { \
return nodePtr->prev##LType; \
} \
static __inline__ void Set_Prev_In_##LType(struct NType *nodePtr, /*@dependent@*/ struct NType *value) { \
nodePtr->prev##LType = value; \
} \
static __inline__ void Add_To_Front_Of_##LType(struct LType *listPtr, /*@dependent@*/ struct NType *nodePtr) { \
KASSERT(!Is_Member_Of_##LType(listPtr, nodePtr)); \
nodePtr->prev##LType = 0; \
if (listPtr->head == 0) { \
listPtr->head = listPtr->tail = nodePtr; \
nodePtr->next##LType = 0; \
} else { \
listPtr->head->prev##LType = nodePtr; \
nodePtr->next##LType = listPtr->head; \
listPtr->head = nodePtr; \
} \
} \
static __inline__ void Unchecked_Add_To_Back_Of_##LType(struct LType *listPtr, /*@dependent@*/ struct NType *nodePtr) { \
nodePtr->next##LType = 0; \
if (listPtr->tail == 0) { \
listPtr->head = listPtr->tail = nodePtr; \
nodePtr->prev##LType = 0; \
} \
else { \
listPtr->tail->next##LType = nodePtr; \
nodePtr->prev##LType = listPtr->tail; \
listPtr->tail = nodePtr; \
} \
} \
static __inline__ void Add_To_Back_Of_##LType(struct LType *listPtr, /*@dependent@*/ struct NType *nodePtr) { \
KASSERT(!Is_Member_Of_##LType(listPtr, nodePtr)); \
Unchecked_Add_To_Back_Of_##LType(listPtr, nodePtr); \
} \
static __inline__ void Append_##LType(struct LType *listToModify, struct LType *listToAppend) { \
if (listToAppend->head != 0) { \
if (listToModify->head == 0) { \
listToModify->head = listToAppend->head; \
listToModify->tail = listToAppend->tail; \
} else { \
KASSERT(listToAppend->head != 0); \
KASSERT(listToModify->tail != 0); \
listToAppend->head->prev##LType = listToModify->tail; \
listToModify->tail->next##LType = listToAppend->head; \
listToModify->tail = listToAppend->tail; \
} \
} \
listToAppend->head = listToAppend->tail = 0; \
} \
static __inline__ /*@null@*/ /*@dependent@*/ struct NType * Remove_From_Front_Of_##LType(struct LType *listPtr) { \
struct NType *nodePtr; \
nodePtr = listPtr->head; \
KASSERT(nodePtr != 0); \
listPtr->head = listPtr->head->next##LType; \
if (listPtr->head == 0) \
listPtr->tail = 0; \
else \
listPtr->head->prev##LType = 0; \
return nodePtr; \
} \
static __inline__ void Remove_From_##LType(struct LType *listPtr, struct NType *nodePtr) { \
KASSERT(Is_Member_Of_##LType(listPtr, nodePtr)); \
if (nodePtr->prev##LType != 0) \
nodePtr->prev##LType->next##LType = nodePtr->next##LType; \
else \
listPtr->head = nodePtr->next##LType; \
if (nodePtr->next##LType != 0) \
nodePtr->next##LType->prev##LType = nodePtr->prev##LType; \
else \
listPtr->tail = nodePtr->prev##LType; \
} \
static __inline__ bool Is_##LType##_Empty(struct LType *listPtr) { \
return listPtr->head == 0; \
} \
static __inline__ void Insert_Into_##LType(struct LType *listPtr, /*@dependent@*/ struct NType *nodeToInsertAfter, /*@dependent@*/ struct NType *nodePtr) { \
KASSERT(Is_Member_Of_##LType(listPtr, nodeToInsertAfter)); \
if(nodeToInsertAfter->next##LType == 0) { \
nodeToInsertAfter->next##LType = nodePtr; \
nodePtr->prev##LType = nodeToInsertAfter; \
nodePtr->next##LType = 0; \
listPtr->tail = nodePtr; \
} \
else { \
nodePtr->next##LType = nodeToInsertAfter->next##LType; \
nodeToInsertAfter->next##LType = nodePtr; \
nodePtr->prev##LType = nodeToInsertAfter; \
nodePtr->next##LType->prev##LType = nodePtr; \
} \
}
#endif /* GEEKOS_LIST_H */