zk-nalloc 0.2.6

High-performance, deterministic memory allocator optimized for Zero-Knowledge Proof (ZKP) systems and cryptographic provers.
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
//! Platform-specific memory allocation interface.
//!
//! This module provides an abstraction over the operating system's
//! virtual memory allocation APIs:
//! - **Linux**: `mmap` via `rustix` with huge pages and guard pages support
//! - **macOS**: `mach_vm_allocate` via `mach2`
//! - **Windows**: `VirtualAlloc` via direct FFI
//! - **Other Unix**: `mmap` via `libc`

use std::fmt;

/// Error type for system memory allocation failures.
#[derive(Debug, Clone, Copy)]
pub struct AllocFailed {
    /// The size that was requested.
    pub requested_size: usize,
    /// Platform-specific error code, if available.
    pub error_code: Option<i32>,
    /// Error kind for better diagnostics.
    pub kind: AllocErrorKind,
}

/// Classification of allocation errors.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AllocErrorKind {
    /// Out of memory
    OutOfMemory,
    /// Invalid size or alignment
    InvalidArgument,
    /// Permission denied
    PermissionDenied,
    /// Huge pages not available
    HugePagesUnavailable,
    /// Memory lock failed
    MlockFailed,
    /// Unknown error
    Unknown,
}

impl std::error::Error for AllocFailed {}

impl fmt::Display for AllocFailed {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.error_code {
            Some(code) => write!(
                f,
                "Memory allocation failed ({:?}): requested {} bytes, error code {}",
                self.kind, self.requested_size, code
            ),
            None => write!(
                f,
                "Memory allocation failed ({:?}): requested {} bytes",
                self.kind, self.requested_size
            ),
        }
    }
}

impl AllocFailed {
    /// Create a new allocation failure error.
    pub fn new(size: usize) -> Self {
        Self {
            requested_size: size,
            error_code: None,
            kind: AllocErrorKind::Unknown,
        }
    }

    pub fn with_code(size: usize, code: i32) -> Self {
        Self {
            requested_size: size,
            error_code: Some(code),
            kind: AllocErrorKind::Unknown,
        }
    }

    pub fn with_kind(size: usize, kind: AllocErrorKind) -> Self {
        Self {
            requested_size: size,
            error_code: None,
            kind,
        }
    }

    pub fn out_of_memory(size: usize) -> Self {
        Self::with_kind(size, AllocErrorKind::OutOfMemory)
    }

    pub fn huge_pages_unavailable(size: usize) -> Self {
        Self::with_kind(size, AllocErrorKind::HugePagesUnavailable)
    }

    pub fn mlock_failed(size: usize) -> Self {
        Self::with_kind(size, AllocErrorKind::MlockFailed)
    }
}

/// Huge page size options (Linux only).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HugePageSize {
    /// 2 MB huge pages (most common)
    Size2MB,
    /// 1 GB huge pages (requires explicit kernel configuration)
    Size1GB,
}

impl HugePageSize {
    pub fn bytes(&self) -> usize {
        match self {
            HugePageSize::Size2MB => 2 * 1024 * 1024,
            HugePageSize::Size1GB => 1024 * 1024 * 1024,
        }
    }
}

/// Result of a guarded allocation.
#[cfg(feature = "guard-pages")]
pub struct GuardedAlloc {
    /// The usable memory pointer (after the front guard page).
    pub ptr: *mut u8,
    /// Total allocation size including guard pages.
    pub total_size: usize,
    /// Usable size (excluding guard pages).
    pub usable_size: usize,
    /// Base pointer (start of front guard page).
    pub base_ptr: *mut u8,
}

/// Platform-specific memory allocation functions.
pub mod sys {
    use super::*;

    // ========================================================================
    // Linux Implementation (using rustix)
    // ========================================================================

    /// Allocate `size` bytes of virtual memory from the OS.
    #[cfg(target_os = "linux")]
    #[inline]
    pub fn alloc(size: usize) -> Result<*mut u8, AllocFailed> {
        use rustix::mm::{mmap_anonymous, MapFlags, ProtFlags};
        use std::ptr;

        debug_assert!(size > 0);

        unsafe {
            match mmap_anonymous(
                ptr::null_mut(),
                size,
                ProtFlags::READ | ProtFlags::WRITE,
                MapFlags::PRIVATE | MapFlags::NORESERVE,
            ) {
                Ok(ptr) => {
                    // Issue #8: Explicit null check for safety
                    let ptr = ptr as *mut u8;
                    if ptr.is_null() {
                        return Err(AllocFailed::out_of_memory(size));
                    }
                    Ok(ptr)
                }
                Err(_) => Err(AllocFailed::out_of_memory(size)),
            }
        }
    }

    /// Allocate memory with huge pages (Linux only).
    #[cfg(all(target_os = "linux", feature = "huge-pages"))]
    pub fn alloc_huge(size: usize, huge_size: HugePageSize) -> Result<*mut u8, AllocFailed> {
        use rustix::mm::{mmap_anonymous, MapFlags, ProtFlags};
        use std::ptr;

        debug_assert!(size > 0);

        // Size must be aligned to huge page size
        let page_size = huge_size.bytes();
        let aligned_size = (size + page_size - 1) & !(page_size - 1);

        // MAP_HUGETLB flags
        let huge_flag = match huge_size {
            HugePageSize::Size2MB => 21 << 26, // MAP_HUGE_2MB
            HugePageSize::Size1GB => 30 << 26, // MAP_HUGE_1GB
        };

        unsafe {
            // First try with huge pages
            let flags = MapFlags::PRIVATE | MapFlags::from_bits_retain(0x40000 | huge_flag); // MAP_HUGETLB

            match mmap_anonymous(
                ptr::null_mut(),
                aligned_size,
                ProtFlags::READ | ProtFlags::WRITE,
                flags,
            ) {
                Ok(ptr) => Ok(ptr as *mut u8),
                Err(_) => Err(AllocFailed::huge_pages_unavailable(size)),
            }
        }
    }

    /// Allocate memory with guard pages at both ends.
    #[cfg(all(target_os = "linux", feature = "guard-pages"))]
    pub fn alloc_with_guards(size: usize) -> Result<GuardedAlloc, AllocFailed> {
        use rustix::mm::{mmap_anonymous, mprotect, MapFlags, MprotectFlags, ProtFlags};
        use std::ptr;

        const PAGE_SIZE: usize = 4096;

        // Allocate: guard_page + usable_memory + guard_page
        let total_size = PAGE_SIZE + size + PAGE_SIZE;

        unsafe {
            let base = match mmap_anonymous(
                ptr::null_mut(),
                total_size,
                ProtFlags::READ | ProtFlags::WRITE,
                MapFlags::PRIVATE,
            ) {
                Ok(ptr) => ptr as *mut u8,
                Err(_) => return Err(AllocFailed::out_of_memory(size)),
            };

            // Protect front guard page (no access)
            if mprotect(base as *mut _, PAGE_SIZE, MprotectFlags::empty()).is_err() {
                let _ = dealloc(base, total_size);
                return Err(AllocFailed::with_kind(
                    size,
                    AllocErrorKind::PermissionDenied,
                ));
            }

            // Protect rear guard page (no access)
            let rear_guard = base.add(PAGE_SIZE + size);
            if mprotect(rear_guard as *mut _, PAGE_SIZE, MprotectFlags::empty()).is_err() {
                let _ = dealloc(base, total_size);
                return Err(AllocFailed::with_kind(
                    size,
                    AllocErrorKind::PermissionDenied,
                ));
            }

            Ok(GuardedAlloc {
                ptr: base.add(PAGE_SIZE),
                total_size,
                usable_size: size,
                base_ptr: base,
            })
        }
    }

    /// Lock memory to prevent swapping (for sensitive data).
    #[cfg(all(target_os = "linux", feature = "mlock"))]
    pub fn mlock(ptr: *mut u8, size: usize) -> Result<(), AllocFailed> {
        unsafe {
            if libc::mlock(ptr as *const _, size) == 0 {
                Ok(())
            } else {
                Err(AllocFailed::mlock_failed(size))
            }
        }
    }

    /// Unlock previously locked memory.
    #[cfg(all(target_os = "linux", feature = "mlock"))]
    pub fn munlock(ptr: *mut u8, size: usize) -> Result<(), AllocFailed> {
        unsafe {
            if libc::munlock(ptr as *const _, size) == 0 {
                Ok(())
            } else {
                Err(AllocFailed::mlock_failed(size))
            }
        }
    }

    /// Deallocate memory previously allocated with `alloc`.
    #[cfg(target_os = "linux")]
    #[inline]
    pub fn dealloc(ptr: *mut u8, size: usize) -> Result<(), AllocFailed> {
        use rustix::mm::munmap;

        if ptr.is_null() {
            return Ok(());
        }

        unsafe {
            match munmap(ptr as *mut _, size) {
                Ok(()) => Ok(()),
                Err(_) => Err(AllocFailed::new(size)),
            }
        }
    }

    // ========================================================================
    // macOS Implementation (using mach2)
    // ========================================================================

    #[cfg(target_vendor = "apple")]
    #[inline]
    pub fn alloc(size: usize) -> Result<*mut u8, AllocFailed> {
        use mach2::kern_return::KERN_SUCCESS;
        use mach2::traps::mach_task_self;
        use mach2::vm::mach_vm_allocate;
        use mach2::vm_statistics::VM_FLAGS_ANYWHERE;
        use mach2::vm_types::{mach_vm_address_t, mach_vm_size_t};

        debug_assert!(size > 0);

        let task = unsafe { mach_task_self() };
        let mut address: mach_vm_address_t = 0;
        let vm_size: mach_vm_size_t = size as mach_vm_size_t;

        let retval = unsafe { mach_vm_allocate(task, &mut address, vm_size, VM_FLAGS_ANYWHERE) };

        if retval == KERN_SUCCESS {
            // Issue #8: Explicit null check for safety
            let ptr = address as *mut u8;
            if ptr.is_null() {
                return Err(AllocFailed::out_of_memory(size));
            }
            Ok(ptr)
        } else {
            Err(AllocFailed::with_code(size, retval))
        }
    }

    /// Lock memory on macOS.
    #[cfg(all(target_vendor = "apple", feature = "mlock"))]
    pub fn mlock(ptr: *mut u8, size: usize) -> Result<(), AllocFailed> {
        unsafe {
            if libc::mlock(ptr as *const _, size) == 0 {
                Ok(())
            } else {
                Err(AllocFailed::mlock_failed(size))
            }
        }
    }

    #[cfg(all(target_vendor = "apple", feature = "mlock"))]
    pub fn munlock(ptr: *mut u8, size: usize) -> Result<(), AllocFailed> {
        unsafe {
            if libc::munlock(ptr as *const _, size) == 0 {
                Ok(())
            } else {
                Err(AllocFailed::mlock_failed(size))
            }
        }
    }

    /// Allocate with guard pages on macOS.
    #[cfg(all(target_vendor = "apple", feature = "guard-pages"))]
    pub fn alloc_with_guards(size: usize) -> Result<GuardedAlloc, AllocFailed> {
        use mach2::kern_return::KERN_SUCCESS;
        use mach2::traps::mach_task_self;
        use mach2::vm::{mach_vm_allocate, mach_vm_protect};
        use mach2::vm_prot::VM_PROT_NONE;
        use mach2::vm_statistics::VM_FLAGS_ANYWHERE;
        use mach2::vm_types::{mach_vm_address_t, mach_vm_size_t};

        const PAGE_SIZE: usize = 4096;
        let total_size = PAGE_SIZE + size + PAGE_SIZE;

        let task = unsafe { mach_task_self() };
        let mut address: mach_vm_address_t = 0;

        let retval = unsafe {
            mach_vm_allocate(
                task,
                &mut address,
                total_size as mach_vm_size_t,
                VM_FLAGS_ANYWHERE,
            )
        };

        if retval != KERN_SUCCESS {
            return Err(AllocFailed::out_of_memory(size));
        }

        let base = address as *mut u8;

        unsafe {
            // Protect front guard
            let ret = mach_vm_protect(task, address, PAGE_SIZE as mach_vm_size_t, 0, VM_PROT_NONE);
            if ret != KERN_SUCCESS {
                let _ = dealloc(base, total_size);
                return Err(AllocFailed::with_kind(
                    size,
                    AllocErrorKind::PermissionDenied,
                ));
            }

            // Protect rear guard
            let rear_addr = address + (PAGE_SIZE + size) as u64;
            let ret = mach_vm_protect(
                task,
                rear_addr,
                PAGE_SIZE as mach_vm_size_t,
                0,
                VM_PROT_NONE,
            );
            if ret != KERN_SUCCESS {
                let _ = dealloc(base, total_size);
                return Err(AllocFailed::with_kind(
                    size,
                    AllocErrorKind::PermissionDenied,
                ));
            }
        }

        Ok(GuardedAlloc {
            ptr: unsafe { base.add(PAGE_SIZE) },
            total_size,
            usable_size: size,
            base_ptr: base,
        })
    }

    /// Deallocate memory previously allocated with `alloc`.
    #[cfg(target_vendor = "apple")]
    #[inline]
    pub fn dealloc(ptr: *mut u8, size: usize) -> Result<(), AllocFailed> {
        use mach2::kern_return::KERN_SUCCESS;
        use mach2::traps::mach_task_self;
        use mach2::vm::mach_vm_deallocate;
        use mach2::vm_types::mach_vm_size_t;

        if ptr.is_null() {
            return Ok(());
        }

        let task = unsafe { mach_task_self() };
        let retval = unsafe { mach_vm_deallocate(task, ptr as u64, size as mach_vm_size_t) };

        if retval == KERN_SUCCESS {
            Ok(())
        } else {
            Err(AllocFailed::with_code(size, retval))
        }
    }

    // ========================================================================
    // Windows Implementation
    // ========================================================================

    #[cfg(target_os = "windows")]
    #[inline]
    pub fn alloc(size: usize) -> Result<*mut u8, AllocFailed> {
        use std::ptr;

        const MEM_COMMIT: u32 = 0x00001000;
        const MEM_RESERVE: u32 = 0x00002000;
        const PAGE_READWRITE: u32 = 0x04;

        extern "system" {
            fn VirtualAlloc(
                lpAddress: *mut u8,
                dwSize: usize,
                flAllocationType: u32,
                flProtect: u32,
            ) -> *mut u8;
        }

        debug_assert!(size > 0);

        let result = unsafe {
            VirtualAlloc(
                ptr::null_mut(),
                size,
                MEM_COMMIT | MEM_RESERVE,
                PAGE_READWRITE,
            )
        };

        if result.is_null() {
            Err(AllocFailed::out_of_memory(size))
        } else {
            Ok(result)
        }
    }

    /// Allocate with guard pages on Windows.
    #[cfg(all(target_os = "windows", feature = "guard-pages"))]
    pub fn alloc_with_guards(size: usize) -> Result<GuardedAlloc, AllocFailed> {
        use std::ptr;

        const MEM_COMMIT: u32 = 0x00001000;
        const MEM_RESERVE: u32 = 0x00002000;
        const PAGE_READWRITE: u32 = 0x04;
        const PAGE_NOACCESS: u32 = 0x01;
        const PAGE_SIZE: usize = 4096;

        extern "system" {
            fn VirtualAlloc(
                lpAddress: *mut u8,
                dwSize: usize,
                flAllocationType: u32,
                flProtect: u32,
            ) -> *mut u8;
            fn VirtualProtect(
                lpAddress: *mut u8,
                dwSize: usize,
                flNewProtect: u32,
                lpflOldProtect: *mut u32,
            ) -> i32;
        }

        let total_size = PAGE_SIZE + size + PAGE_SIZE;

        let base = unsafe {
            VirtualAlloc(
                ptr::null_mut(),
                total_size,
                MEM_COMMIT | MEM_RESERVE,
                PAGE_READWRITE,
            )
        };

        if base.is_null() {
            return Err(AllocFailed::out_of_memory(size));
        }

        unsafe {
            let mut old_protect: u32 = 0;

            // Protect front guard
            if VirtualProtect(base, PAGE_SIZE, PAGE_NOACCESS, &mut old_protect) == 0 {
                let _ = dealloc(base, total_size);
                return Err(AllocFailed::with_kind(
                    size,
                    AllocErrorKind::PermissionDenied,
                ));
            }

            // Protect rear guard
            let rear_guard = base.add(PAGE_SIZE + size);
            if VirtualProtect(rear_guard, PAGE_SIZE, PAGE_NOACCESS, &mut old_protect) == 0 {
                let _ = dealloc(base, total_size);
                return Err(AllocFailed::with_kind(
                    size,
                    AllocErrorKind::PermissionDenied,
                ));
            }
        }

        Ok(GuardedAlloc {
            ptr: unsafe { base.add(PAGE_SIZE) },
            total_size,
            usable_size: size,
            base_ptr: base,
        })
    }

    /// Lock memory on Windows.
    #[cfg(all(target_os = "windows", feature = "mlock"))]
    pub fn mlock(ptr: *mut u8, size: usize) -> Result<(), AllocFailed> {
        extern "system" {
            fn VirtualLock(lpAddress: *mut u8, dwSize: usize) -> i32;
        }

        unsafe {
            if VirtualLock(ptr, size) != 0 {
                Ok(())
            } else {
                Err(AllocFailed::mlock_failed(size))
            }
        }
    }

    #[cfg(all(target_os = "windows", feature = "mlock"))]
    pub fn munlock(ptr: *mut u8, size: usize) -> Result<(), AllocFailed> {
        extern "system" {
            fn VirtualUnlock(lpAddress: *mut u8, dwSize: usize) -> i32;
        }

        unsafe {
            if VirtualUnlock(ptr, size) != 0 {
                Ok(())
            } else {
                Err(AllocFailed::mlock_failed(size))
            }
        }
    }

    /// Deallocate memory previously allocated with `alloc`.
    #[cfg(target_os = "windows")]
    #[inline]
    pub fn dealloc(ptr: *mut u8, _size: usize) -> Result<(), AllocFailed> {
        const MEM_RELEASE: u32 = 0x00008000;

        extern "system" {
            fn VirtualFree(lpAddress: *mut u8, dwSize: usize, dwFreeType: u32) -> i32;
        }

        if ptr.is_null() {
            return Ok(());
        }

        // For MEM_RELEASE, dwSize must be 0
        let result = unsafe { VirtualFree(ptr, 0, MEM_RELEASE) };

        if result != 0 {
            Ok(())
        } else {
            Err(AllocFailed::new(0))
        }
    }

    // ========================================================================
    // Unix Fallback (using libc mmap)
    // ========================================================================

    /// Fallback for other Unix-like systems.
    #[cfg(all(
        not(target_os = "linux"),
        not(target_vendor = "apple"),
        not(target_os = "windows"),
        unix
    ))]
    #[inline]
    pub fn alloc(size: usize) -> Result<*mut u8, AllocFailed> {
        use libc::{mmap, MAP_ANON, MAP_FAILED, MAP_PRIVATE, PROT_READ, PROT_WRITE};
        use std::ptr;

        debug_assert!(size > 0);

        let result = unsafe {
            mmap(
                ptr::null_mut(),
                size,
                PROT_READ | PROT_WRITE,
                MAP_PRIVATE | MAP_ANON,
                -1,
                0,
            )
        };

        if result == MAP_FAILED {
            Err(AllocFailed::out_of_memory(size))
        } else {
            Ok(result as *mut u8)
        }
    }

    /// Deallocate memory previously allocated with `alloc`.
    #[cfg(all(
        not(target_os = "linux"),
        not(target_vendor = "apple"),
        not(target_os = "windows"),
        unix
    ))]
    #[inline]
    pub fn dealloc(ptr: *mut u8, size: usize) -> Result<(), AllocFailed> {
        use libc::munmap;

        if ptr.is_null() {
            return Ok(());
        }

        let result = unsafe { munmap(ptr as *mut _, size) };

        if result == 0 {
            Ok(())
        } else {
            Err(AllocFailed::new(size))
        }
    }

    // ========================================================================
    // Guard page deallocation helpers
    // ========================================================================

    /// Deallocate a guarded allocation.
    #[cfg(feature = "guard-pages")]
    pub fn dealloc_guarded(guarded: &GuardedAlloc) -> Result<(), AllocFailed> {
        dealloc(guarded.base_ptr, guarded.total_size)
    }
}

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

    #[test]
    fn test_alloc_dealloc_roundtrip() {
        let size = 4096;
        let ptr = sys::alloc(size).expect("allocation should succeed");

        assert!(!ptr.is_null());

        // Write to verify it's accessible
        unsafe {
            std::ptr::write_bytes(ptr, 0xAB, size);
        }

        sys::dealloc(ptr, size).expect("deallocation should succeed");
    }

    #[test]
    fn test_large_allocation() {
        let size = 64 * 1024 * 1024; // 64 MB
        let ptr = sys::alloc(size).expect("large allocation should succeed");

        assert!(!ptr.is_null());

        // Touch first and last pages
        unsafe {
            *ptr = 0x42;
            *ptr.add(size - 1) = 0x42;
        }

        sys::dealloc(ptr, size).expect("deallocation should succeed");
    }

    #[test]
    fn test_alloc_failed_display() {
        let err = AllocFailed::new(1024);
        let msg = format!("{}", err);
        assert!(msg.contains("1024"));
    }

    #[test]
    fn test_alloc_error_kinds() {
        let err = AllocFailed::out_of_memory(1024);
        assert_eq!(err.kind, AllocErrorKind::OutOfMemory);

        let err = AllocFailed::huge_pages_unavailable(1024);
        assert_eq!(err.kind, AllocErrorKind::HugePagesUnavailable);

        let err = AllocFailed::mlock_failed(1024);
        assert_eq!(err.kind, AllocErrorKind::MlockFailed);
    }

    #[test]
    #[cfg(all(target_os = "linux", feature = "guard-pages"))]
    fn test_guard_pages() {
        let size = 4096;
        let guarded = sys::alloc_with_guards(size).expect("guarded allocation should succeed");

        assert!(!guarded.ptr.is_null());
        assert_eq!(guarded.usable_size, size);
        assert!(guarded.total_size > size);

        // Write to usable area
        unsafe {
            std::ptr::write_bytes(guarded.ptr, 0xAB, size);
        }

        sys::dealloc_guarded(&guarded).expect("deallocation should succeed");
    }
}