bomalloc 0.3.1

A fragmentation-aware experimental allocator built around address-span ownership
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
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
#![cfg(any(windows, target_os = "linux"))]

use std::alloc::Layout;
use std::ptr::NonNull;
use std::sync::{Arc, Barrier, mpsc};
use std::thread;

use bomalloc::{
    BoAllocError, BoAllocator, Config, MAX_ADAPTIVE_RUN_TERRITORY_EXTENSION_EXTENTS,
    MAX_BLACK_SPAN_RING_CAPACITY, MAX_LOCAL_MAGAZINE_CAPACITY, MAX_LOW_CAPACITY_PAGE_QUEUE_SPANS,
    MAX_RECENT_FREE_WORD_HINTS, MAX_REMOTE_CACHE_CAPACITY, MAX_RUN_LOCAL_WINDOW_CAPACITY,
    SIZE_CLASSES, TrimMode,
};

fn thin(allocation: NonNull<[u8]>) -> NonNull<u8> {
    // A slice pointer has the same data address as its first element.
    NonNull::new(allocation.as_ptr() as *mut u8).unwrap()
}

#[test]
fn zero_sized_allocation_is_aligned_and_free_is_noop() {
    let allocator = BoAllocator::new(Config::default()).unwrap();
    let layout = Layout::from_size_align(0, 4096).unwrap();
    let allocation = allocator.allocate(layout).unwrap();
    let pointer = thin(allocation);
    assert_eq!(pointer.as_ptr() as usize % 4096, 0);
    // SAFETY: this is the matching zero-sized allocation and zero-sized deallocation is a no-op.
    unsafe { allocator.deallocate(pointer, layout) };
    allocator.validate().unwrap();
}

#[test]
fn oversized_local_magazine_configuration_is_rejected() {
    assert!(matches!(
        BoAllocator::new(Config {
            local_magazine_capacity: MAX_LOCAL_MAGAZINE_CAPACITY + 1,
            ..Config::default()
        }),
        Err(BoAllocError::InvalidConfig)
    ));
}

#[test]
fn oversized_black_span_ring_configuration_is_rejected() {
    assert!(matches!(
        BoAllocator::new(Config {
            black_span_ring_capacity: MAX_BLACK_SPAN_RING_CAPACITY + 1,
            ..Config::default()
        }),
        Err(BoAllocError::InvalidConfig)
    ));
}

#[test]
fn oversized_remote_cache_configuration_is_rejected() {
    assert!(matches!(
        BoAllocator::new(Config {
            remote_cache_capacity: MAX_REMOTE_CACHE_CAPACITY + 1,
            ..Config::default()
        }),
        Err(BoAllocError::InvalidConfig)
    ));
}

#[test]
fn invalid_recent_free_hint_depth_is_rejected() {
    for recent_free_word_hints in [0, MAX_RECENT_FREE_WORD_HINTS + 1] {
        assert!(matches!(
            BoAllocator::new(Config {
                recent_free_word_hints,
                ..Config::default()
            }),
            Err(BoAllocError::InvalidConfig)
        ));
    }
}

#[test]
fn oversized_low_capacity_page_queue_is_rejected() {
    assert!(matches!(
        BoAllocator::new(Config {
            low_capacity_page_queue_spans: MAX_LOW_CAPACITY_PAGE_QUEUE_SPANS + 1,
            ..Config::default()
        }),
        Err(BoAllocError::InvalidConfig)
    ));
}

#[test]
fn oversized_adaptive_territory_extension_is_rejected() {
    assert_eq!(
        BoAllocator::new(Config {
            adaptive_territory_extension_spans: bomalloc::MAX_ADAPTIVE_TERRITORY_EXTENSION_SPANS
                + 1,
            ..Config::default()
        })
        .err(),
        Some(BoAllocError::InvalidConfig)
    );
}

#[test]
fn oversized_adaptive_run_territory_extension_is_rejected() {
    assert_eq!(
        BoAllocator::new(Config {
            adaptive_run_territory_extension_extents: MAX_ADAPTIVE_RUN_TERRITORY_EXTENSION_EXTENTS
                + 1,
            ..Config::default()
        })
        .err(),
        Some(BoAllocError::InvalidConfig)
    );
}

#[test]
fn oversized_run_local_window_is_rejected() {
    assert_eq!(
        BoAllocator::new(Config {
            run_local_window_capacity: MAX_RUN_LOCAL_WINDOW_CAPACITY + 1,
            ..Config::default()
        })
        .err(),
        Some(BoAllocError::InvalidConfig)
    );
}

#[test]
fn young_ring_cannot_exceed_the_recent_span_cache() {
    assert!(matches!(
        BoAllocator::new(Config {
            recent_spans_per_class: 2,
            young_ring_spans: 3,
            ..Config::default()
        }),
        Err(BoAllocError::InvalidConfig)
    ));
}

#[test]
fn young_ring_seals_old_spans_and_cross_thread_free_remains_safe() {
    let allocator = BoAllocator::new(Config {
        young_ring_spans: 1,
        recent_spans_per_class: 2,
        locality_reserve_spans: 2,
        local_magazine_capacity: 0,
        ..Config::default()
    })
    .unwrap();
    let layout = Layout::from_size_align(1024, 1024).unwrap();
    let mut pointers: Vec<_> = (0..160)
        .map(|_| thin(allocator.allocate(layout).unwrap()))
        .collect();
    let released_hole = pointers.remove(0);
    // SAFETY: release one object from a now-old span to expose sealed-hole accounting.
    unsafe { allocator.deallocate(released_hole, layout) };
    let live = allocator.stats();
    assert!(live.cohort_promotions >= 1);
    assert!(live.sealed_black_spans >= 1);
    assert!(live.young_spans <= 1);
    assert!(live.sealed_hole_bytes > 0);

    let consumer = allocator.clone();
    let addresses: Vec<_> = pointers
        .into_iter()
        .map(|pointer| pointer.as_ptr() as usize)
        .collect();
    thread::spawn(move || {
        for address in addresses {
            // SAFETY: each unique pointer is transferred and released once.
            unsafe {
                consumer.deallocate(NonNull::new(address as *mut u8).unwrap(), layout);
            }
        }
    })
    .join()
    .unwrap();

    assert_eq!(allocator.stats().requested_live_bytes, 0);
    allocator.trim(TrimMode::Aggressive);
    allocator.validate().unwrap();
    assert_eq!(allocator.stats().sealed_black_spans, 0);
}

#[test]
fn young_ring_does_not_seal_classes_larger_than_four_kib() {
    let allocator = BoAllocator::new(Config {
        young_ring_spans: 1,
        recent_spans_per_class: 2,
        local_magazine_capacity: 0,
        ..Config::default()
    })
    .unwrap();
    let layout = Layout::from_size_align(8192, 4096).unwrap();
    let pointers: Vec<_> = (0..32)
        .map(|_| thin(allocator.allocate(layout).unwrap()))
        .collect();
    let stats = allocator.stats();
    assert_eq!(stats.cohort_promotions, 0);
    assert_eq!(stats.sealed_black_spans, 0);
    for pointer in pointers {
        // SAFETY: every pointer is live and paired with layout.
        unsafe { allocator.deallocate(pointer, layout) };
    }
    allocator.trim(TrimMode::Aggressive);
    allocator.validate().unwrap();
}

#[test]
fn all_small_classes_allocate_align_write_and_reuse() {
    let allocator = BoAllocator::new(Config::default()).unwrap();
    for &size in &SIZE_CLASSES {
        let alignment = size.next_power_of_two().min(4096);
        let layout = Layout::from_size_align(size, alignment).unwrap();
        let first = thin(allocator.allocate(layout).unwrap());
        assert_eq!(first.as_ptr() as usize % alignment, 0);
        // SAFETY: the allocation is live for at least layout.size() bytes.
        unsafe { std::ptr::write_bytes(first.as_ptr(), 0xa5, layout.size()) };
        // SAFETY: pointer/layout are the live matching allocation.
        unsafe { allocator.deallocate(first, layout) };
        let mut recycled = Vec::new();
        for _ in 0..Config::default().local_magazine_capacity {
            let pointer = thin(allocator.allocate(layout).unwrap());
            assert_eq!(pointer.as_ptr() as usize % alignment, 0);
            assert!(
                !recycled.contains(&pointer),
                "magazine returned a live slot twice"
            );
            recycled.push(pointer);
        }
        assert!(
            recycled.contains(&first),
            "the thread-local magazine should retain the released slot"
        );
        for pointer in recycled {
            // SAFETY: each unique pointer/layout pair is live exactly once.
            unsafe { allocator.deallocate(pointer, layout) };
        }
    }
    allocator.validate().unwrap();
    assert_eq!(allocator.stats().requested_live_bytes, 0);
}

#[test]
fn medium_large_and_overaligned_paths_round_trip() {
    let allocator = BoAllocator::new(Config::default()).unwrap();
    for layout in [
        Layout::from_size_align(40_000, 4096).unwrap(),
        Layout::from_size_align(2 * 1024 * 1024, 64).unwrap(),
        Layout::from_size_align(80_000, 128 * 1024).unwrap(),
    ] {
        let pointer = thin(allocator.allocate(layout).unwrap());
        assert_eq!(pointer.as_ptr() as usize % layout.align(), 0);
        // Touch both ends without making the test unnecessarily expensive.
        unsafe {
            pointer.as_ptr().write(0x11);
            pointer.as_ptr().add(layout.size() - 1).write(0x22);
            allocator.deallocate(pointer, layout);
        }
    }
    allocator.validate().unwrap();
    let trimmed = allocator.trim(TrimMode::Aggressive);
    assert!(trimmed.bytes_released > 0);
    assert_eq!(allocator.stats().reserved_bytes, 0);
    allocator.validate().unwrap();
}

#[test]
fn empty_dedicated_commit_budget_discards_and_reuses_reservation() {
    let allocator = BoAllocator::new(Config {
        dedicated_commit_cache_bytes: 0,
        ..Config::default()
    })
    .unwrap();
    let layout = Layout::from_size_align(2 * 1024 * 1024, 64).unwrap();

    let first = thin(allocator.allocate(layout).unwrap());
    let live = allocator.stats();
    assert!(live.committed_bytes >= layout.size());
    assert!(live.reserved_bytes >= live.committed_bytes);
    // SAFETY: this is the matching live dedicated allocation.
    unsafe { allocator.deallocate(first, layout) };

    let empty = allocator.stats();
    assert_eq!(empty.requested_live_bytes, 0);
    assert_eq!(empty.empty_dedicated_committed_bytes, 0);
    assert_eq!(empty.committed_bytes, live.committed_bytes);
    assert_eq!(empty.discarded_bytes, live.committed_bytes);
    assert_eq!(empty.reserved_bytes, live.reserved_bytes);
    assert_eq!(empty.discards, live.discards + 1);

    let second = thin(allocator.allocate(layout).unwrap());
    assert_eq!(
        second, first,
        "the retained reservation should be recommitted"
    );
    // SAFETY: this is the matching live recommitted dedicated allocation.
    unsafe { allocator.deallocate(second, layout) };
    allocator.validate().unwrap();

    let trimmed = allocator.trim(TrimMode::Aggressive);
    assert!(trimmed.bytes_released >= layout.size());
    assert_eq!(allocator.stats().reserved_bytes, 0);
}

#[test]
fn dedicated_reuse_selects_the_smallest_compatible_reservation() {
    let allocator = BoAllocator::new(Config {
        dedicated_commit_cache_bytes: usize::MAX,
        ..Config::default()
    })
    .unwrap();
    let small = Layout::from_size_align(2 * 1024 * 1024, 64).unwrap();
    let medium = Layout::from_size_align(4 * 1024 * 1024, 64).unwrap();
    let large = Layout::from_size_align(6 * 1024 * 1024, 64).unwrap();
    let request = Layout::from_size_align(3 * 1024 * 1024, 64).unwrap();
    let small_pointer = thin(allocator.allocate(small).unwrap());
    let medium_pointer = thin(allocator.allocate(medium).unwrap());
    let large_pointer = thin(allocator.allocate(large).unwrap());
    // SAFETY: all three allocations are live and use their original layouts.
    unsafe {
        allocator.deallocate(small_pointer, small);
        allocator.deallocate(medium_pointer, medium);
        allocator.deallocate(large_pointer, large);
    }

    let reused = thin(allocator.allocate(request).unwrap());
    assert_eq!(reused, medium_pointer);
    // SAFETY: reused is the sole live allocation for request.
    unsafe { allocator.deallocate(reused, request) };
    allocator.validate().unwrap();
}

#[test]
fn dedicated_commit_budget_retains_a_bounded_empty_working_set() {
    let layout = Layout::from_size_align(2 * 1024 * 1024, 64).unwrap();
    let allocator = BoAllocator::new(Config {
        dedicated_commit_cache_bytes: 3 * 1024 * 1024,
        ..Config::default()
    })
    .unwrap();
    let first = thin(allocator.allocate(layout).unwrap());
    let second = thin(allocator.allocate(layout).unwrap());
    // SAFETY: both pointers are distinct live allocations with this layout.
    unsafe {
        allocator.deallocate(first, layout);
        allocator.deallocate(second, layout);
    }
    let stats = allocator.stats();
    assert!(stats.empty_dedicated_committed_bytes <= 3 * 1024 * 1024);
    assert_eq!(
        stats.discarded_bytes + stats.empty_dedicated_committed_bytes,
        stats.committed_bytes
    );
    assert!(stats.discards >= 1);
    allocator.validate().unwrap();
}

#[test]
fn shared_run_territory_allocates_and_frees_across_threads() {
    const THREADS: usize = 8;
    const PER_THREAD: usize = 8;
    let allocator = BoAllocator::new(Config::default()).unwrap();
    let barrier = Arc::new(Barrier::new(THREADS));
    let layout = Layout::from_size_align(64 * 1024, 4096).unwrap();
    let mut producers = Vec::new();
    for _ in 0..THREADS {
        let allocator = allocator.clone();
        let barrier = Arc::clone(&barrier);
        producers.push(thread::spawn(move || {
            barrier.wait();
            (0..PER_THREAD)
                .map(|_| {
                    let pointer = thin(allocator.allocate(layout).unwrap());
                    // SAFETY: both touched bytes belong to this live allocation.
                    unsafe {
                        pointer.as_ptr().write(0x31);
                        pointer.as_ptr().add(layout.size() - 1).write(0x79);
                    }
                    pointer.as_ptr() as usize
                })
                .collect::<Vec<_>>()
        }));
    }
    let mut addresses = producers
        .into_iter()
        .flat_map(|producer| producer.join().unwrap())
        .collect::<Vec<_>>();
    addresses.sort_unstable();
    addresses.dedup();
    assert_eq!(addresses.len(), THREADS * PER_THREAD);
    assert_eq!(
        allocator.stats().requested_live_bytes,
        THREADS * PER_THREAD * layout.size()
    );

    let mut consumers = Vec::new();
    for chunk in addresses.chunks(PER_THREAD) {
        let allocator = allocator.clone();
        let addresses = chunk.to_vec();
        consumers.push(thread::spawn(move || {
            for address in addresses.into_iter().rev() {
                // SAFETY: every unique live address is transferred to exactly
                // one consumer and released with its original layout.
                unsafe {
                    allocator.deallocate(NonNull::new(address as *mut u8).unwrap(), layout);
                }
            }
        }));
    }
    for consumer in consumers {
        consumer.join().unwrap();
    }
    assert_eq!(allocator.stats().requested_live_bytes, 0);
    allocator.validate().unwrap();
    allocator.trim(TrimMode::Aggressive);
    assert_eq!(allocator.stats().reserved_bytes, 0);
    allocator.validate().unwrap();
}

#[test]
fn cross_thread_free_and_hot_contention_are_safe() {
    const THREADS: usize = 8;
    const PER_THREAD: usize = 1000;
    let allocator = BoAllocator::new(Config::default()).unwrap();
    let barrier = Arc::new(Barrier::new(THREADS));
    let mut workers = Vec::new();
    for thread_index in 0..THREADS {
        let allocator = allocator.clone();
        let barrier = Arc::clone(&barrier);
        workers.push(thread::spawn(move || {
            let layout = Layout::from_size_align(48 + (thread_index % 2) * 16, 16).unwrap();
            barrier.wait();
            let mut allocations = Vec::with_capacity(PER_THREAD);
            for _ in 0..PER_THREAD {
                allocations.push(thin(allocator.allocate(layout).unwrap()));
            }
            for pointer in allocations.into_iter().rev() {
                // SAFETY: every allocation is released exactly once with its matching layout.
                unsafe { allocator.deallocate(pointer, layout) };
            }
        }));
    }
    for worker in workers {
        worker.join().unwrap();
    }
    allocator.validate().unwrap();
    assert_eq!(allocator.stats().requested_live_bytes, 0);

    let (send, receive) = mpsc::channel();
    let producer = allocator.clone();
    let producer_thread = thread::spawn(move || {
        let layout = Layout::from_size_align(96, 32).unwrap();
        for _ in 0..2000 {
            send.send(thin(producer.allocate(layout).unwrap()).as_ptr() as usize)
                .unwrap();
        }
    });
    let consumer = allocator.clone();
    let consumer_thread = thread::spawn(move || {
        let layout = Layout::from_size_align(96, 32).unwrap();
        for address in receive {
            let pointer = NonNull::new(address as *mut u8).unwrap();
            // SAFETY: producer transfers unique ownership of each matching allocation.
            unsafe { consumer.deallocate(pointer, layout) };
        }
    });
    producer_thread.join().unwrap();
    consumer_thread.join().unwrap();
    allocator.validate().unwrap();
    assert_eq!(allocator.stats().requested_live_bytes, 0);
}

#[test]
fn low_capacity_page_queue_revalidates_after_reserved_red_transfer() {
    const BURST: usize = 64;
    for size in [24 * 1024, 32 * 1024] {
        let allocator = Arc::new(
            BoAllocator::new(Config {
                local_magazine_capacity: 0,
                recent_spans_per_class: 1,
                red_cold_slow_paths: 1,
                random_seed: Some(7),
                ..Config::default()
            })
            .unwrap(),
        );
        let layout = Layout::from_size_align(size, 4096).unwrap();
        let ready = Arc::new(Barrier::new(2));
        let transferred = Arc::new(Barrier::new(2));
        let owner = {
            let allocator = Arc::clone(&allocator);
            let ready = Arc::clone(&ready);
            let transferred = Arc::clone(&transferred);
            thread::spawn(move || {
                // Activate one Black span and keep it live. The remainder of
                // the freshly carved territory stays Green/ReservedSmall and
                // is the only legal target for the other owner to turn Red.
                let live = thin(allocator.allocate(layout).unwrap());
                ready.wait();
                transferred.wait();
                // SAFETY: this is the matching live allocation.
                unsafe { allocator.deallocate(live, layout) };

                // Re-enter through the low-capacity page queue after another
                // owner transferred one of its stale reserved handles.
                let mut unique = std::collections::HashSet::new();
                let pointers: Vec<_> = (0..BURST)
                    .map(|_| {
                        let pointer = thin(allocator.allocate(layout).unwrap());
                        assert!(unique.insert(pointer.as_ptr() as usize));
                        pointer
                    })
                    .collect();
                for pointer in pointers.into_iter().rev() {
                    // SAFETY: each pointer is unique, live, and paired with layout.
                    unsafe { allocator.deallocate(pointer, layout) };
                }
            })
        };

        ready.wait();
        let pointer = thin(allocator.allocate(layout).unwrap());
        assert!(allocator.stats().steals > 0);
        // SAFETY: this is the matching live allocation.
        unsafe { allocator.deallocate(pointer, layout) };
        transferred.wait();
        owner.join().unwrap();

        allocator.validate().unwrap();
    }
}

#[test]
fn many_consumers_can_free_one_owners_spans_without_control_entry() {
    const CONSUMERS: usize = 8;
    const PER_CONSUMER: usize = 2_000;
    let layout = Layout::from_size_align(96, 32).unwrap();

    for remote_free_batching in [false, true] {
        let allocator = BoAllocator::new(Config {
            remote_free_batching,
            ..Config::default()
        })
        .unwrap();
        let start = Arc::new(Barrier::new(CONSUMERS + 1));
        let mut senders = Vec::with_capacity(CONSUMERS);
        let mut workers = Vec::with_capacity(CONSUMERS);
        for _ in 0..CONSUMERS {
            let (sender, receiver) = mpsc::channel::<usize>();
            senders.push(sender);
            let allocator = allocator.clone();
            let start = Arc::clone(&start);
            workers.push(thread::spawn(move || {
                start.wait();
                for _ in 0..PER_CONSUMER {
                    let address = receiver.recv().unwrap();
                    // SAFETY: the producing thread transfers each unique allocation once.
                    unsafe {
                        allocator.deallocate(NonNull::new(address as *mut u8).unwrap(), layout)
                    };
                }
            }));
        }
        for index in 0..(CONSUMERS * PER_CONSUMER) {
            let allocation = allocator.allocate(layout).unwrap();
            senders[index % CONSUMERS]
                .send(allocation.as_ptr() as *mut u8 as usize)
                .unwrap();
        }
        start.wait();
        for worker in workers {
            worker.join().unwrap();
        }
        assert_eq!(allocator.stats().requested_live_bytes, 0);
        allocator.validate().unwrap();
        allocator.trim(TrimMode::Aggressive);
        allocator.validate().unwrap();
    }
}

#[test]
fn aggressive_trim_invalidates_tls_cache_and_allocator_recovers() {
    let allocator = BoAllocator::new(Config::default()).unwrap();
    for layout in [
        Layout::from_size_align(128, 64).unwrap(),
        Layout::from_size_align(32 * 1024, 4096).unwrap(),
    ] {
        let pointer = thin(allocator.allocate(layout).unwrap());
        unsafe { allocator.deallocate(pointer, layout) };
        allocator.trim(TrimMode::Aggressive);
        let next = thin(allocator.allocate(layout).unwrap());
        assert_eq!(next.as_ptr() as usize % layout.align(), 0);
        unsafe { allocator.deallocate(next, layout) };
    }
    allocator.validate().unwrap();
}

#[test]
fn live_black_span_is_never_stolen() {
    let allocator = BoAllocator::new(Config::default()).unwrap();
    let layout = Layout::from_size_align(64, 16).unwrap();
    let first_owner = allocator.clone();
    let first =
        thread::spawn(move || thin(first_owner.allocate(layout).unwrap()).as_ptr() as usize)
            .join()
            .unwrap();

    let second_owner = allocator.clone();
    let second =
        thread::spawn(move || thin(second_owner.allocate(layout).unwrap()).as_ptr() as usize)
            .join()
            .unwrap();
    assert_ne!(first, second);
    assert_eq!(
        allocator.stats().steals,
        0,
        "an extent containing a live object is absolute Black territory"
    );
    unsafe {
        allocator.deallocate(NonNull::new(first as *mut u8).unwrap(), layout);
        allocator.deallocate(NonNull::new(second as *mut u8).unwrap(), layout);
    }
    allocator.validate().unwrap();
}

#[test]
fn published_empty_black_span_can_transfer_while_old_owner_remains_alive() {
    let allocator = BoAllocator::new(Config {
        local_magazine_capacity: 0,
        locality_reserve_spans: 1,
        red_cold_slow_paths: 0,
        ..Config::default()
    })
    .unwrap();
    let layout = Layout::from_size_align(64, 16).unwrap();
    let (ready_send, ready_receive) = mpsc::channel();
    let (resume_send, resume_receive) = mpsc::channel();
    let (old_owner_send, old_owner_receive) = mpsc::channel();
    let first_owner = allocator.clone();
    let owner = thread::spawn(move || {
        let pointer = thin(first_owner.allocate(layout).unwrap());
        let address = pointer.as_ptr() as usize;
        // SAFETY: this is the matching live allocation. With magazines disabled,
        // the release is fully published and leaves no active span lease.
        unsafe { first_owner.deallocate(pointer, layout) };
        ready_send.send(address).unwrap();
        resume_receive.recv().unwrap();
        let next = thin(first_owner.allocate(layout).unwrap());
        old_owner_send.send(next.as_ptr() as usize).unwrap();
        unsafe { first_owner.deallocate(next, layout) };
    });

    let first = ready_receive.recv().unwrap();
    let second = thin(allocator.allocate(layout).unwrap());
    let span_mask = !(64 * 1024 - 1);
    assert_eq!(
        first & span_mask,
        second.as_ptr() as usize & span_mask,
        "a fully published empty Black pool should be reclaimed on the slow path"
    );
    let after_reclaim = allocator.stats();
    assert_eq!(after_reclaim.empty_black_reclaims, 1);
    assert_eq!(after_reclaim.steals, 1);
    resume_send.send(()).unwrap();
    let old_owner_next = old_owner_receive.recv().unwrap();
    assert_ne!(
        old_owner_next & span_mask,
        second.as_ptr() as usize & span_mask,
        "the old owner's stale TLS hint must not enter a transferred live span"
    );
    // SAFETY: this is the matching live allocation.
    unsafe { allocator.deallocate(second, layout) };
    owner.join().unwrap();
    allocator.validate().unwrap();
}

#[test]
fn deterministic_mixed_trace_never_overlaps_live_allocations() {
    let allocator = BoAllocator::new(Config::default()).unwrap();
    let mut rng = 0x9e37_79b9_7f4a_7c15_u64;
    let mut live: Vec<(usize, Layout)> = Vec::new();
    for step in 0..5000 {
        rng ^= rng << 13;
        rng ^= rng >> 7;
        rng ^= rng << 17;
        if !live.is_empty() && rng % 3 == 0 {
            let index = rng as usize % live.len();
            let (address, layout) = live.swap_remove(index);
            unsafe {
                allocator.deallocate(NonNull::new(address as *mut u8).unwrap(), layout);
            }
        } else {
            let sizes = [1, 7, 24, 63, 511, 4097, 20_000, 40_000, 200_000];
            let size = sizes[rng as usize % sizes.len()];
            let align = 1_usize << ((rng >> 8) as usize % 10);
            let layout = Layout::from_size_align(size, align).unwrap();
            let address = thin(allocator.allocate(layout).unwrap()).as_ptr() as usize;
            let end = address + layout.size();
            assert!(live.iter().all(|(other, other_layout)| {
                end <= *other || other + other_layout.size() <= address
            }));
            unsafe { std::ptr::write_bytes(address as *mut u8, step as u8, layout.size()) };
            live.push((address, layout));
        }
        if step % 257 == 0 {
            allocator.trim(TrimMode::Excess);
            allocator.validate().unwrap();
        }
    }
    for (address, layout) in live {
        unsafe { allocator.deallocate(NonNull::new(address as *mut u8).unwrap(), layout) };
    }
    allocator.validate().unwrap();
    assert_eq!(allocator.stats().requested_live_bytes, 0);
}

#[test]
fn aggressive_trim_races_reserved_and_headerless_activation_safely() {
    const WORKERS: usize = 4;
    let allocator = BoAllocator::new(Config {
        recent_spans_per_class: 1,
        locality_reserve_spans: 5,
        ..Config::default()
    })
    .unwrap();
    let barrier = Arc::new(Barrier::new(WORKERS + 1));
    let mut workers = Vec::new();
    for worker_index in 0..WORKERS {
        let allocator = allocator.clone();
        let barrier = Arc::clone(&barrier);
        workers.push(thread::spawn(move || {
            // Exercise both an anchored span and the two-object, headerless
            // class whose descriptor is resolved through the local page queue.
            let size = if worker_index % 2 == 0 {
                4096
            } else {
                32 * 1024
            };
            let layout = Layout::from_size_align(size, 4096).unwrap();
            barrier.wait();
            for _ in 0..100 {
                let mut burst = Vec::with_capacity(32);
                for _ in 0..32 {
                    burst.push(thin(allocator.allocate(layout).unwrap()));
                }
                for pointer in burst.into_iter().rev() {
                    // SAFETY: each matching allocation is released exactly once.
                    unsafe { allocator.deallocate(pointer, layout) };
                }
            }
        }));
    }
    barrier.wait();
    for _ in 0..100 {
        allocator.trim(TrimMode::Aggressive);
        thread::yield_now();
    }
    for worker in workers {
        worker.join().unwrap();
    }
    allocator.trim(TrimMode::Aggressive);
    allocator.validate().unwrap();
    assert_eq!(allocator.stats().requested_live_bytes, 0);
}

#[test]
fn remote_frees_are_drained_as_one_bitmap_batch() {
    const BLOCKS_IN_96_BYTE_SPAN: usize = 640;
    let allocator = BoAllocator::new(Config {
        remote_free_batching: true,
        remote_cache_capacity: 0,
        ..Config::default()
    })
    .unwrap();
    let layout = Layout::from_size_align(96, 32).unwrap();
    let mut addresses = Vec::with_capacity(BLOCKS_IN_96_BYTE_SPAN);
    for _ in 0..BLOCKS_IN_96_BYTE_SPAN {
        addresses.push(thin(allocator.allocate(layout).unwrap()).as_ptr() as usize);
    }
    let consumer = allocator.clone();
    thread::spawn(move || {
        for address in addresses {
            // SAFETY: the producer transferred every unique matching allocation.
            unsafe {
                consumer.deallocate(NonNull::new(address as *mut u8).unwrap(), layout);
            }
        }
    })
    .join()
    .unwrap();

    let queued = allocator.stats();
    assert_eq!(queued.remote_batches_drained, 0);
    assert_eq!(queued.requested_live_bytes, 0);

    let recycled = thin(allocator.allocate(layout).unwrap());
    let drained = allocator.stats();
    assert_eq!(drained.remote_batches_drained, 1);
    assert_eq!(drained.remote_blocks_drained, BLOCKS_IN_96_BYTE_SPAN as u64);
    // SAFETY: this is the matching live recycled allocation.
    unsafe { allocator.deallocate(recycled, layout) };
    allocator.validate().unwrap();
    assert_eq!(allocator.stats().requested_live_bytes, 0);
}

#[test]
fn remote_cache_reuses_foreign_black_blocks_before_publication() {
    let allocator = BoAllocator::new(Config {
        remote_cache_capacity: 8,
        ..Config::default()
    })
    .unwrap();
    let layout = Layout::from_size_align(96, 32).unwrap();
    let original: Vec<_> = (0..4)
        .map(|_| thin(allocator.allocate(layout).unwrap()).as_ptr() as usize)
        .collect();
    let consumer = allocator.clone();
    let expected = original.clone();
    let reused = thread::spawn(move || {
        for address in original {
            // SAFETY: every unique live allocation is transferred once.
            unsafe { consumer.deallocate(NonNull::new(address as *mut u8).unwrap(), layout) };
        }
        let mut reused = Vec::with_capacity(4);
        for _ in 0..4 {
            reused.push(thin(consumer.allocate(layout).unwrap()).as_ptr() as usize);
        }
        for address in reused.iter().copied() {
            // SAFETY: every cached allocation is still live and uses the same layout.
            unsafe { consumer.deallocate(NonNull::new(address as *mut u8).unwrap(), layout) };
        }
        reused
    })
    .join()
    .unwrap();

    let mut expected = expected;
    let mut reused = reused;
    expected.sort_unstable();
    reused.sort_unstable();
    assert_eq!(reused, expected);
    assert_eq!(allocator.stats().requested_live_bytes, 0);
    allocator.validate().unwrap();
}

#[test]
fn thread_exit_flushes_owner_local_overflow_for_reuse() {
    const BURST: usize = 64;
    let allocator = BoAllocator::new(Config {
        local_magazine_capacity: 8,
        ..Config::default()
    })
    .unwrap();
    let layout = Layout::from_size_align(96, 32).unwrap();
    let worker_allocator = allocator.clone();
    let released_addresses = thread::spawn(move || {
        for count in [1, 2, 4, 8] {
            let pointers: Vec<_> = (0..count)
                .map(|_| thin(worker_allocator.allocate(layout).unwrap()))
                .collect();
            for pointer in pointers {
                // SAFETY: every pointer is unique, live, and paired with layout.
                unsafe { worker_allocator.deallocate(pointer, layout) };
            }
        }
        let pointers: Vec<_> = (0..BURST)
            .map(|_| thin(worker_allocator.allocate(layout).unwrap()))
            .collect();
        let addresses = pointers
            .iter()
            .map(|pointer| pointer.as_ptr() as usize)
            .collect::<std::collections::HashSet<_>>();
        for pointer in pointers {
            // SAFETY: every pointer is unique, live, and paired with layout.
            unsafe { worker_allocator.deallocate(pointer, layout) };
        }
        addresses
    })
    .join()
    .unwrap();

    assert_eq!(allocator.stats().requested_live_bytes, 0);
    let reused: Vec<_> = (0..BURST)
        .map(|_| thin(allocator.allocate(layout).unwrap()))
        .collect();
    let reused_addresses = reused
        .iter()
        .map(|pointer| pointer.as_ptr() as usize)
        .collect::<std::collections::HashSet<_>>();
    assert_eq!(reused_addresses.len(), BURST);
    assert!(
        !released_addresses.is_disjoint(&reused_addresses),
        "thread-exit flush should return owner-local blocks to the shared span"
    );
    for pointer in reused {
        // SAFETY: every pointer is unique, live, and paired with layout.
        unsafe { allocator.deallocate(pointer, layout) };
    }
    allocator.trim(TrimMode::Aggressive);
    allocator.validate().unwrap();
}