polkavm 0.29.1

A fast and secure RISC-V based virtual machine
Documentation
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
use core::mem::replace;

#[rustfmt::skip] // Screws up the formatting otherwise.
macro_rules! define_for_size {
    ($d:tt $Size:ty) => {
        // This is based on: https://github.com/sebbbi/OffsetAllocator/blob/main/offsetAllocator.cpp
        #[doc(hidden)]
        #[inline]
        pub const fn to_bin_index<const MANTISSA_BITS: u32, const ROUND_UP: bool>(size: $Size) -> u32 {
            if size == 0 {
                return 0;
            }

            let mantissa_value = 1 << MANTISSA_BITS;
            if size < mantissa_value {
                // The first 2^MANTISSA_BITS buckets contain only a single element.
                return (size - 1) as u32;
            }

            let mantissa_start_bit: u32 = (core::mem::size_of::<$Size>() as u32 * 8 - 1 - size.leading_zeros()) - MANTISSA_BITS;
            let exponent = mantissa_start_bit + 1;
            let mut mantissa = (size >> mantissa_start_bit) & (mantissa_value - 1);

            if ROUND_UP {
                let low_bits_mask: $Size = (1 << mantissa_start_bit) - 1;
                if (size & low_bits_mask) != 0 {
                    mantissa += 1;
                }
            }

            let out = exponent << MANTISSA_BITS;
            let mantissa = mantissa as u32;
            if ROUND_UP {
                out + mantissa - 1
            } else {
                (out | mantissa) - 1
            }
        }

        #[doc(hidden)]
        pub const fn calculate_optimal_bin_config<PrimaryMask, SecondaryMask>(
            max_allocation_size: $Size,
            mut requested_max_bins: u32,
        ) -> super::AllocatorBinConfig {
            let true_max_bins = (core::mem::size_of::<PrimaryMask>() * 8 * ::core::mem::size_of::<SecondaryMask>() * 8) as u32;
            if true_max_bins < requested_max_bins {
                requested_max_bins = true_max_bins
            }

            macro_rules! try_all {
                ($d($mantissa_bits:expr),+) => {
                    $d(
                        let highest_bin_index = to_bin_index::<$mantissa_bits, true>(max_allocation_size);
                        if highest_bin_index < requested_max_bins {
                            return super::AllocatorBinConfig {
                                mantissa_bits: $mantissa_bits,
                                bin_count: highest_bin_index + 1
                            };
                        }
                    )+
                }
            }

            try_all! {
                8, 7, 6, 5, 4, 3, 2, 1
            }

            panic!("failed to calculate optimal configuration for the allocator");
        }
    };
}

#[cfg(kani)]
#[kani::proof]
fn proof_to_bin_index_rounded_up_is_never_less_than_rounded_down() {
    let size: u64 = kani::any();
    let bin_rounded_down = self::u64::to_bin_index::<8, false>(size);
    let bin_rounded_up = self::u64::to_bin_index::<8, true>(size);
    assert!(bin_rounded_down <= bin_rounded_up);
}

#[cfg(kani)]
#[kani::proof]
fn proof_bin_indexes_never_decrease_with_increasing_size() {
    let size: u64 = kani::any_where(|&size| size < u64::MAX);
    let current_bin = self::u64::to_bin_index::<8, false>(size);
    let next_bin = self::u64::to_bin_index::<8, false>(size + 1);
    assert!(next_bin >= current_bin);
}

#[cfg(kani)]
#[kani::proof]
fn proof_bitness_of_the_size_does_not_matter_when_calculating_the_bin_index() {
    let size: u32 = kani::any();
    let bin32 = self::u32::to_bin_index::<8, false>(size);
    let bin64 = self::u64::to_bin_index::<8, false>(u64::from(size));
    assert!(bin32 == bin64);
}

// Printing out the bucket ranges:
//
// ```rust
// const MANTISSA_BITS: u32 = 3;
// let mut lower_bound = 0;
// let mut bin = to_bin_index::<MANTISSA_BITS, true>(0);
//
// for n in 1..=u32::MAX {
//     let next_bin = to_bin_index::<MANTISSA_BITS, true>(n);
//     if bin != next_bin {
//         println!("{}: {}..={}", bin, lower_bound, n - 1);
//         lower_bound = n;
//         bin = next_bin;
//     }
// }
// println!("{}: {}..", bin, lower_bound);
// ```

#[test]
fn test_to_bin_index() {
    assert_eq!(self::u32::to_bin_index::<3, true>(0), 0);
    assert_eq!(self::u32::to_bin_index::<3, true>(1), 0);
    assert_eq!(self::u32::to_bin_index::<3, true>(2), 1);
}

#[doc(hidden)]
#[cfg_attr(test, derive(PartialEq, Debug))]
pub struct AllocatorBinConfig {
    pub mantissa_bits: u32,
    pub bin_count: u32,
}

#[test]
fn test_calculate_optimal_bin_config() {
    assert_eq!(
        self::u32::calculate_optimal_bin_config::<u64, u64>((i32::MAX as u32) / 4096, u32::MAX),
        AllocatorBinConfig {
            mantissa_bits: 8,
            bin_count: 3072
        }
    );

    assert_eq!(
        self::u32::calculate_optimal_bin_config::<u64, u64>((i32::MAX as u32) / 4096, 3072),
        AllocatorBinConfig {
            mantissa_bits: 8,
            bin_count: 3072
        }
    );

    assert_eq!(
        self::u32::calculate_optimal_bin_config::<u64, u64>((i32::MAX as u32) / 4096, 3071),
        AllocatorBinConfig {
            mantissa_bits: 7,
            bin_count: 1664
        }
    );
}

trait EmptyInit {
    fn empty_init() -> Self;
}

impl<const LENGTH: usize> EmptyInit for [u32; LENGTH] {
    #[inline]
    fn empty_init() -> Self {
        [EMPTY; LENGTH]
    }
}

trait BitIndexT: Copy + core::fmt::Debug {
    fn index(&self) -> usize;
}

trait BitMaskT: Default {
    type Index: BitIndexT;
    fn index(index: u32) -> Self::Index;
    fn set(&mut self, index: Self::Index);
    fn unset(&mut self, index: Self::Index);
    fn find_first(&mut self, min_index: Self::Index) -> Option<Self::Index>;
}

impl<Primary, Secondary> BitIndexT for crate::bit_mask::BitIndex<Primary, Secondary>
where
    Primary: Copy + core::fmt::Debug,
    Secondary: Copy + core::fmt::Debug,
{
    #[inline]
    fn index(&self) -> usize {
        Self::index(self)
    }
}

impl<Primary, Secondary, const SECONDARY_LENGTH: usize> BitMaskT for crate::bit_mask::BitMask<Primary, Secondary, SECONDARY_LENGTH>
where
    Primary: crate::bit_mask::RawMask,
    Secondary: crate::bit_mask::RawMask,
{
    type Index = crate::bit_mask::BitIndex<Primary, Secondary>;

    #[inline]
    fn index(index: u32) -> Self::Index {
        Self::index(index)
    }

    #[inline]
    fn set(&mut self, index: Self::Index) {
        Self::set(self, index)
    }

    #[inline]
    fn unset(&mut self, index: Self::Index) {
        Self::unset(self, index)
    }

    #[inline]
    fn find_first(&mut self, min_index: Self::Index) -> Option<Self::Index> {
        Self::find_first(self, min_index)
    }
}

pub trait SizeT:
    Copy
    + From<u32>
    + Ord
    + core::ops::Add<Output = Self>
    + core::ops::AddAssign
    + core::ops::Sub<Output = Self>
    + core::fmt::LowerHex
    + core::fmt::Debug
    + core::hash::Hash
{
    #[doc(hidden)]
    const ZERO: Self;
}

impl SizeT for u32 {
    const ZERO: Self = 0;
}

impl SizeT for u64 {
    const ZERO: Self = 0;
}

pub trait AllocatorConfig {
    #[doc(hidden)]
    type Size: SizeT;

    #[doc(hidden)]
    const MAX_ALLOCATION_SIZE: Self::Size;

    #[doc(hidden)]
    #[allow(private_bounds)]
    type BitMask: BitMaskT;

    #[doc(hidden)]
    #[allow(private_bounds)]
    type BinArray: core::ops::Index<usize, Output = u32> + core::ops::IndexMut<usize> + EmptyInit;

    #[doc(hidden)]
    fn to_bin_index<const ROUND_UP: bool>(size: Self::Size) -> u32;
}

pub mod u32 {
    define_for_size!($ u32);
}

#[cfg(any(kani, test, feature = "export-internals-for-testing"))]
pub mod u64 {
    define_for_size!($ u64);
}

// TODO: Remove this once this is fixed: https://github.com/rust-lang/rust/issues/60551
#[doc(hidden)]
#[macro_export]
macro_rules! _allocator_config {
    (
        impl AllocatorConfig for $type:ty {
            const MAX_ALLOCATION_SIZE: $Size:ident = $max_allocation_size:expr;
            const MAX_BINS: u32 = $max_bins:expr;
        }
    ) => {
        impl $crate::generic_allocator::AllocatorConfig for $type {
            type Size = $Size;
            const MAX_ALLOCATION_SIZE: $Size = $max_allocation_size;
            type BitMask = $crate::bit_mask::bitmask_type!(
                usize,
                usize,
                $crate::generic_allocator::$Size::calculate_optimal_bin_config::<usize, usize>($max_allocation_size, $max_bins).bin_count
                    as usize
            );
            type BinArray =
                [u32; $crate::generic_allocator::$Size::calculate_optimal_bin_config::<usize, usize>($max_allocation_size, $max_bins)
                    .bin_count as usize];

            fn to_bin_index<const ROUND_UP: bool>(size: $Size) -> u32 {
                const MANTISSA_BITS: u32 =
                    $crate::generic_allocator::$Size::calculate_optimal_bin_config::<usize, usize>($max_allocation_size, $max_bins)
                        .mantissa_bits;
                $crate::generic_allocator::$Size::to_bin_index::<MANTISSA_BITS, ROUND_UP>(size)
            }
        }
    };
}

pub use _allocator_config as allocator_config;

const EMPTY: u32 = u32::MAX;

#[derive(Clone, Debug)]
struct Node<Size> {
    next_by_address: u32,
    prev_by_address: u32,
    next_in_bin: u32,
    prev_in_bin: u32,
    offset: Size,
    size: Size,
    is_allocated: bool,
}

#[derive(Debug)]
pub struct GenericAllocator<C: AllocatorConfig> {
    nodes: Vec<Node<C::Size>>,
    unused_node_slots: Vec<u32>,
    bins_with_free_space: C::BitMask,
    first_unallocated_for_bin: C::BinArray,
}

#[derive(Copy, Clone, Debug)]
pub struct GenericAllocation<Size> {
    node: u32,
    offset: Size,
    size: Size,
}

impl<Size> GenericAllocation<Size>
where
    Size: SizeT,
{
    pub const EMPTY: GenericAllocation<Size> = GenericAllocation {
        node: EMPTY,
        offset: Size::ZERO,
        size: Size::ZERO,
    };

    pub fn is_empty(&self) -> bool {
        self.node == EMPTY
    }

    pub fn offset(&self) -> Size {
        self.offset
    }

    pub fn size(&self) -> Size {
        self.size
    }
}

impl<C> GenericAllocator<C>
where
    C: AllocatorConfig,
{
    pub fn new(total_space: C::Size) -> Self {
        let mut mutable = GenericAllocator {
            bins_with_free_space: C::BitMask::default(),
            first_unallocated_for_bin: C::BinArray::empty_init(),
            nodes: Vec::new(),
            unused_node_slots: Vec::new(),
        };

        mutable.insert_free_node(C::Size::from(0), total_space);
        mutable
    }

    fn size_to_bin_round_down(size: C::Size) -> <C::BitMask as BitMaskT>::Index {
        let size = core::cmp::min(size, C::MAX_ALLOCATION_SIZE);
        C::BitMask::index(C::to_bin_index::<false>(size))
    }

    fn size_to_bin_round_up(size: C::Size) -> <C::BitMask as BitMaskT>::Index {
        let size = core::cmp::min(size, C::MAX_ALLOCATION_SIZE);
        C::BitMask::index(C::to_bin_index::<true>(size))
    }

    fn insert_free_node(&mut self, offset: C::Size, size: C::Size) -> Option<u32> {
        if size == C::Size::from(0) {
            return None;
        }

        // Get the bin index; round down to make sure the node's size is at least as big as what the bin expects.
        let bin = Self::size_to_bin_round_down(size);

        let first_node_in_bin = self.first_unallocated_for_bin[bin.index()];
        let region = Node {
            next_by_address: EMPTY,
            prev_by_address: EMPTY,
            next_in_bin: first_node_in_bin,
            prev_in_bin: EMPTY,
            offset,
            size,
            is_allocated: false,
        };

        let new_node = if let Some(new_node) = self.unused_node_slots.pop() {
            self.nodes[new_node as usize] = region;
            new_node
        } else {
            let new_node = self.nodes.len() as u32;
            self.nodes.push(region);
            new_node
        };

        if let Some(first_node_in_bin) = self.nodes.get_mut(first_node_in_bin as usize) {
            first_node_in_bin.prev_in_bin = new_node
        } else {
            self.bins_with_free_space.set(bin);
        }

        self.first_unallocated_for_bin[bin.index()] = new_node;
        Some(new_node)
    }

    fn remove_node(&mut self, node: u32) {
        let prev_in_bin = self.nodes[node as usize].prev_in_bin;
        if prev_in_bin != EMPTY {
            let next_in_bin = self.nodes[node as usize].next_in_bin;
            self.nodes[prev_in_bin as usize].next_in_bin = next_in_bin;

            if let Some(next) = self.nodes.get_mut(next_in_bin as usize) {
                next.prev_in_bin = prev_in_bin;
            } else {
                debug_assert_eq!(next_in_bin, EMPTY);
            }
        } else {
            let bin = Self::size_to_bin_round_down(self.nodes[node as usize].size);
            self.remove_first_free_node(node, bin);
        }

        self.unused_node_slots.push(node);
    }

    #[inline]
    fn remove_first_free_node(&mut self, node: u32, bin: <C::BitMask as BitMaskT>::Index) {
        debug_assert_eq!(self.first_unallocated_for_bin[bin.index()], node);

        let next_in_bin = self.nodes[node as usize].next_in_bin;
        self.first_unallocated_for_bin[bin.index()] = next_in_bin;
        if let Some(next) = self.nodes.get_mut(next_in_bin as usize) {
            next.prev_in_bin = EMPTY
        } else {
            debug_assert_eq!(next_in_bin, EMPTY);
            self.bins_with_free_space.unset(bin);
        }
    }

    pub fn alloc(&mut self, size: C::Size) -> Option<GenericAllocation<C::Size>> {
        if size == C::Size::from(0) {
            return Some(GenericAllocation::EMPTY);
        }

        if size > C::MAX_ALLOCATION_SIZE {
            return None;
        }

        // Calculate the minimum bin to fit this allocation; round up in case the size doesn't match the bin size exactly.
        let min_bin = Self::size_to_bin_round_up(size);

        // Find a bin with enough free space and allocate a node there.
        let (bin, node) = if let Some(bin) = self.bins_with_free_space.find_first(min_bin) {
            (bin, self.first_unallocated_for_bin[bin.index()])
        } else {
            // No such bin exists; let's try rounding down and see if maybe we can find an oversized region in the previous bin.
            let bin = self.bins_with_free_space.find_first(Self::size_to_bin_round_down(size))?;
            let node = self.first_unallocated_for_bin[bin.index()];

            if self.nodes[node as usize].size < size {
                return None;
            }

            (bin, node)
        };

        let original_size = replace(&mut self.nodes[node as usize].size, size);
        debug_assert!(original_size >= size);
        debug_assert!(!self.nodes[node as usize].is_allocated);
        self.nodes[node as usize].is_allocated = true;

        // Remove the node from the bin's free node list.
        self.remove_first_free_node(node, bin);

        // If we haven't allocated all of the free space then add what's remaining back for later use.
        let offset = self.nodes[node as usize].offset;
        let remaining_free_pages = original_size - size;
        if let Some(new_free_node) = self.insert_free_node(offset + size, remaining_free_pages) {
            // Link the nodes together so that we can later merge them.
            let next_by_address = replace(&mut self.nodes[node as usize].next_by_address, new_free_node);
            if let Some(next) = self.nodes.get_mut(next_by_address as usize) {
                next.prev_by_address = new_free_node;
            } else {
                debug_assert_eq!(next_by_address, EMPTY);
            }
            self.nodes[new_free_node as usize].prev_by_address = node;
            self.nodes[new_free_node as usize].next_by_address = next_by_address;
        }

        Some(GenericAllocation { node, offset, size })
    }

    pub fn free(&mut self, alloc: GenericAllocation<C::Size>) {
        if alloc.is_empty() {
            return;
        }

        let GenericAllocation {
            node,
            mut offset,
            mut size,
        } = alloc;

        // Check if there's a free node before this node with which we can merge.
        {
            let prev_by_address = self.nodes[node as usize].prev_by_address;
            if (prev_by_address != EMPTY) && !self.nodes[prev_by_address as usize].is_allocated {
                offset = self.nodes[prev_by_address as usize].offset;
                size += self.nodes[prev_by_address as usize].size;

                self.remove_node(prev_by_address);

                debug_assert_eq!(self.nodes[prev_by_address as usize].next_by_address, node);
                self.nodes[node as usize].prev_by_address = self.nodes[prev_by_address as usize].prev_by_address;
            }
        }

        // Check if there's a free node after this node with which we can merge.
        {
            let next_by_address = self.nodes[node as usize].next_by_address;
            if (next_by_address != EMPTY) && !self.nodes[next_by_address as usize].is_allocated {
                size += self.nodes[next_by_address as usize].size;

                self.remove_node(next_by_address);

                debug_assert_eq!(self.nodes[next_by_address as usize].prev_by_address, node);
                self.nodes[node as usize].next_by_address = self.nodes[next_by_address as usize].next_by_address;
            }
        }

        let next_by_address = self.nodes[node as usize].next_by_address;
        let prev_by_address = self.nodes[node as usize].prev_by_address;
        self.unused_node_slots.push(node);

        let new_node = self.insert_free_node(offset, size);
        debug_assert!(new_node.is_some());
        if let Some(new_node) = new_node {
            if next_by_address != EMPTY {
                self.nodes[new_node as usize].next_by_address = next_by_address;
                self.nodes[next_by_address as usize].prev_by_address = new_node;
            }

            if prev_by_address != EMPTY {
                self.nodes[new_node as usize].prev_by_address = prev_by_address;
                self.nodes[prev_by_address as usize].next_by_address = new_node;
            }
        }
    }
}

#[cfg(any(test, feature = "export-internals-for-testing"))]
pub mod tests {
    use super::{GenericAllocation, GenericAllocator};
    use std::collections::HashMap;

    pub struct TestAllocator<C>
    where
        C: super::AllocatorConfig,
    {
        allocator: GenericAllocator<C>,
        allocations: HashMap<C::Size, GenericAllocation<C::Size>>,
    }

    impl<C> TestAllocator<C>
    where
        C: super::AllocatorConfig,
    {
        pub fn new(size: C::Size) -> Self {
            Self {
                allocator: GenericAllocator::<C>::new(size),
                allocations: HashMap::new(),
            }
        }

        pub fn alloc(&mut self, size: C::Size) -> Option<C::Size> {
            let allocation = self.allocator.alloc(size)?;
            if size == C::Size::from(0) {
                assert_eq!(allocation.offset(), C::Size::from(0));
                return Some(allocation.offset());
            }

            let pointer = allocation.offset();
            for (&old_pointer, old_allocation) in &self.allocations {
                let is_overlapping = (pointer >= old_pointer && pointer < (old_pointer + old_allocation.size()))
                    || (pointer + allocation.size() > old_pointer && pointer + allocation.size() <= (old_pointer + old_allocation.size()));

                assert!(
                    !is_overlapping,
                    "overlapping allocation: original = 0x{:08x}-0x{:08x}, new = 0x{:08x}-0x{:08x}",
                    old_pointer,
                    old_pointer + old_allocation.size(),
                    pointer,
                    pointer + allocation.size(),
                );
            }

            let offset = allocation.offset();
            assert!(self.allocations.insert(allocation.offset(), allocation).is_none());
            Some(offset)
        }

        pub fn free(&mut self, pointer: C::Size) -> bool {
            if let Some(allocation) = self.allocations.remove(&pointer) {
                self.allocator.free(allocation);
                true
            } else {
                false
            }
        }
    }

    #[cfg(test)]
    struct TestConfig64;

    #[cfg(test)]
    crate::generic_allocator::allocator_config! {
        impl AllocatorConfig for TestConfig64 {
            const MAX_ALLOCATION_SIZE: u64 = 6000000;
            const MAX_BINS: u32 = 4096;
        }
    }

    #[test]
    fn test_allocations_over_the_max_size_fail() {
        let mut allocator = TestAllocator::<TestConfig64>::new(6000000);
        let a0 = allocator.alloc(6000000).unwrap();
        assert_eq!(allocator.alloc(6000000), None);
        assert!(allocator.free(a0));
        let a0 = allocator.alloc(6000000).unwrap();
        assert!(allocator.free(a0));
        assert_eq!(allocator.alloc(6000001), None);
        assert_eq!(allocator.alloc(u64::MAX), None);
    }

    #[test]
    fn test_zero_max_size_allocator_only_gives_out_zero_sized_allocations() {
        let mut allocator = TestAllocator::<TestConfig64>::new(0);
        assert!(allocator.alloc(1).is_none());
        assert!(allocator.alloc(0).is_some());
    }

    #[test]
    fn test_allocator_after_using_up_all_free_space() {
        let mut allocator = TestAllocator::<TestConfig64>::new(3);
        assert!(allocator.alloc(1).is_some());
        assert!(allocator.alloc(1).is_some());
        assert!(allocator.alloc(1).is_some());
        assert!(allocator.alloc(1).is_none());
        assert!(allocator.alloc(0).is_some());
    }
}