-
Notifications
You must be signed in to change notification settings - Fork 17
/
nblqueue.h
1037 lines (740 loc) · 24.2 KB
/
nblqueue.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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*++
Copyright (c) Microsoft. All rights reserved.
This code is licensed under the MIT License.
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.
Module Name:
nblqueue.h
Provenance:
Version 1.2.0 from https://github.com/microsoft/ndis-driver-library
Abstract:
Defines the NBL_QUEUE and utility functions to operate on it
NBLs are typically linked into a singly-linked list. Because it's a
singly-linked list, appending NBLs is a slow O(n) operation. This module
defines the NBL_QUEUE datastructure, which is designed to allow efficient
O(1) appends.
The NBL_COUNTED_QUEUE is an NBL_QUEUE, but also adds the additional feature
of tracking how many NBLs are in the queue.
Example usage:
NET_BUFFER_LIST *NblChain1 = // A=>B=>C=>NULL
NET_BUFFER_LIST *NblChain2 = // D=>NULL
NBL_QUEUE NblQueue;
NdisInitializeNblQueue(&NblQueue);
// NblQueue is now empty
NdisAppendNblChainToNblQueue(&NblQueue, NblChain1);
// NblQueue now has three NBLs in it: A, B, and C
NdisAppendNblChainToNblQueue(&NblQueue, NblChain2);
// NblQueue now has four NBLs in it: A, B, C, and D
NET_BUFFER_LIST *NblChain3 = NdisPopAllFromNblQueue(&NblQueue);
// NblQueue is now empty
// NblChain3 is A=>B=>C=>D=>NULL
Programming languages:
All the features of this header are available to C language code. However,
users of C++ may wish to consume the features via a C++ wrapper class. The
wrapper class provides automatic initialization of structures and a few
convenient member functions.
The example usage presented above could also be written in C++ as follows:
NET_BUFFER_LIST *NblChain1 = // A=>B=>C=>NULL
NET_BUFFER_LIST *NblChain2 = // D=>NULL
ndis::nbl_queue NblQueue;
NblQueue.append_slow(NblChain1);
// NblQueue now has three NBLs in it: A, B, and C
NblQueue.append_slow(NblChain2);
// NblQueue now has four NBLs in it: A, B, C, and D
NET_BUFFER_LIST *NblChain3 = NblQueue.clear();
// NblQueue is now empty
// NblChain3 is A=>B=>C=>D=>NULL
Table of Contents:
Routines for NBL_QUEUEs:
NdisInitializeNblQueue
NdisIsNblQueueEmpty
NdisGetNblChainFromNblQueue
NdisAppendNblChainToNblQueueFast
NdisAppendNblQueueToNblQueueFast
NdisAppendNblChainToNblQueue
NdisAppendSingleNblToNblQueue
NdisPopAllFromNblQueue
NdisPopFirstNblFromNblQueue
Routines for NBL_COUNTED_QUEUEs:
NdisInitializeNblCountedQueue
NdisIsNblCountedQueueEmpty
NdisGetNblChainFromNblCountedQueue
NdisAppendNblChainToNblCountedQueueFast
NdisAppendNblCountedQueueToNblCountedQueueFast
NdisAppendNblChainToNblCountedQueue
NdisAppendSingleNblToNblCountedQueue
NdisPopAllFromNblCountedQueue
NdisPopFirstNblFromNblCountedQueue
Environment:
Kernel mode
--*/
#pragma once
#pragma region System Family (kernel drivers) with Desktop Family for compat
#include <winapifamily.h>
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_SYSTEM | WINAPI_PARTITION_DESKTOP)
#include <ndis/ndl/nblchain.h>
typedef struct NBL_QUEUE_t
{
// Pointer to first NBL in chain, or NULL if the queue is empty
NET_BUFFER_LIST *First;
// Pointer to last NBL in chain, or to this->First if queue is empty
NET_BUFFER_LIST **Last;
#ifdef __cplusplus
NBL_QUEUE_t() = default;
~NBL_QUEUE_t() = default;
// Do not copy this data structure; it takes internal pointers to itself,
// so cannot be trivially copied.
NBL_QUEUE_t(NBL_QUEUE_t &) = delete;
NBL_QUEUE_t &operator=(NBL_QUEUE_t &) = delete;
// Move is okay; use with std::move.
inline NBL_QUEUE_t(NBL_QUEUE_t &&rhs);
inline NBL_QUEUE_t &operator=(NBL_QUEUE_t &&rhs);
#endif // __cplusplus
} NBL_QUEUE;
typedef struct NBL_COUNTED_QUEUE_t
{
// Contains 0 or more NBLs
NBL_QUEUE Queue;
// The number of NBLs in the Queue
SIZE_T NblCount;
#ifdef __cplusplus
NBL_COUNTED_QUEUE_t() = default;
~NBL_COUNTED_QUEUE_t() = default;
// Do not copy this data structure; it takes internal pointers to itself,
// so cannot be trivially copied.
NBL_COUNTED_QUEUE_t(NBL_COUNTED_QUEUE_t &) = delete;
NBL_COUNTED_QUEUE_t &operator=(NBL_COUNTED_QUEUE_t &) = delete;
// Move is okay; use with std::move.
inline NBL_COUNTED_QUEUE_t(NBL_COUNTED_QUEUE_t &&rhs);
inline NBL_COUNTED_QUEUE_t &operator=(NBL_COUNTED_QUEUE_t &&rhs);
#endif // __cplusplus
} NBL_COUNTED_QUEUE;
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
void
NDIS_ASSERT_VALID_NBL_QUEUE(
_In_ NBL_QUEUE const *NblQueue)
/*++
Routine Description:
Verifies the NBL_QUEUE appears to be correct
Arguments:
NblQueue
--*/
{
#if DBG
NDIS_ASSERT(NblQueue->Last != NULL);
if (NblQueue->First == NULL)
{
NDIS_ASSERT(NblQueue->Last == &NblQueue->First);
}
else
{
NET_BUFFER_LIST *Last = NdisLastNblInNblChain(NblQueue->First);
NDIS_ASSERT(NblQueue->Last == &Last->Next);
}
#else
UNREFERENCED_PARAMETER(NblQueue);
#endif
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
void
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(
_In_ NBL_COUNTED_QUEUE const *CountedQueue)
/*++
Routine Description:
Verifies the NBL_COUNTED_QUEUE appears to be correct
Arguments:
CountedQueue
--*/
{
#if DBG
const SIZE_T Count = NdisNumNblsInNblChain(CountedQueue->Queue.First);
NDIS_ASSERT_VALID_NBL_QUEUE(&CountedQueue->Queue);
NDIS_ASSERT(Count == CountedQueue->NblCount);
#else
UNREFERENCED_PARAMETER(CountedQueue);
#endif
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
void
NdisInitializeNblQueue(
_Out_ NBL_QUEUE *NblQueue)
/*++
Routine Description:
Initializes an NBL_QUEUE datastructure
Arguments:
NblQueue
--*/
{
NblQueue->First = NULL;
{ C_ASSERT(FIELD_OFFSET(NET_BUFFER_LIST, Next) == 0); }
NblQueue->Last = &NblQueue->First;
NDIS_ASSERT_VALID_NBL_QUEUE(NblQueue);
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
void
NdisInitializeNblCountedQueue(
_Out_ NBL_COUNTED_QUEUE *CountedQueue)
/*++
Routine Description:
Initializes an NBL_COUNTED_QUEUE datastructure
Arguments:
CountedQueue
--*/
{
NdisInitializeNblQueue(&CountedQueue->Queue);
CountedQueue->NblCount = 0;
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(CountedQueue);
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
BOOLEAN
NdisIsNblQueueEmpty(
_In_ NBL_QUEUE const *NblQueue)
/*++
Routine Description:
Determines whether any NBLs are in the queue
Arguments:
NblQueue
Return Value:
FALSE if there is at least one NBL in the queue, else
TRUE if there are no NBLs in the queue
--*/
{
NDIS_ASSERT_VALID_NBL_QUEUE(NblQueue);
return NblQueue->First == NULL;
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
BOOLEAN
NdisIsNblCountedQueueEmpty(
_In_ NBL_COUNTED_QUEUE const *CountedQueue)
/*++
Routine Description:
Determines whether any NBLs are in the queue
Arguments:
CountedQueue
Return Value:
FALSE if there is at least one NBL in the queue, else
TRUE if there are no NBLs in the queue
--*/
{
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(CountedQueue);
return NdisIsNblQueueEmpty(&CountedQueue->Queue);
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
NET_BUFFER_LIST *
NdisGetNblChainFromNblQueue(
_In_ NBL_QUEUE const *Queue)
/*++
Routine Description:
Retreives the NBL chain from the queue
Arguments:
Queue
Return Value:
The NBL chain stored in the queue, or NULL if the queue is empty
--*/
{
NDIS_ASSERT_VALID_NBL_QUEUE(Queue);
return Queue->First;
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
NET_BUFFER_LIST *
NdisGetNblChainFromNblCountedQueue(
_In_ NBL_COUNTED_QUEUE const *CountedQueue)
/*++
Routine Description:
Retreives the NBL chain from the queue
Arguments:
CountedQueue
Return Value:
The NBL chain stored in the queue, or NULL if the queue is empty
--*/
{
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(CountedQueue);
return NdisGetNblChainFromNblQueue(&CountedQueue->Queue);
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
void
NdisAppendNblChainToNblQueueFast(
_Inout_ NBL_QUEUE *NblQueue,
_In_ NET_BUFFER_LIST *NblChainFirst,
_In_ NET_BUFFER_LIST *NblChainLast)
/*++
Routine Description:
Appends an NBL chain to an NBL_QUEUE
Executes in O(1) time, so it's "Fast". But you must know the last
NBL in the chain. If you don't know the last NBL in the chain,
use NdisAppendNblChainToNblQueue instead, which is O(n).
Arguments:
NblQueue
NblChainFirst - First NBL in the chain
NblChainLast - Last NBL in the chain (NblChainLast->Next must be NULL)
--*/
{
NDIS_ASSERT_VALID_NBL_QUEUE(NblQueue);
#if DBG
{
const NET_BUFFER_LIST *Last = NdisLastNblInNblChain(NblChainFirst);
NDIS_ASSERT(Last == NblChainLast);
NDIS_ASSERT(Last != *NblQueue->Last);
}
#endif
*NblQueue->Last = NblChainFirst;
NblQueue->Last = &NblChainLast->Next;
NDIS_ASSERT_VALID_NBL_QUEUE(NblQueue);
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
void
NdisAppendNblChainToNblCountedQueueFast(
_Inout_ NBL_COUNTED_QUEUE *CountedQueue,
_In_ NET_BUFFER_LIST *NblChainFirst,
_In_ NET_BUFFER_LIST *NblChainLast,
_In_ SIZE_T NumNblsToAppend)
/*++
Routine Description:
Appends an NBL chain to an NBL_COUNTED_QUEUE
Executes in O(1) time, so it's "Fast". But you must know the last
NBL in the chain. If you don't know the last NBL in the chain,
use NdisAppendNblChainToNblCountedQueue instead, which is O(n).
Arguments:
CountedQueue
NblChainFirst - First NBL in the chain
NblChainLast - Last NBL in the chain (NblChainLast->Next must be NULL)
NumNblsToAppend - The number of NBLs in [NblChainFirst, NblChainLast]
--*/
{
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(CountedQueue);
#if DBG
{
const SIZE_T VerifiedCount = NdisNumNblsInNblChain(NblChainFirst);
NDIS_ASSERT(VerifiedCount == NumNblsToAppend);
// Overflow check
NDIS_ASSERT(CountedQueue->NblCount + NumNblsToAppend >= NumNblsToAppend);
}
#endif
NdisAppendNblChainToNblQueueFast(&CountedQueue->Queue, NblChainFirst, NblChainLast);
CountedQueue->NblCount += NumNblsToAppend;
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(CountedQueue);
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
void
NdisAppendNblQueueToNblQueueFast(
_Inout_ NBL_QUEUE *Destination,
_Inout_ NBL_QUEUE *Source)
/*++
Routine Description:
Removes all the NBLs from Source and appends them to Destination
N.B. There is no "Slow" version of this routine, but it's named
"Fast" so that it's clear that it's an O(1) operation, unlike
NdisAppendNblChainToNblQueue.
Arguments:
Destination - Receives all the NBLs from Source
Source - Donates NBLs to Destination; is empty after call returns
--*/
{
NDIS_ASSERT_VALID_NBL_QUEUE(Destination);
NDIS_ASSERT_VALID_NBL_QUEUE(Source);
// No cycles allowed
NDIS_ASSERT(Destination->Last != Source->Last);
if (NdisIsNblQueueEmpty(Source))
{
return;
}
*Destination->Last = Source->First;
Destination->Last = Source->Last;
NdisInitializeNblQueue(Source);
NDIS_ASSERT_VALID_NBL_QUEUE(Destination);
NDIS_ASSERT_VALID_NBL_QUEUE(Source);
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
void
NdisAppendNblCountedQueueToNblCountedQueueFast(
_Inout_ NBL_COUNTED_QUEUE *Destination,
_Inout_ NBL_COUNTED_QUEUE *Source)
/*++
Routine Description:
Removes all the NBLs from Source and appends them to Destination
N.B. There is no "Slow" version of this routine, but it's named
"Fast" so that it's clear that it's an O(1) operation, unlike
NdisAppendNblChainToNblCountedQueue.
Arguments:
Destination - Receives all the NBLs from Source
Source - Donates NBLs to Destination; is empty after call returns
--*/
{
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(Destination);
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(Source);
// Overflow check
NDIS_ASSERT(Destination->NblCount + Source->NblCount >= Source->NblCount);
NdisAppendNblQueueToNblQueueFast(&Destination->Queue, &Source->Queue);
Destination->NblCount += Source->NblCount;
Source->NblCount = 0;
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(Destination);
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(Source);
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
void
NdisAppendNblChainToNblQueue(
_In_ NBL_QUEUE *NblQueue,
_In_ NET_BUFFER_LIST *NblChain)
/*++
Routine Description:
Appends an NBL chain to an NBL_QUEUE
This routine has the same effect as NdisAppendNblChainToNblQueueFast,
however it is slower. Use this routine if you don't have handy a pointer
to the last NBL in the chain.
Arguments:
NblQueue
NblChain
--*/
{
NdisAppendNblChainToNblQueueFast(NblQueue, NblChain, NdisLastNblInNblChain(NblChain));
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
void
NdisAppendNblChainToNblCountedQueue(
_In_ NBL_COUNTED_QUEUE *CountedQueue,
_In_ NET_BUFFER_LIST *NblChain)
/*++
Routine Description:
Appends an NBL chain to an NBL_COUNTED_QUEUE
This routine has the same effect as NdisAppendNblChainToNblCountedQueueFast,
however it is slower. Use this routine if you don't have handy a pointer
to the last NBL in the chain.
Arguments:
CountedQueue
NblChain
--*/
{
SIZE_T Count;
NET_BUFFER_LIST *const LastNbl = NdisLastNblInNblChainWithCount(NblChain, &Count);
// Overflow check
NDIS_ASSERT(CountedQueue->NblCount + Count >= Count);
NdisAppendNblChainToNblQueueFast(&CountedQueue->Queue, NblChain, LastNbl);
CountedQueue->NblCount += Count;
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(CountedQueue);
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
void
NdisAppendSingleNblToNblQueue(
_In_ NBL_QUEUE *NblQueue,
_In_ NET_BUFFER_LIST *Nbl)
/*++
Routine Description:
Appends one NBL to an NBL_QUEUE
Arguments:
NblQueue
Nbl
--*/
{
Nbl->Next = NULL;
NdisAppendNblChainToNblQueueFast(NblQueue, Nbl, Nbl);
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
void
NdisAppendSingleNblToNblCountedQueue(
_In_ NBL_COUNTED_QUEUE *CountedQueue,
_In_ NET_BUFFER_LIST *Nbl)
/*++
Routine Description:
Appends one NBL to an NBL_COUNTED_QUEUE
Arguments:
CountedQueue
Nbl
--*/
{
Nbl->Next = NULL;
NdisAppendNblChainToNblCountedQueueFast(CountedQueue, Nbl, Nbl, 1);
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
NET_BUFFER_LIST *
NdisPopAllFromNblQueue(
_In_ NBL_QUEUE *NblQueue)
/*++
Routine Description:
Removes all NBLs from the queue, and returns them in a chain
Arguments:
NblQueue
Return Value:
The previous contents of the NBL_QUEUE
--*/
{
NET_BUFFER_LIST *const Nbls = NblQueue->First;
NDIS_ASSERT_VALID_NBL_QUEUE(NblQueue);
NdisInitializeNblQueue(NblQueue);
return Nbls;
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
NET_BUFFER_LIST *
NdisPopAllFromNblCountedQueue(
_In_ NBL_COUNTED_QUEUE *CountedQueue)
/*++
Routine Description:
Removes all NBLs from the queue, and returns them in a chain
Arguments:
CountedQueue
Return Value:
The previous contents of the NBL_COUNTED_QUEUE
--*/
{
NET_BUFFER_LIST *const Nbls = CountedQueue->Queue.First;
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(CountedQueue);
NdisInitializeNblCountedQueue(CountedQueue);
return Nbls;
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
NET_BUFFER_LIST *
NdisPopFirstNblFromNblQueue(
_In_ NBL_QUEUE *NblQueue)
/*++
Routine Description:
Removes the first NBL from the queue, if any
Arguments:
NblQueue
Return Value:
NULL if the NblQueue was empty, else
the NBL that was recently the first NBL in the queue
--*/
{
NDIS_ASSERT_VALID_NBL_QUEUE(NblQueue);
if (NblQueue->First == NULL)
{
// The queue has 0 items
return NULL;
}
else if (NblQueue->Last == &NblQueue->First->Next)
{
// The queue has 1 item
return NdisPopAllFromNblQueue(NblQueue);
}
else
{
// The queue has 2+ items
NET_BUFFER_LIST *const First = NblQueue->First;
NblQueue->First = First->Next;
First->Next = NULL;
NDIS_ASSERT_VALID_NBL_QUEUE(NblQueue);
return First;
}
}
_IRQL_requires_max_(DISPATCH_LEVEL)
inline
NET_BUFFER_LIST *
NdisPopFirstNblFromNblCountedQueue(
_In_ NBL_COUNTED_QUEUE *CountedQueue)
/*++
Routine Description:
Removes the first NBL from the queue, if any
Arguments:
CountedQueue
Return Value:
NULL if the CountedQueue was empty, else
the NBL that was recently the first NBL in the queue
--*/
{
NET_BUFFER_LIST *Nbl;
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(CountedQueue);
Nbl = NdisPopFirstNblFromNblQueue(&CountedQueue->Queue);
if (Nbl != NULL)
{
CountedQueue->NblCount -= 1;
}
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(CountedQueue);
return Nbl;
}
#ifdef __cplusplus
inline NBL_QUEUE_t::NBL_QUEUE_t(NBL_QUEUE_t &&rhs)
{
NDIS_ASSERT_VALID_NBL_QUEUE(&rhs);
if (rhs.First)
{
this->First = rhs.First;
this->Last = rhs.Last;
NdisInitializeNblQueue(&rhs);
}
else
{
NdisInitializeNblQueue(this);
}
NDIS_ASSERT_VALID_NBL_QUEUE(this);
NDIS_ASSERT_VALID_NBL_QUEUE(&rhs);
}
inline NBL_QUEUE_t &
NBL_QUEUE_t::operator=(NBL_QUEUE_t &&rhs)
{
NDIS_ASSERT_VALID_NBL_QUEUE(&rhs);
if (this->First != rhs.First)
{
if (rhs.First)
{
this->First = rhs.First;
this->Last = rhs.Last;
NdisInitializeNblQueue(&rhs);
}
else
{
NdisInitializeNblQueue(this);
}
}
NDIS_ASSERT_VALID_NBL_QUEUE(this);
NDIS_ASSERT_VALID_NBL_QUEUE(&rhs);
return *this;
}
inline NBL_COUNTED_QUEUE_t::NBL_COUNTED_QUEUE_t(NBL_COUNTED_QUEUE_t &&rhs)
{
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(&rhs);
if (rhs.Queue.First)
{
this->Queue.First = rhs.Queue.First;
this->Queue.Last = rhs.Queue.Last;
this->NblCount = rhs.NblCount;
NdisInitializeNblCountedQueue(&rhs);
}
else
{
NdisInitializeNblCountedQueue(this);
}
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(this);
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(&rhs);
}
inline NBL_COUNTED_QUEUE_t &
NBL_COUNTED_QUEUE_t::operator=(NBL_COUNTED_QUEUE_t &&rhs)
{
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(&rhs);
if (this->Queue.First != rhs.Queue.First)
{
if (rhs.Queue.First)
{
this->Queue.First = rhs.Queue.First;
this->Queue.Last = rhs.Queue.Last;
this->NblCount = rhs.NblCount;
NdisInitializeNblCountedQueue(&rhs);
}
else
{
NdisInitializeNblCountedQueue(this);
}
}
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(this);
NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(&rhs);
return *this;
}
namespace ndis {
struct nbl_queue : public NBL_QUEUE
{
nbl_queue() { NdisInitializeNblQueue(this); }
~nbl_queue() = default;
explicit nbl_queue(NET_BUFFER_LIST *nblChain)
{
this->First = nblChain;
this->Last = &NdisLastNblInNblChain(nblChain)->Next;
ASSERT_VALID();
}
nbl_queue(nbl_queue &) = delete;
nbl_queue &operator=(nbl_queue &) = delete;
nbl_queue(nbl_queue &&rhs) = default;
nbl_queue(NBL_QUEUE &&rhs) : NBL_QUEUE(static_cast<NBL_QUEUE &&>(rhs)) {}
nbl_queue &operator=(nbl_queue &&rhs) = default;
nbl_queue &operator=(NBL_QUEUE &&rhs)
{
*static_cast<NBL_QUEUE *>(this) = static_cast<NBL_QUEUE &&>(rhs);
return *this;
}
bool empty() const { return !!NdisIsNblQueueEmpty(this); }
NET_BUFFER_LIST *get_nbls() const { return NdisGetNblChainFromNblQueue(this); }
void append(_In_ NET_BUFFER_LIST *first, _In_ NET_BUFFER_LIST *last)
{
NdisAppendNblChainToNblQueueFast(this, first, last);
}
void append(_Inout_ NBL_QUEUE *queue) { NdisAppendNblQueueToNblQueueFast(this, queue); }
void append_slow(_In_ NET_BUFFER_LIST *nblChain) { NdisAppendNblChainToNblQueue(this, nblChain); }
void append_one_nbl(_In_ NET_BUFFER_LIST *nbl) { NdisAppendSingleNblToNblQueue(this, nbl); }
NET_BUFFER_LIST *clear() { return NdisPopAllFromNblQueue(this); }
NET_BUFFER_LIST *pop() { return NdisPopFirstNblFromNblQueue(this); }
void ASSERT_VALID() const { NDIS_ASSERT_VALID_NBL_QUEUE(this); }
};
struct nbl_counted_queue : public NBL_COUNTED_QUEUE
{
nbl_counted_queue() { NdisInitializeNblCountedQueue(this); }
~nbl_counted_queue() = default;
explicit nbl_counted_queue(NET_BUFFER_LIST *nblChain)
{
SIZE_T count;
auto last = NdisLastNblInNblChainWithCount(nblChain, &count);
this->Queue.First = nblChain;
this->Queue.Last = &last->Next;
this->NblCount = count;
ASSERT_VALID();
}
nbl_counted_queue(nbl_counted_queue &&rhs) = default;
nbl_counted_queue(NBL_COUNTED_QUEUE &&rhs) : NBL_COUNTED_QUEUE(static_cast<NBL_COUNTED_QUEUE &&>(rhs)) {}
nbl_counted_queue &operator=(nbl_counted_queue &&rhs) = default;
nbl_counted_queue &operator=(NBL_COUNTED_QUEUE &&rhs)
{
*static_cast<NBL_COUNTED_QUEUE *>(this) = static_cast<NBL_COUNTED_QUEUE &&>(rhs);
return *this;
}
nbl_counted_queue(nbl_counted_queue &) = delete;
nbl_counted_queue &operator=(nbl_counted_queue &) = delete;
bool empty() const { return !!NdisIsNblCountedQueueEmpty(this); }
NET_BUFFER_LIST *get_nbls() const { return NdisGetNblChainFromNblCountedQueue(this); }
size_t count() const { return this->NblCount; }
void append(_In_ NET_BUFFER_LIST *first, _In_ NET_BUFFER_LIST *last, _In_ size_t numNbls)
{
NdisAppendNblChainToNblCountedQueueFast(this, first, last, numNbls);
}
void append(_Inout_ NBL_COUNTED_QUEUE *queue) { NdisAppendNblCountedQueueToNblCountedQueueFast(this, queue); }
void append_slow(_In_ NET_BUFFER_LIST *nblChain) { NdisAppendNblChainToNblCountedQueue(this, nblChain); }
void append_one_nbl(_In_ NET_BUFFER_LIST *nbl) { NdisAppendSingleNblToNblCountedQueue(this, nbl); }
NET_BUFFER_LIST *clear() { return NdisPopAllFromNblCountedQueue(this); }
NET_BUFFER_LIST *pop() { return NdisPopFirstNblFromNblCountedQueue(this); }
void ASSERT_VALID() const { NDIS_ASSERT_VALID_NBL_COUNTED_QUEUE(this); }
};