-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtlskip.c
452 lines (381 loc) · 10.2 KB
/
rtlskip.c
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//
// rtlskip.c
//
// Native NT implementation of a skip list - O(log n).
//
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non - commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <http://unlicense.org/>
//
// Acknowledgments:
//
// Skip Lists: A Probabilistic Alternative to Balanced Trees - William Pugh
// ftp://ftp.cs.umd.edu/pub/skipLists/skiplists.pdf (Original Paper)
// http://cglab.ca/~morin/teaching/5408/refs/p90b.pdf (A Skip List Cookbook)
//
#include "rtlskip.h"
#if defined (RTL_SKIPLIST_USE_RANDOMEX)
#define RTL_SKIPLIST_DEFAULT_SEED 31337UL
#endif
// Generates a random number on the interval [0,0xffffffff]
static ULONG sl_rand
(
_In_ PRTL_SKIPLIST_TABLE Table
)
{
ASSERT(Table != NULL);
#if defined (RTL_SKIPLIST_USE_RANDOMEX)
return RtlRandomEx(&Table->Seed);
#else
ULONGLONG t;
Table->KissState.x = 314527869 * Table->KissState.x + 1234567;
Table->KissState.y ^= Table->KissState.y << 5;
Table->KissState.y ^= Table->KissState.y >> 7;
Table->KissState.y ^= Table->KissState.y << 22;
t = 4294584393ULL * Table->KissState.z + Table->KissState.c;
Table->KissState.c = t >> 32;
Table->KissState.z = (ULONG)t;
return Table->KissState.x + Table->KissState.y + Table->KissState.z;
#endif
}
// Probabilistic random level generator.
static LONG sl_rlevel
(
_In_ PRTL_SKIPLIST_TABLE Table
)
{
ASSERT(Table != NULL);
LONG level = 1;
while (sl_rand(Table) < (2147483647 / 2) && level < RTL_SKIPLIST_MAX_LEVEL)
level++;
return level;
}
static PRTL_SKIPLIST_NODE _locate
(
_In_ PRTL_SKIPLIST_TABLE Table,
_In_ PVOID Buffer,
_Out_ BOOLEAN *Found,
_Inout_opt_ PRTL_SKIPLIST_NODE *Update
)
{
ASSERT(Table != NULL);
PRTL_SKIPLIST_NODE x = Table->Head;
*Found = FALSE;
for (LONG i = Table->Level; i >= 0; i--)
{
while (x->Forward[i] != NULL)
{
RTL_GENERIC_COMPARE_RESULTS result = Table->CompareRoutine
(
Table,
Buffer,
x->Forward[i]->Data
);
if (result == GenericGreaterThan)
x = x->Forward[i];
else
{
// One downside to the Pugh algorithm is that it requires
// two calls to the compare routine; one for traversal
// and one for the actual compare for equality. We can
// optimize out this double compare by doing the test
// for equality in the same pass as traversal.
if (result == GenericEqual)
*Found = TRUE;
break;
}
}
if (ARGUMENT_PRESENT(Update))
Update[i] = x;
}
return x;
}
_Must_inspect_result_
PVOID
NTAPI
RtlLookupElementGenericTableSkiplist
(
_In_ PRTL_SKIPLIST_TABLE Table,
_In_ PVOID Buffer
)
{
ASSERT(Table != NULL);
BOOLEAN found;
PRTL_SKIPLIST_NODE x = _locate(Table, Buffer, &found, NULL)->Forward[0];
return (found == TRUE) ? x->Data : NULL;
}
PVOID
NTAPI
RtlInsertElementGenericTableSkiplist
(
_In_ PRTL_SKIPLIST_TABLE Table,
_In_reads_bytes_(BufferSize) PVOID Buffer,
_In_ CLONG BufferSize,
_Out_opt_ PBOOLEAN NewElement
)
{
ASSERT(Table != NULL);
if (ARGUMENT_PRESENT(NewElement))
*NewElement = FALSE;
BOOLEAN found;
PRTL_SKIPLIST_NODE update[RTL_SKIPLIST_MAX_LEVEL + 1];
PRTL_SKIPLIST_NODE x = _locate(Table, Buffer, &found, update)->Forward[0];
if (found == TRUE)
return x->Data;
// This is what makes the whole thing work.
LONG level = sl_rlevel(Table);
x = Table->AllocateRoutine
(
Table,
sizeof(RTL_SKIPLIST_NODE) + (level * sizeof(PRTL_SKIPLIST_NODE))
);
if (x == NULL)
return NULL;
PVOID data = Table->AllocateRoutine(Table, BufferSize);
if (data == NULL)
{
Table->FreeRoutine(Table, x);
return NULL;
}
if (ARGUMENT_PRESENT(NewElement))
*NewElement = TRUE;
RtlCopyMemory(data, Buffer, BufferSize);
x->Data = data;
Table->Size++;
if (level > Table->Level)
{
for (LONG i = Table->Level + 1; i <= level; i++)
update[i] = Table->Head;
Table->Level = level;
}
#if defined (RTL_SKIPLIST_INCLUDE_BACKWARD_LINK)
x->Backward = update[0];
if (update[0]->Forward[0] != NULL)
update[0]->Forward[0]->Backward = x;
#endif
for (INT i = 0; i <= level; i++)
{
x->Forward[i] = update[i]->Forward[i];
update[i]->Forward[i] = x;
}
return data;
}
BOOLEAN
NTAPI
RtlDeleteElementGenericTableSkiplist
(
_In_ PRTL_SKIPLIST_TABLE Table,
_In_ PVOID Buffer
)
{
ASSERT(Table != NULL);
BOOLEAN found;
PRTL_SKIPLIST_NODE update[RTL_SKIPLIST_MAX_LEVEL + 1];
PRTL_SKIPLIST_NODE x = _locate(Table, Buffer, &found, update)->Forward[0];
if (found == FALSE)
return FALSE;
for (LONG i = 0; i < Table->Level; i++)
{
if (update[i]->Forward[i] != x)
break;
update[i]->Forward[i] = x->Forward[i];
}
#if defined (RTL_SKIPLIST_INCLUDE_BACKWARD_LINK)
if (x->Backward != NULL)
x->Backward->Forward[0] = x->Forward[0];
if (x->Forward[0] != NULL)
x->Forward[0]->Backward = x->Backward;
#endif
Table->FreeRoutine(Table, x->Data);
Table->FreeRoutine(Table, x);
--Table->Size;
while (Table->Level > 0)
{
if (Table->Head->Forward[Table->Level - 1] != NULL)
break;
--Table->Level;
}
return TRUE;
}
BOOLEAN
NTAPI
RtlIsGenericTableEmptySkiplist
(
_In_ PRTL_SKIPLIST_TABLE Table
)
{
ASSERT(Table != NULL);
return ((Table->Size) ? (FALSE) : (TRUE));
}
LONGLONG
NTAPI
RtlNumberGenericTableElementsSkiplist
(
_In_ PRTL_SKIPLIST_TABLE Table
)
{
ASSERT(Table != NULL);
return Table->Size;
}
PVOID
NTAPI
RtlEnumerateGenericTableSkiplist
(
_In_ PRTL_SKIPLIST_TABLE Table,
_In_ PVOID *RestartKey
)
{
ASSERT(Table != NULL);
PRTL_SKIPLIST_NODE x;
if (ARGUMENT_PRESENT(RestartKey))
{
PRTL_SKIPLIST_NODE restart = (PRTL_SKIPLIST_NODE)*RestartKey;
*RestartKey = x = restart->Forward[0];
}
else
x = Table->Head->Forward[0];
return (x ? x->Data : NULL);
}
PVOID
NTAPI
RtlEnumerateGenericTableWithoutSplayingSkiplist
(
_In_ PRTL_SKIPLIST_TABLE Table,
_In_ PVOID *RestartKey
)
{
ASSERT(Table != NULL);
PRTL_SKIPLIST_NODE x;
if (ARGUMENT_PRESENT(RestartKey))
{
PRTL_SKIPLIST_NODE restart = (PRTL_SKIPLIST_NODE)*RestartKey;
*RestartKey = x = restart->Forward[0];
}
else
x = Table->Head->Forward[0];
return (x ? x->Data : NULL);
}
_Must_inspect_result_
BOOLEAN
NTAPI
RtlInitializeGenericTableSkiplist
(
_Out_ PRTL_SKIPLIST_TABLE Table,
_In_ PRTL_SKIPLIST_COMPARE_ROUTINE CompareRoutine,
_In_ PRTL_SKIPLIST_ALLOCATE_ROUTINE AllocateRoutine,
_In_ PRTL_SKIPLIST_FREE_ROUTINE FreeRoutine,
_In_opt_ PVOID TableContext
)
{
// Seed the random number generator.
#if defined (RTL_SKIPLIST_USE_RANDOMEX)
Table->Seed = RTL_SKIPLIST_DEFAULT_SEED;
#else
Table->KissState.x = 123456789;
Table->KissState.y = 987654321;
Table->KissState.z = 43219876;
Table->KissState.c = 6543217;
#endif
PRTL_SKIPLIST_NODE head = AllocateRoutine
(
Table,
sizeof(RTL_SKIPLIST_NODE) + (RTL_SKIPLIST_MAX_LEVEL * sizeof(PRTL_SKIPLIST_NODE))
);
if (head == NULL)
{ // Oops!
KdPrint
((
"%s: %s(%d) Unable to allocate head node.\n",
__FILE__,
__FUNCTION__,
__LINE__
));
return FALSE;
}
head->Data = NULL;
for (LONG i = 0; i <= RTL_SKIPLIST_MAX_LEVEL; i++)
head->Forward[i] = NULL;
Table->Head = head;
Table->Level = 0;
Table->Size = 0;
Table->Context = TableContext;
Table->CompareRoutine = CompareRoutine;
Table->AllocateRoutine = AllocateRoutine;
Table->FreeRoutine = FreeRoutine;
return TRUE;
}
/*
PVOID
NTAPI
RtlInsertElementGenericTableFullSkiplist
(
_In_ PRTL_SKIPLIST_TABLE Table,
_In_reads_bytes_(BufferSize) PVOID Buffer,
_In_ CLONG BufferSize,
_Out_opt_ PBOOLEAN NewElement,
_In_ PVOID NodeOrParent,
_In_ TABLE_SEARCH_RESULT SearchResult
)
{
}
VOID
NTAPI
RtlDeleteElementGenericTableSkiplistEx
(
_In_ PRTL_SKIPLIST_TABLE Table,
_In_ PVOID NodeOrParent
)
{
}
PVOID
NTAPI
RtlLookupElementGenericTableFullSkiplist
(
_In_ PRTL_SKIPLIST_TABLE Table,
_In_ PVOID Buffer,
_Out_ PVOID *NodeOrParent,
_Out_ TABLE_SEARCH_RESULT *SearchResult
)
{
}
PVOID
NTAPI
RtlLookupFirstMatchingElementGenericTableSkiplist
(
_In_ PRTL_SKIPLIST_TABLE Table,
_In_ PVOID Buffer,
_Out_ PVOID *RestartKey
)
{
}
PVOID
NTAPI
RtlGetElementGenericTableSkiplist
(
_In_ PRTL_SKIPLIST_TABLE Table,
_In_ ULONG I
)
{
}
*/