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
mod caolang_alloc;
pub use caolang_alloc::*;

use std::{
    alloc::{alloc, dealloc, Layout},
    ptr::NonNull,
};

// TODO: replace w/ standard traits once they are stabilized
pub trait Allocator {
    unsafe fn alloc(&self, l: Layout) -> Result<NonNull<u8>, AllocError>;
    unsafe fn dealloc(&self, p: NonNull<u8>, l: Layout);
}

#[derive(Debug, Clone, thiserror::Error)]
pub enum AllocError {
    #[error("The allocator is out of memory")]
    OutOfMemory,
}

/// Calls system functions
#[derive(Debug, Default, Clone, Copy)]
pub struct SysAllocator;

impl Allocator for SysAllocator {
    unsafe fn alloc(&self, l: Layout) -> Result<NonNull<u8>, AllocError> {
        let res = alloc(l);
        if res.is_null() {
            return Err(AllocError::OutOfMemory);
        }
        Ok(NonNull::new_unchecked(res))
    }

    unsafe fn dealloc(&self, p: NonNull<u8>, l: Layout) {
        dealloc(p.as_ptr(), l);
    }
}

unsafe impl Send for SysAllocator {}
unsafe impl Sync for SysAllocator {}