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
#![no_std]
#![deny(warnings)]

mod ffi;
use crate::ffi as kern;
use bk_primitives as raw;

use core::alloc::{GlobalAlloc, Layout};

pub struct BkAllocator;

impl BkAllocator {
    unsafe fn alloc(&self, layout: Layout, flags: i32) -> *mut u8 {
        kern::malloc(
            layout.size() as raw::c_size_t,
            &mut kern::M_DEVBUF[0],
            flags,
        ) as *mut u8
    }
}

unsafe impl GlobalAlloc for BkAllocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        let flags = kern::M_WAITOK as i32;
        self.alloc(layout, flags)
    }
    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
        let flags = kern::M_WAITOK as i32 | kern::M_ZERO as i32;
        self.alloc(layout, flags)
    }
    unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
        kern::free(ptr as *mut raw::c_void, &mut kern::M_DEVBUF[0])
    }
    unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, new_size: usize) -> *mut u8 {
        let flags = kern::M_WAITOK as i32;
        kern::realloc(
            ptr as *mut raw::c_void,
            new_size as raw::c_ulong,
            &mut kern::M_DEVBUF[0],
            flags,
        ) as *mut u8
    }
}

#[global_allocator]
static BK_ALLOCATOR: BkAllocator = BkAllocator;