oxigdal-core 0.1.4

Core abstractions for OxiGDAL - Pure Rust GDAL reimplementation with zero-copy buffers and cloud-native support
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
//! Custom Memory Allocators for Geospatial Data
//!
//! This module provides specialized allocators optimized for geospatial workloads:
//! - Slab allocator for fixed-size blocks (tiles)
//! - Buddy allocator for variable-size blocks
//! - Thread-local allocation pools
//! - Allocation tracking and statistics
//! - Memory leak detection in debug mode

// Unsafe code is necessary for custom allocators
#![allow(unsafe_code)]
// Default impls use expect() for configuration errors - acceptable here
#![allow(clippy::expect_used)]
// Arc with custom allocator types that have unsafe Send+Sync impls
#![allow(clippy::arc_with_non_send_sync)]

use crate::error::{OxiGdalError, Result};
use parking_lot::{Mutex, RwLock};
use std::alloc::{Layout, alloc, dealloc};
use std::collections::{BTreeMap, HashMap};
use std::ptr::NonNull;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};

/// Minimum alignment for all allocations
pub const MIN_ALIGNMENT: usize = 16;

/// Maximum block size for buddy allocator (16MB)
pub const MAX_BLOCK_SIZE: usize = 16 * 1024 * 1024;

/// Slab sizes for common geospatial tile dimensions
pub const SLAB_SIZES: &[usize] = &[
    256,       // 16x16 bytes
    1024,      // 32x32 bytes
    4096,      // 64x64 bytes or 4KB page
    16384,     // 128x128 bytes
    65536,     // 256x256 bytes or 64KB
    262_144,   // 512x512 bytes or 256KB
    1_048_576, // 1024x1024 bytes or 1MB
    4_194_304, // 2048x2048 bytes or 4MB
];

/// Statistics for allocator performance tracking
#[derive(Debug, Default)]
pub struct AllocatorStats {
    /// Total number of allocations
    pub total_allocations: AtomicU64,
    /// Total number of deallocations
    pub total_deallocations: AtomicU64,
    /// Current bytes allocated
    pub bytes_allocated: AtomicUsize,
    /// Peak bytes allocated
    pub peak_bytes_allocated: AtomicUsize,
    /// Number of allocation failures
    pub allocation_failures: AtomicU64,
    /// Number of slab hits
    pub slab_hits: AtomicU64,
    /// Number of slab misses
    pub slab_misses: AtomicU64,
}

impl AllocatorStats {
    /// Create new statistics
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Record an allocation
    pub fn record_allocation(&self, size: usize) {
        self.total_allocations.fetch_add(1, Ordering::Relaxed);
        let new_allocated = self.bytes_allocated.fetch_add(size, Ordering::Relaxed) + size;

        // Update peak if necessary
        let mut peak = self.peak_bytes_allocated.load(Ordering::Relaxed);
        while new_allocated > peak {
            match self.peak_bytes_allocated.compare_exchange_weak(
                peak,
                new_allocated,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(x) => peak = x,
            }
        }
    }

    /// Record a deallocation
    pub fn record_deallocation(&self, size: usize) {
        self.total_deallocations.fetch_add(1, Ordering::Relaxed);
        self.bytes_allocated.fetch_sub(size, Ordering::Relaxed);
    }

    /// Record an allocation failure
    pub fn record_failure(&self) {
        self.allocation_failures.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a slab hit
    pub fn record_slab_hit(&self) {
        self.slab_hits.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a slab miss
    pub fn record_slab_miss(&self) {
        self.slab_misses.fetch_add(1, Ordering::Relaxed);
    }

    /// Get current allocation count
    pub fn current_allocations(&self) -> u64 {
        self.total_allocations.load(Ordering::Relaxed)
            - self.total_deallocations.load(Ordering::Relaxed)
    }

    /// Get slab hit rate
    pub fn slab_hit_rate(&self) -> f64 {
        let hits = self.slab_hits.load(Ordering::Relaxed);
        let misses = self.slab_misses.load(Ordering::Relaxed);
        let total = hits + misses;
        if total == 0 {
            0.0
        } else {
            hits as f64 / total as f64
        }
    }
}

/// A block in the slab allocator
#[allow(dead_code)]
struct SlabBlock {
    ptr: NonNull<u8>,
    size: usize,
}

impl Drop for SlabBlock {
    fn drop(&mut self) {
        // SAFETY: The layout matches the one used during allocation.
        // The pointer is valid and aligned, and we have exclusive ownership.
        unsafe {
            let layout = Layout::from_size_align_unchecked(self.size, MIN_ALIGNMENT);
            dealloc(self.ptr.as_ptr(), layout);
        }
    }
}

/// Slab allocator for fixed-size blocks
pub struct SlabAllocator {
    /// Available blocks for each size class
    free_lists: Arc<RwLock<HashMap<usize, Vec<NonNull<u8>>>>>,
    /// Statistics
    stats: Arc<AllocatorStats>,
    /// Block tracking for leak detection in debug mode
    #[cfg(debug_assertions)]
    allocated_blocks: Arc<Mutex<HashMap<usize, Vec<NonNull<u8>>>>>,
}

// SAFETY: SlabAllocator is Send because all NonNull pointers are protected
// by Arc<RwLock> or Arc<Mutex>, providing proper synchronization
unsafe impl Send for SlabAllocator {}

// SAFETY: SlabAllocator is Sync because all NonNull pointers are protected
// by Arc<RwLock> or Arc<Mutex>, providing proper synchronization
unsafe impl Sync for SlabAllocator {}

impl SlabAllocator {
    /// Create a new slab allocator
    #[must_use]
    pub fn new() -> Self {
        let mut free_lists = HashMap::new();
        for &size in SLAB_SIZES {
            free_lists.insert(size, Vec::new());
        }

        Self {
            free_lists: Arc::new(RwLock::new(free_lists)),
            stats: Arc::new(AllocatorStats::new()),
            #[cfg(debug_assertions)]
            allocated_blocks: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Allocate a block of the given size
    pub fn allocate(&self, size: usize) -> Result<NonNull<u8>> {
        // Find the appropriate slab size
        let slab_size = SLAB_SIZES
            .iter()
            .find(|&&s| s >= size)
            .copied()
            .ok_or_else(|| {
                self.stats.record_slab_miss();
                OxiGdalError::invalid_parameter(
                    "parameter",
                    format!(
                        "Size {} exceeds maximum slab size {}",
                        size,
                        SLAB_SIZES.last().copied().unwrap_or(0)
                    ),
                )
            })?;

        // Try to get a block from the free list
        {
            let mut free_lists = self.free_lists.write();
            if let Some(blocks) = free_lists.get_mut(&slab_size) {
                if let Some(ptr) = blocks.pop() {
                    self.stats.record_slab_hit();
                    self.stats.record_allocation(slab_size);

                    #[cfg(debug_assertions)]
                    {
                        let mut allocated = self.allocated_blocks.lock();
                        allocated.entry(slab_size).or_default().push(ptr);
                    }

                    return Ok(ptr);
                }
            }
        }

        // No free block available, allocate a new one
        self.stats.record_slab_miss();
        let layout = Layout::from_size_align(slab_size, MIN_ALIGNMENT)
            .map_err(|e| OxiGdalError::allocation_error(e.to_string()))?;

        // SAFETY: We've validated the layout and check for null after allocation.
        // NonNull::new_unchecked is safe because we've verified the pointer is non-null.
        let ptr = unsafe {
            let raw_ptr = alloc(layout);
            if raw_ptr.is_null() {
                self.stats.record_failure();
                return Err(OxiGdalError::allocation_error(
                    "Failed to allocate memory".to_string(),
                ));
            }
            NonNull::new_unchecked(raw_ptr)
        };

        self.stats.record_allocation(slab_size);

        #[cfg(debug_assertions)]
        {
            let mut allocated = self.allocated_blocks.lock();
            allocated.entry(slab_size).or_default().push(ptr);
        }

        Ok(ptr)
    }

    /// Deallocate a block
    pub fn deallocate(&self, ptr: NonNull<u8>, size: usize) -> Result<()> {
        // Find the appropriate slab size
        let slab_size = SLAB_SIZES
            .iter()
            .find(|&&s| s >= size)
            .copied()
            .ok_or_else(|| {
                OxiGdalError::invalid_parameter(
                    "parameter",
                    format!("Size {size} exceeds maximum slab size"),
                )
            })?;

        #[cfg(debug_assertions)]
        {
            let mut allocated = self.allocated_blocks.lock();
            if let Some(blocks) = allocated.get_mut(&slab_size) {
                if let Some(pos) = blocks.iter().position(|&p| p == ptr) {
                    blocks.swap_remove(pos);
                } else {
                    return Err(OxiGdalError::invalid_parameter(
                        "parameter",
                        "Block not found in allocated list".to_string(),
                    ));
                }
            } else {
                return Err(OxiGdalError::invalid_parameter(
                    "parameter",
                    "Invalid slab size for deallocation".to_string(),
                ));
            }
        }

        // Return the block to the free list
        let mut free_lists = self.free_lists.write();
        free_lists.entry(slab_size).or_default().push(ptr);

        self.stats.record_deallocation(slab_size);
        Ok(())
    }

    /// Get statistics
    #[must_use]
    pub fn stats(&self) -> Arc<AllocatorStats> {
        Arc::clone(&self.stats)
    }

    /// Check for memory leaks (debug mode only)
    #[cfg(debug_assertions)]
    pub fn check_leaks(&self) -> Result<()> {
        let allocated = self.allocated_blocks.lock();
        let mut total_leaks = 0;
        for (size, blocks) in allocated.iter() {
            if !blocks.is_empty() {
                eprintln!(
                    "Memory leak detected: {} blocks of size {} bytes",
                    blocks.len(),
                    size
                );
                total_leaks += blocks.len();
            }
        }

        if total_leaks > 0 {
            Err(OxiGdalError::invalid_state(format!(
                "Detected {total_leaks} memory leaks"
            )))
        } else {
            Ok(())
        }
    }
}

impl Default for SlabAllocator {
    fn default() -> Self {
        Self::new()
    }
}

/// Buddy allocator for variable-size blocks
pub struct BuddyAllocator {
    /// Free lists for each order (power of 2)
    free_lists: Arc<Mutex<BTreeMap<usize, Vec<NonNull<u8>>>>>,
    /// Minimum block size (power of 2)
    min_block_size: usize,
    /// Maximum block size (power of 2)
    max_block_size: usize,
    /// Statistics
    stats: Arc<AllocatorStats>,
}

// SAFETY: BuddyAllocator is Send because all NonNull pointers are protected
// by Arc<Mutex>, providing proper synchronization
unsafe impl Send for BuddyAllocator {}

// SAFETY: BuddyAllocator is Sync because all NonNull pointers are protected
// by Arc<Mutex>, providing proper synchronization
unsafe impl Sync for BuddyAllocator {}

impl BuddyAllocator {
    /// Create a new buddy allocator
    pub fn new(min_block_size: usize, max_block_size: usize) -> Result<Self> {
        if !min_block_size.is_power_of_two() || !max_block_size.is_power_of_two() {
            return Err(OxiGdalError::invalid_parameter(
                "parameter",
                "Block sizes must be powers of 2".to_string(),
            ));
        }

        if min_block_size > max_block_size {
            return Err(OxiGdalError::invalid_parameter(
                "parameter",
                "Min block size must be <= max block size".to_string(),
            ));
        }

        Ok(Self {
            free_lists: Arc::new(Mutex::new(BTreeMap::new())),
            min_block_size,
            max_block_size,
            stats: Arc::new(AllocatorStats::new()),
        })
    }

    /// Create with default sizes
    ///
    /// # Errors
    ///
    /// Returns an error if allocation fails
    pub fn with_defaults() -> Result<Self> {
        Self::new(MIN_ALIGNMENT, MAX_BLOCK_SIZE)
    }

    /// Round up to next power of 2
    fn next_power_of_two(&self, size: usize) -> usize {
        if size <= self.min_block_size {
            self.min_block_size
        } else {
            size.next_power_of_two().min(self.max_block_size)
        }
    }

    /// Allocate a block
    pub fn allocate(&self, size: usize) -> Result<NonNull<u8>> {
        let block_size = self.next_power_of_two(size);

        if block_size > self.max_block_size {
            self.stats.record_failure();
            return Err(OxiGdalError::allocation_error(format!(
                "Requested size {} exceeds maximum block size {}",
                size, self.max_block_size
            )));
        }

        // Try to find a free block
        let mut free_lists = self.free_lists.lock();

        // Look for a block of the exact size or larger
        let mut found_size = None;
        for (&list_size, blocks) in free_lists.range(block_size..) {
            if !blocks.is_empty() {
                found_size = Some(list_size);
                break;
            }
        }

        if let Some(found_size) = found_size {
            // Take a block from the free list
            let blocks = free_lists
                .get_mut(&found_size)
                .ok_or_else(|| OxiGdalError::invalid_state("Free list disappeared".to_string()))?;
            let ptr = blocks
                .pop()
                .ok_or_else(|| OxiGdalError::invalid_state("Block disappeared".to_string()))?;

            // Split the block if it's larger than needed
            let mut current_size = found_size;
            while current_size > block_size {
                current_size /= 2;
                if current_size >= block_size {
                    // Create buddy block
                    // SAFETY: The pointer arithmetic is within the allocated block bounds.
                    // current_size is guaranteed to be less than the original allocation size.
                    let buddy_ptr =
                        unsafe { NonNull::new_unchecked(ptr.as_ptr().add(current_size)) };
                    free_lists.entry(current_size).or_default().push(buddy_ptr);
                }
            }

            self.stats.record_allocation(block_size);
            Ok(ptr)
        } else {
            // No free block available, allocate a new one
            drop(free_lists);

            let layout = Layout::from_size_align(block_size, block_size)
                .map_err(|e| OxiGdalError::allocation_error(e.to_string()))?;

            // SAFETY: Layout is valid and we check for null before creating NonNull.
            // The pointer will be properly aligned according to the layout.
            let ptr = unsafe {
                let raw_ptr = alloc(layout);
                if raw_ptr.is_null() {
                    self.stats.record_failure();
                    return Err(OxiGdalError::allocation_error(
                        "Failed to allocate memory".to_string(),
                    ));
                }
                NonNull::new_unchecked(raw_ptr)
            };

            self.stats.record_allocation(block_size);
            Ok(ptr)
        }
    }

    /// Deallocate a block
    pub fn deallocate(&self, ptr: NonNull<u8>, size: usize) -> Result<()> {
        let block_size = self.next_power_of_two(size);

        let mut free_lists = self.free_lists.lock();
        free_lists.entry(block_size).or_default().push(ptr);

        self.stats.record_deallocation(block_size);
        Ok(())
    }

    /// Get statistics
    #[must_use]
    pub fn stats(&self) -> Arc<AllocatorStats> {
        Arc::clone(&self.stats)
    }
}

/// Thread-local allocator pool
pub struct ThreadLocalAllocator {
    /// Slab allocator for small fixed-size allocations
    slab: SlabAllocator,
    /// Buddy allocator for larger variable-size allocations
    buddy: BuddyAllocator,
    /// Threshold for using slab vs buddy
    slab_threshold: usize,
}

// SAFETY: ThreadLocalAllocator is Send because it contains SlabAllocator and
// BuddyAllocator which are both Send
unsafe impl Send for ThreadLocalAllocator {}

// SAFETY: ThreadLocalAllocator is Sync because it contains SlabAllocator and
// BuddyAllocator which are both Sync
unsafe impl Sync for ThreadLocalAllocator {}

impl ThreadLocalAllocator {
    /// Create a new thread-local allocator
    ///
    /// # Errors
    ///
    /// Returns an error if allocator initialization fails
    pub fn new() -> Result<Self> {
        Ok(Self {
            slab: SlabAllocator::new(),
            buddy: BuddyAllocator::with_defaults()?,
            slab_threshold: *SLAB_SIZES.last().ok_or_else(|| OxiGdalError::Internal {
                message: "SLAB_SIZES is empty".to_string(),
            })?,
        })
    }

    /// Allocate memory
    pub fn allocate(&self, size: usize, alignment: usize) -> Result<NonNull<u8>> {
        if alignment > MIN_ALIGNMENT && alignment > size {
            return Err(OxiGdalError::invalid_parameter(
                "parameter",
                "Alignment requirements not supported".to_string(),
            ));
        }

        if size <= self.slab_threshold {
            self.slab.allocate(size)
        } else {
            self.buddy.allocate(size)
        }
    }

    /// Deallocate memory
    pub fn deallocate(&self, ptr: NonNull<u8>, size: usize) -> Result<()> {
        if size <= self.slab_threshold {
            self.slab.deallocate(ptr, size)
        } else {
            self.buddy.deallocate(ptr, size)
        }
    }

    /// Get combined statistics
    #[must_use]
    pub fn stats(&self) -> (Arc<AllocatorStats>, Arc<AllocatorStats>) {
        (self.slab.stats(), self.buddy.stats())
    }
}

/// Generic allocator trait
pub trait Allocator: Send + Sync {
    /// Allocate memory
    fn allocate(&self, size: usize, alignment: usize) -> Result<NonNull<u8>>;

    /// Deallocate memory
    fn deallocate(&self, ptr: NonNull<u8>, size: usize) -> Result<()>;

    /// Get statistics
    fn stats(&self) -> Arc<AllocatorStats>;
}

impl Allocator for SlabAllocator {
    fn allocate(&self, size: usize, _alignment: usize) -> Result<NonNull<u8>> {
        self.allocate(size)
    }

    fn deallocate(&self, ptr: NonNull<u8>, size: usize) -> Result<()> {
        self.deallocate(ptr, size)
    }

    fn stats(&self) -> Arc<AllocatorStats> {
        self.stats()
    }
}

impl Allocator for BuddyAllocator {
    fn allocate(&self, size: usize, _alignment: usize) -> Result<NonNull<u8>> {
        self.allocate(size)
    }

    fn deallocate(&self, ptr: NonNull<u8>, size: usize) -> Result<()> {
        self.deallocate(ptr, size)
    }

    fn stats(&self) -> Arc<AllocatorStats> {
        self.stats()
    }
}

impl Allocator for ThreadLocalAllocator {
    fn allocate(&self, size: usize, alignment: usize) -> Result<NonNull<u8>> {
        self.allocate(size, alignment)
    }

    fn deallocate(&self, ptr: NonNull<u8>, size: usize) -> Result<()> {
        self.deallocate(ptr, size)
    }

    fn stats(&self) -> Arc<AllocatorStats> {
        // Return slab stats for now
        self.slab.stats()
    }
}

#[cfg(test)]
#[allow(useless_ptr_null_checks)]
mod tests {
    use super::*;

    #[test]
    fn test_slab_allocator() {
        let allocator = SlabAllocator::new();

        // Allocate a small block
        let ptr1 = allocator
            .allocate(100)
            .expect("Slab allocator should allocate 100 bytes");
        assert!(!ptr1.as_ptr().is_null());

        // Allocate another block
        let ptr2 = allocator
            .allocate(200)
            .expect("Slab allocator should allocate 200 bytes");
        assert!(!ptr2.as_ptr().is_null());
        assert_ne!(ptr1, ptr2);

        // Deallocate
        allocator
            .deallocate(ptr1, 100)
            .expect("Deallocation should succeed");
        allocator
            .deallocate(ptr2, 200)
            .expect("Deallocation should succeed");

        // Check stats
        let stats = allocator.stats();
        assert_eq!(stats.total_allocations.load(Ordering::Relaxed), 2);
        assert_eq!(stats.total_deallocations.load(Ordering::Relaxed), 2);
    }

    #[test]
    fn test_buddy_allocator() {
        let allocator =
            BuddyAllocator::with_defaults().expect("Default buddy allocator should be created");

        // Allocate blocks
        let ptr1 = allocator
            .allocate(1000)
            .expect("Buddy allocator should allocate 1000 bytes");
        let ptr2 = allocator
            .allocate(2000)
            .expect("Buddy allocator should allocate 2000 bytes");

        assert!(!ptr1.as_ptr().is_null());
        assert!(!ptr2.as_ptr().is_null());

        // Deallocate
        allocator
            .deallocate(ptr1, 1000)
            .expect("Buddy deallocation should succeed");
        allocator
            .deallocate(ptr2, 2000)
            .expect("Buddy deallocation should succeed");
    }

    #[test]
    fn test_thread_local_allocator() {
        let allocator =
            ThreadLocalAllocator::new().expect("Thread-local allocator should be created");

        // Test small allocation (slab)
        let ptr1 = allocator
            .allocate(256, MIN_ALIGNMENT)
            .expect("Thread-local allocator should allocate small block");
        assert!(!ptr1.as_ptr().is_null());

        // Test large allocation (buddy)
        let ptr2 = allocator
            .allocate(1024 * 1024, MIN_ALIGNMENT)
            .expect("Thread-local allocator should allocate large block");
        assert!(!ptr2.as_ptr().is_null());

        // Deallocate
        allocator
            .deallocate(ptr1, 256)
            .expect("Thread-local deallocation should succeed");
        allocator
            .deallocate(ptr2, 1024 * 1024)
            .expect("Thread-local deallocation should succeed");
    }

    #[test]
    fn test_allocator_stats() {
        let stats = AllocatorStats::new();

        stats.record_allocation(1024);
        stats.record_allocation(2048);

        assert_eq!(stats.total_allocations.load(Ordering::Relaxed), 2);
        assert_eq!(stats.bytes_allocated.load(Ordering::Relaxed), 3072);
        assert_eq!(stats.peak_bytes_allocated.load(Ordering::Relaxed), 3072);

        stats.record_deallocation(1024);
        assert_eq!(stats.bytes_allocated.load(Ordering::Relaxed), 2048);
        assert_eq!(stats.current_allocations(), 1);
    }
}