list.h
1 /*
2  Dokan : user-mode file system library for Windows
3 
4  Copyright (C) 2020 - 2023 Google, Inc.
5  Copyright (C) 2015 - 2019 Adrien J. <liryna.stark@gmail.com> and Maxime C. <maxime@islog.com>
6  Copyright (C) 2007 - 2011 Hiroki Asakawa <info@dokan-dev.net>
7 
8  http://dokan-dev.github.io
9 
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 3 of the License, or (at your option) any
13 later version.
14 
15 This program is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU Lesser General Public License along
20 with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #ifndef LIST_H_
24 #define LIST_H_
25 
26 #include <windows.h>
27 
28 FORCEINLINE
29 VOID InitializeListHead(PLIST_ENTRY ListHead) {
30  ListHead->Flink = ListHead->Blink = ListHead;
31 }
32 
33 FORCEINLINE
34 BOOLEAN
35 IsListEmpty(const LIST_ENTRY *ListHead) {
36  return (BOOLEAN)(ListHead == NULL || ListHead->Flink == ListHead);
37 }
38 
39 FORCEINLINE
40 BOOLEAN
41 RemoveEntryList(PLIST_ENTRY Entry) {
42  PLIST_ENTRY Blink;
43  PLIST_ENTRY Flink;
44 
45  if (Entry != NULL) {
46  Flink = Entry->Flink;
47  Blink = Entry->Blink;
48  Blink->Flink = Flink;
49  Flink->Blink = Blink;
50  return (BOOLEAN)(Flink == Blink);
51  }
52  /* Assumes the list is empty */
53  return TRUE;
54 }
55 
56 FORCEINLINE
57 PLIST_ENTRY
58 RemoveHeadList(PLIST_ENTRY ListHead) {
59  PLIST_ENTRY Flink;
60  PLIST_ENTRY Entry;
61 
62  Entry = ListHead->Flink;
63  Flink = Entry->Flink;
64  ListHead->Flink = Flink;
65  Flink->Blink = ListHead;
66  return Entry;
67 }
68 
69 FORCEINLINE
70 PLIST_ENTRY
71 RemoveTailList(PLIST_ENTRY ListHead) {
72  PLIST_ENTRY Blink;
73  PLIST_ENTRY Entry;
74 
75  Entry = ListHead->Blink;
76  Blink = Entry->Blink;
77  ListHead->Blink = Blink;
78  Blink->Flink = ListHead;
79  return Entry;
80 }
81 
82 FORCEINLINE
83 VOID InsertTailList(PLIST_ENTRY ListHead, PLIST_ENTRY Entry) {
84  PLIST_ENTRY Blink;
85 
86  Blink = ListHead->Blink;
87  Entry->Flink = ListHead;
88  Entry->Blink = Blink;
89  Blink->Flink = Entry;
90  ListHead->Blink = Entry;
91 }
92 
93 FORCEINLINE
94 VOID InsertHeadList(PLIST_ENTRY ListHead, PLIST_ENTRY Entry) {
95  PLIST_ENTRY Flink;
96 
97  Flink = ListHead->Flink;
98  Entry->Flink = Flink;
99  Entry->Blink = ListHead;
100  Flink->Blink = Entry;
101  ListHead->Flink = Entry;
102 }
103 
104 FORCEINLINE
105 VOID AppendTailList(PLIST_ENTRY ListHead, PLIST_ENTRY ListToAppend) {
106  PLIST_ENTRY ListEnd = ListHead->Blink;
107 
108  ListHead->Blink->Flink = ListToAppend;
109  ListHead->Blink = ListToAppend->Blink;
110  ListToAppend->Blink->Flink = ListHead;
111  ListToAppend->Blink = ListEnd;
112 }
113 
114 FORCEINLINE
115 PSINGLE_LIST_ENTRY
116 PopEntryList(PSINGLE_LIST_ENTRY ListHead) {
117  PSINGLE_LIST_ENTRY FirstEntry;
118  FirstEntry = ListHead->Next;
119  if (FirstEntry != NULL) {
120  ListHead->Next = FirstEntry->Next;
121  }
122 
123  return FirstEntry;
124 }
125 
126 FORCEINLINE
127 VOID PushEntryList(PSINGLE_LIST_ENTRY ListHead, PSINGLE_LIST_ENTRY Entry) {
128  Entry->Next = ListHead->Next;
129  ListHead->Next = Entry;
130 }
131 
132 #endif // LIST_H_