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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
//! Windows kernel-mode pool allocator for `#![no_std]` Rust drivers.
//!
//! `ntalloc` wraps the WDK pool APIs (`ExAllocatePool2`, `ExFreePool`,
//! `ExFreePoolWithTag`) behind the standard [`GlobalAlloc`] trait and, with
//! the `allocator-api` feature, the [`Allocator`] trait, so ordinary
//! `alloc`-crate types (`Vec`, `Box`, ...) can be backed by kernel pool
//! memory from a `#![no_std]` driver. It builds on stable Rust 1.85 or
//! newer; `allocator-api` needs Rust 1.100, and `allocator-api-markers` a
//! nightly compiler.
//!
//! [`Allocator`]: core::alloc::Allocator
//!
//! # Overview
//!
//! - [`NtAlloc`] is a zero-sized allocator type parameterized over the pool
//! flags, the memory tag, whether deallocation uses the tagged or untagged
//! free routine, and whether it realigns requests the pool cannot align by
//! itself, which it does by default. The pool flags are exported as
//! `POOL_FLAG_*` constants, such as [`POOL_FLAG_NON_PAGED`] and
//! [`POOL_FLAG_UNINITIALIZED`].
//! - [`PagedAlloc`] and [`NonPagedAlloc`] are convenience aliases for the
//! paged and non-paged pool, respectively.
//! - Allocations are tagged with a 4-byte ASCII tag (see
//! [`DEFAULT_MEMORY_TAG`]) so they can be attributed back to this allocator
//! with tools such as `!poolused`.
//!
//! # Design
//!
//! `ExAllocatePool2` only guarantees an allocation aligned to
//! [`MIN_ALIGNMENT`] (the WDK's `MEMORY_ALLOCATION_ALIGNMENT`: two pointers,
//! i.e. 16 bytes on 64-bit targets), or to the page size for allocations of
//! at least one page (see [`NtAlloc::kernel_alignment`]). Rust [`Layout`]s can
//! request stricter alignment than that, so when a request exceeds what the
//! pool allocator natively guarantees, [`NtAlloc::allocate_aligned`]
//! over-allocates by `layout.align()` extra bytes, rounds the returned
//! pointer up to the requested alignment, and stashes the original
//! (unaligned) pointer in the pointer-sized slot immediately before the
//! aligned pointer. There is always room for that slot because
//! `ExAllocatePool2` never returns a base below [`MIN_ALIGNMENT`], so
//! rounding up moves the pointer by a multiple of [`MIN_ALIGNMENT`] — two
//! pointers — and never by less. [`NtAlloc::deallocate_aligned`] reads that
//! slot back out to recover the pointer that must be passed to
//! `ExFreePool`/`ExFreePoolWithTag`, which is why an over-aligned allocation
//! has to be released through it rather than through [`NtAlloc::deallocate`].
//!
//! Realignment is the default. A driver that never needs more than the pool's
//! own alignment can turn it off with the `REALIGN` parameter, as described
//! under [Turning realignment off](NtAlloc#turning-realignment-off).
//!
//! # Example
//!
//! ```no_run
//! use ntalloc::NonPagedAlloc;
//!
//! type Alloc = NonPagedAlloc;
//!
//! let ptr = Alloc::allocate(64);
//! assert!(!ptr.is_null());
//! // SAFETY: `ptr` came from `Alloc::allocate` and needed no realignment.
//! unsafe { Alloc::deallocate(ptr) };
//! ```
//!
//! # Features
//!
//! - **`allocator-api`** — implements the `Allocator` trait for
//! [`NtAlloc`], so a collection can use it directly, as in `Vec::new_in`
//! and `Box::new_in`, not only as the `#[global_allocator]`. It needs Rust
//! 1.100 or newer, where the trait is stable.
//! - **`allocator-api-markers`** — implements the `StaticAllocator` and
//! `AllocatorClone` marker traits for [`NtAlloc`]. `alloc` requires them in
//! places: `Box::pin_in` needs `StaticAllocator`, and cloning an `Arc` or
//! `Rc`, or using a `BTreeMap` at all, needs `AllocatorClone`. It implies
//! `allocator-api`. The marker traits are still unstable (`allocator_ext`),
//! so it needs a nightly compiler from October 2026 or later.
//! - **`usermode-backend`** — for tests only. Allocates from the process heap
//! (`std::alloc::System`) instead of the kernel pool, so code that allocates
//! through [`NtAlloc`] can run in tests on a normal machine. Where the pool
//! would bugcheck with `BAD_POOL_CALLER`, the test process prints what went
//! wrong and aborts instead: on a double free, on a pointer it never handed
//! out, and, with `TAGGED_DEALLOC`, on the wrong tag. On Windows any abort
//! is reported as `STATUS_STACK_BUFFER_OVERRUN`, whatever its cause, and
//! running the tests with `-- --test-threads=1` shows which test it was.
//! It brings in `std`, along with the `hashbrown` and `allocator-api2`
//! crates, so enable it only under `[dev-dependencies]`: through a normal
//! dependency it reaches the driver itself, which then fails to build with
//! a duplicate `panic_impl` lang item, as `std`'s panic handler clashes with
//! the driver's own. [`NtAlloc::kernel_allocate`] always uses the pool,
//! with or without it.
//!
//! ```toml
//! [dev-dependencies]
//! ntalloc = { version = "0.1", features = ["usermode-backend"] }
//! ```
// Doctests are this crate's documentation, so let them fail on unused imports
// rather than accumulating stale `use` lines. Scoped to `unused` rather than
// all warnings: this only ever runs for our own doctests, but a blanket deny
// would also break on an unrelated future lint in a dependency's API.
extern crate std;
use cratePOOL_FLAG_RAISE_ON_FAILURE;
pub use crate;
use ;
use ;
use ;
use ;
use ;
// The README's examples are compiled as doctests, all but the `Allocator` one,
// which needs Rust 1.100 and the allocator-api feature and is marked ignore.
;
/// Default memory tag for allocations, visible via kernel debugger pool
/// inspection tools such as `!poolused`/`!poolfind`.
///
/// Built with `from_ne_bytes` deliberately. Every Windows target is
/// little-endian and the kernel reads a pool tag as a little-endian `u32`,
/// so native byte order is what makes the debugger print `Rust` rather than
/// `tsuR`. This is not a portability oversight; do not change it to
/// `from_be_bytes`.
pub const DEFAULT_MEMORY_TAG: u32 = u32from_ne_bytes;
/// `"BIG "`, a tag the kernel reserves. Microsoft lists allocating with it as
/// bug check `BAD_POOL_CALLER` (`0x9C`): such memory would be untrackable, and
/// could corrupt the pool's tag tables.
const BIG_POOL_TAG: u32 = u32from_ne_bytes;
/// The lowest byte `ExAllocatePool2` allows in a pool tag character: `0x20`,
/// a space.
const MIN_TAG_CHAR: u8 = 0x20;
/// The highest byte `ExAllocatePool2` allows in a pool tag character: `0x7E`,
/// a tilde.
const MAX_TAG_CHAR: u8 = 0x7E;
/// Whether `tag` is one to four printable ASCII characters, each from
/// [`MIN_TAG_CHAR`] to [`MAX_TAG_CHAR`], in the order the debugger shows them,
/// followed only by zero bytes when it is shorter, as `ExAllocatePool2`'s
/// documentation requires.
const
/// Whether any byte of `tag` is an ASCII letter or digit. Microsoft lists
/// allocating with a tag that has neither as bug check `BAD_POOL_CALLER`
/// (`0x9D`), since such tags make pool usage hard to track.
const
/// A stateless, `#![no_std]`-friendly Windows kernel pool allocator.
///
/// `NtAlloc` is a zero-sized type: all of its behavior is selected entirely at
/// compile time via its const generic parameters, and every operation is
/// exposed as an associated function, so no instance ever needs to be
/// constructed to use them directly. An instance is still needed to satisfy
/// the [`GlobalAlloc`] trait bound (and, with the `allocator-api` feature,
/// the [`Allocator`] one), and can always be created with
/// [`Default::default()`] at zero runtime cost.
///
/// [`Allocator`]: core::alloc::Allocator
///
/// # Type Parameters
/// - `ALLOC_POOL_FLAGS`: the pool flags passed to `ExAllocatePool2`.
/// [`POOL_FLAG_PAGED`] or [`POOL_FLAG_NON_PAGED`] selects the pool, and
/// [`POOL_FLAG_UNINITIALIZED`], [`POOL_FLAG_CACHE_ALIGNED`],
/// [`POOL_FLAG_USE_QUOTA`] or [`POOL_FLAG_SPECIAL_POOL`] can be added to
/// it, as in the example below. Flags from `wdm.h` that this crate does not
/// export can be passed as raw values. `POOL_FLAG_RAISE_ON_FAILURE` is
/// refused; see [below](#unsupported-configurations).
/// - `MEMORY_TAG`: the 4-byte pool tag attached to every allocation, used by
/// tools such as `!poolused`/`!poolfind` to attribute pool memory back to
/// this allocator. Defaults to [`DEFAULT_MEMORY_TAG`]. It must be one to
/// four printable ASCII characters (`0x20` to `0x7E`), padded with zero
/// bytes at the end when shorter, with at least one letter or digit; see
/// [below](#unsupported-configurations).
/// - `TAGGED_DEALLOC`: selects the free routine used by [`NtAlloc::deallocate`]:
/// `true` uses `ExFreePoolWithTag` (the tag must match the one the memory
/// was allocated with), `false` uses the untagged `ExFreePool`. Defaults to
/// `true`.
/// - `REALIGN`: whether [`NtAlloc::allocate_aligned`], and through it the
/// [`GlobalAlloc`] and [`Allocator`] implementations, serve a [`Layout`]
/// aligned more strictly than the pool guarantees for its size (see
/// [`NtAlloc::kernel_alignment`]) by over-allocating and realigning, as the
/// [crate documentation](crate#design) describes. Defaults to `true`;
/// `false` turns realignment off, as described
/// [below](#turning-realignment-off).
///
/// Prefer the [`PagedAlloc`]/[`NonPagedAlloc`] aliases over naming `NtAlloc`
/// directly, unless the configuration needs more flags than the pool:
///
/// ```
/// use ntalloc::{NtAlloc, POOL_FLAG_NON_PAGED, POOL_FLAG_UNINITIALIZED};
///
/// // Non-paged pool that skips the zeroing ExAllocatePool2 otherwise does.
/// type ScratchAlloc = NtAlloc<{ POOL_FLAG_NON_PAGED | POOL_FLAG_UNINITIALIZED }>;
///
/// assert!(!ScratchAlloc::POOL_ZEROES);
/// ```
///
/// Call associated functions through an alias that names the whole type, such
/// as `type Alloc = NonPagedAlloc;` followed by `Alloc::allocate(64)`. Writing
/// `NonPagedAlloc::allocate(64)` does not compile (E0284): Rust fills in the
/// aliases' default parameters only where a type is expected.
///
/// # Turning realignment off
///
/// Realignment is on by default. A driver that never needs more than the
/// pool's own alignment can turn it off by setting `REALIGN` to `false`:
/// [`NtAlloc::allocate_aligned`], and with it the [`GlobalAlloc`] and
/// [`Allocator`] implementations, then pass every request to the pool by size
/// and ignore the alignment it asks for. Nothing is over-allocated or stored
/// next to a block, and a block is aligned only as the pool aligns it: to
/// [`MIN_ALIGNMENT`] below a page, and to a page from one page up.
///
/// Only do so if nothing allocated through such a configuration needs more,
/// including types from other crates, which a `#[global_allocator]` serves
/// too. Anything `#[repr(align(32))]` or larger needs more, as do 256- and
/// 512-bit SIMD vectors and cache-padded wrappers such as crossbeam's
/// `CachePadded`. A misaligned `Box` or reference is undefined behaviour, and
/// an aligned SIMD load from one faults, which in a driver is a bug check.
/// Debug builds usually stop at the first misaligned access, with "misaligned
/// pointer dereference"; release builds do not.
///
/// ```no_run
/// use core::alloc::{GlobalAlloc, Layout};
/// use ntalloc::{DEFAULT_MEMORY_TAG, MIN_ALIGNMENT, NonPagedAlloc};
///
/// type NoRealignAlloc = NonPagedAlloc<DEFAULT_MEMORY_TAG, true, false>;
///
/// let alloc = NoRealignAlloc::default();
///
/// // Asks for 64-byte alignment, but gets only what the pool gives a small block.
/// let layout = Layout::from_size_align(64, 64).unwrap();
/// // SAFETY: the layout has a non-zero size.
/// let ptr = unsafe { alloc.alloc(layout) };
/// assert!(!ptr.is_null());
/// assert_eq!(ptr as usize % MIN_ALIGNMENT, 0);
///
/// // SAFETY: from `alloc` with this layout, freed once.
/// unsafe { alloc.dealloc(ptr, layout) };
/// ```
///
/// # Unsupported configurations
///
/// These configurations break `ExAllocatePool2`'s documented rules, fail every
/// allocation, are listed by Microsoft as bug checks, or fail in a way Rust
/// cannot handle, so a type that uses one fails to build as soon as anything
/// allocates with it. The error comes from `cargo build`; `cargo check` does
/// not evaluate it.
///
/// - `POOL_FLAG_RAISE_ON_FAILURE` (`0x20`) makes `ExAllocatePool2` raise an
/// SEH exception on failure instead of returning null. Rust cannot catch
/// SEH: left unhandled the exception bugchecks the system, and handled
/// further up it unwinds through Rust frames, which is undefined behaviour.
/// - A zero `MEMORY_TAG`, which `ExAllocatePool2` rejects as invalid.
/// - A `MEMORY_TAG` that is not one to four printable ASCII characters
/// (`0x20` to `0x7E`) followed only by zero padding, as `ExAllocatePool2`'s
/// documentation requires: one with a control byte, a byte above `0x7E`, or
/// a zero byte before its last character.
/// - A `MEMORY_TAG` with no ASCII letter or digit, such as four spaces, which
/// Microsoft lists as bug check `BAD_POOL_CALLER` (`0x9D`).
/// - `"BIG "`, a tag the kernel reserves, which Microsoft lists as bug check
/// `BAD_POOL_CALLER` (`0x9C`).
///
/// Without `POOL_FLAG_RAISE_ON_FAILURE`, failure is reported as null.
/// Fallible APIs such as `Box::try_new_in` return it as an error, and
/// infallible ones such as `Box::new` pass it to `handle_alloc_error`, which
/// in a `#![no_std]` build panics by default: the fail-fast behaviour the flag
/// would otherwise give.
///
/// The flags are not validated beyond that: any other combination the pool
/// considers invalid fails at run time, like any other allocation failure.
///
/// ```compile_fail,E0080
/// use ntalloc::{NtAlloc, POOL_FLAG_NON_PAGED};
///
/// // 0x20 is POOL_FLAG_RAISE_ON_FAILURE, which this crate does not export.
/// type Raising = NtAlloc<{ POOL_FLAG_NON_PAGED | 0x20 }>;
///
/// let _ = Raising::allocate(64);
/// ```
///
/// ```compile_fail,E0080
/// use ntalloc::{NtAlloc, POOL_FLAG_NON_PAGED};
///
/// type Untagged = NtAlloc<POOL_FLAG_NON_PAGED, 0>;
///
/// let _ = Untagged::allocate(64);
/// ```
///
/// ```compile_fail,E0080
/// use ntalloc::{NtAlloc, POOL_FLAG_NON_PAGED};
///
/// // A zero byte before the last character.
/// type Gapped = NtAlloc<POOL_FLAG_NON_PAGED, { u32::from_ne_bytes(*b"ab\0c") }>;
///
/// let _ = Gapped::allocate(64);
/// ```
///
/// ```compile_fail,E0080
/// use ntalloc::{NtAlloc, POOL_FLAG_NON_PAGED};
///
/// type Blank = NtAlloc<POOL_FLAG_NON_PAGED, { u32::from_ne_bytes(*b" ") }>;
///
/// let _ = Blank::allocate(64);
/// ```
///
/// ```compile_fail,E0080
/// use ntalloc::{NtAlloc, POOL_FLAG_NON_PAGED};
///
/// type Big = NtAlloc<POOL_FLAG_NON_PAGED, { u32::from_ne_bytes(*b"BIG ") }>;
///
/// let _ = Big::allocate(64);
/// ```
;
/// [`NtAlloc`] backed by the paged pool ([`POOL_FLAG_PAGED`]).
///
/// Memory from the paged pool may be paged out to disk, so it may only be
/// touched at IRQL `APC_LEVEL` or below — that is, strictly below
/// `DISPATCH_LEVEL`. Never touch it from a DPC, from an ISR, or while
/// holding a spin lock.
pub type PagedAlloc<
const MEMORY_TAG: u32 = DEFAULT_MEMORY_TAG,
const TAGGED_DEALLOC: bool = true,
const REALIGN: bool = true,
> = ;
/// [`NtAlloc`] backed by the non-paged pool ([`POOL_FLAG_NON_PAGED`]).
///
/// Non-paged pool memory is always resident and safe to access at any IRQL,
/// at the cost of being a more constrained system-wide resource than paged
/// pool.
pub type NonPagedAlloc<
const MEMORY_TAG: u32 = DEFAULT_MEMORY_TAG,
const TAGGED_DEALLOC: bool = true,
const REALIGN: bool = true,
> = ;
/// Size, in bytes, of a pointer on the target platform.
pub const POINTER_SIZE: usize = ;
/// The minimum alignment `ExAllocatePool2` guarantees for allocations smaller
/// than a page. Mirrors the WDK's `MEMORY_ALLOCATION_ALIGNMENT` (two
/// pointers: 16 bytes on 64-bit targets, 8 bytes on 32-bit).
pub const MIN_ALIGNMENT: usize = POINTER_SIZE * 2;
// SAFETY: [`GlobalAlloc`] asks for blocks that fit the layout and stay valid
// until freed, and for no method to unwind. For [`NtAlloc`]:
//
// - `alloc` returns null or `layout.size()` bytes aligned to `layout.align()`:
// the pool's own block when its alignment suffices, otherwise an address
// realigned inside a block over-allocated by `layout.align()`. With `REALIGN`
// set to `false` it returns the pool's own block whatever the layout asks
// for, so that promise holds only for layouts within the pool's alignment;
// NtAlloc documents that such a configuration must not be asked for more;
// - a block belongs to the pool, not to the allocator value, so it stays valid
// until `dealloc` hands it back, recovering the pool's own address from the
// same layout;
// - `alloc_zeroed` returns zeroed memory: the pool zeroes it unless
// [`POOL_FLAG_UNINITIALIZED`] is set, and then it writes the zeroes itself;
// - in a release build nothing on the pool path panics; its `debug_assert!`s
// fire only on a bug here or on a caller that broke the contract, and the
// heap backend aborts rather than unwinds on misuse.
unsafe
// SAFETY: [`Allocator`] asks that a block stay valid until it is deallocated,
// however the allocator value is copied, moved or dropped, and that a copy
// behave like the original. For [`NtAlloc`]:
//
// - blocks come from the pool (or the heap backend), never from the value,
// which is a zero-sized `Copy` type that owns nothing, so no copy, move or
// drop of it can invalidate one;
// - every value of one type makes the same pool calls, so any of them can free
// what another allocated;
// - `allocate` reports exactly `layout.size()` bytes, so the layout the free
// path receives is the one the stash decision was made with;
// - with `REALIGN` set to `false`, `allocate` returns the pool's own block
// whatever `layout.align()` asks for, so the block fits the layout only when
// the pool's alignment covers it, which NtAlloc documents as the condition
// for using such a configuration;
// - a zero-size request gets a dangling, aligned pointer, which `deallocate`
// recognizes and never frees.
unsafe
// SAFETY: [`StaticAllocator`] asks that nothing about the allocator's own
// lifetime can invalidate memory it handed out. For [`NtAlloc`]:
//
// - it has no `Drop` impl, so dropping one releases nothing;
// - it is a zero-sized type holding no state;
// - its lifetime expiring frees nothing, because it owns nothing;
// - nothing reachable from safe code can invalidate an allocation. The safe
// public surface is [`NtAlloc::POOL_ZEROES`], [`NtAlloc::kernel_alignment`],
// [`NtAlloc::kernel_allocate`], [`NtAlloc::allocate`] and
// [`NtAlloc::allocate_aligned`], none of which free anything. Of the five
// functions that do return memory, two are `unsafe`
// ([`NtAlloc::deallocate`] and [`NtAlloc::deallocate_aligned`]) and the other
// three are private and called only from inside those two;
// - all of the above hold for every equivalent allocator, since instances
// sharing the same const parameters are indistinguishable.
//
// A pool block therefore stays valid until the matching `ExFreePool` or
// `ExFreePoolWithTag`, regardless of what happens to the allocator value.
unsafe
// SAFETY: [`AllocatorClone`] asks that a clone be interchangeable with the
// original, so that either can free what the other allocated. [`NtAlloc`] is a
// zero-sized type carrying no state: the pool, the tag and the free routine all
// come from its const parameters, so two values of the same type forward to the
// same pool calls identically. Moving or dropping one frees nothing, so blocks
// stay valid while clones live, and the derived `Clone` copies a zero-sized
// value and cannot unwind. The type is also `Copy`, which preserves that
// equivalence exactly as cloning does.
unsafe