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
use crate::reserve::{ReserveHeader, ReserveType, RESERVE_ALIGN};
use crate::{huge, subhuge, utils};
use core::alloc::{GlobalAlloc, Layout};
use core::{cmp, ptr};
use haz_alloc_internal::UsizeExt;

/// # Safety
///
/// Layout must be valid.
#[inline]
pub unsafe fn alloc(layout: Layout) -> *mut u8 {
    if layout.align() > RESERVE_ALIGN {
        ptr::null_mut()
    } else if layout.size() > subhuge::MAX || layout.align() > utils::page_size() {
        huge::alloc(layout)
    } else {
        subhuge::alloc(layout, false)
    }
}

/// # Safety
///
/// Layout must be valid.
#[inline]
pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
    if layout.align() > RESERVE_ALIGN {
        ptr::null_mut()
    } else if layout.size() > subhuge::MAX || layout.align() > utils::page_size() {
        huge::alloc(layout)
    } else {
        subhuge::alloc(layout, true)
    }
}

/// # Safety
///
/// Layout and pointer must be valid.
///
/// Alignment must match of original allocation.
pub unsafe fn realloc(ptr: *mut u8, layout: Layout) -> *mut u8 {
    let header = (ptr as usize - 1).align_down(RESERVE_ALIGN) as *mut ReserveHeader;
    match (*header).ty {
        ReserveType::Huge => {
            if huge::realloc_in_place(header, ptr, layout) {
                return ptr;
            }
        }
        ReserveType::SubHuge => {
            if subhuge::realloc_in_place(header, ptr, layout) {
                return ptr;
            }
        }
    }

    let new = alloc(layout);
    if new.is_null() {
        return ptr::null_mut();
    }
    new.copy_from_nonoverlapping(ptr, cmp::min(layout.size(), size(ptr)));
    dealloc(ptr);
    new
}

/// # Safety
///
/// Pointer must be valid.
#[inline]
pub unsafe fn dealloc(ptr: *mut u8) {
    let header = (ptr as usize - 1).align_down(RESERVE_ALIGN) as *mut ReserveHeader;
    match (*header).ty {
        ReserveType::Huge => huge::dealloc(header),
        ReserveType::SubHuge => subhuge::dealloc(header, ptr),
    }
}

/// # Safety
///
/// Pointer must be valid.
#[inline]
pub unsafe fn size(ptr: *mut u8) -> usize {
    let header = (ptr as usize - 1).align_down(RESERVE_ALIGN) as *mut ReserveHeader;
    match (*header).ty {
        ReserveType::Huge => huge::size(header, ptr),
        ReserveType::SubHuge => subhuge::size(ptr),
    }
}

pub struct Alloc;
unsafe impl GlobalAlloc for Alloc {
    #[inline]
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        alloc(layout)
    }

    #[inline]
    unsafe fn dealloc(&self, ptr: *mut u8, _: Layout) {
        dealloc(ptr)
    }

    #[inline]
    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
        alloc_zeroed(layout)
    }

    #[inline]
    unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
        realloc(
            ptr,
            Layout::from_size_align_unchecked(new_size, layout.align()),
        )
    }
}