2019-02-11 04:23:21 +09:00
|
|
|
use crate::alloc::{GlobalAlloc, Layout, System};
|
|
|
|
|
use crate::sys::c;
|
2019-11-27 10:28:39 -08:00
|
|
|
use crate::sys_common::alloc::{realloc_fallback, MIN_ALIGN};
|
2018-11-03 11:15:48 -07:00
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
|
struct Header(*mut u8);
|
|
|
|
|
|
|
|
|
|
unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
|
|
|
|
|
&mut *(ptr as *mut Header).offset(-1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
|
|
|
|
|
let aligned = ptr.add(align - (ptr as usize & (align - 1)));
|
|
|
|
|
*get_header(aligned) = Header(ptr);
|
|
|
|
|
aligned
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn allocate_with_flags(layout: Layout, flags: c::DWORD) -> *mut u8 {
|
|
|
|
|
if layout.align() <= MIN_ALIGN {
|
2019-11-27 10:28:39 -08:00
|
|
|
return c::HeapAlloc(c::GetProcessHeap(), flags, layout.size()) as *mut u8;
|
2018-11-03 11:15:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let size = layout.size() + layout.align();
|
|
|
|
|
let ptr = c::HeapAlloc(c::GetProcessHeap(), flags, size);
|
2019-11-27 10:28:39 -08:00
|
|
|
if ptr.is_null() { ptr as *mut u8 } else { align_ptr(ptr as *mut u8, layout.align()) }
|
2018-11-03 11:15:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "alloc_system_type", since = "1.28.0")]
|
|
|
|
|
unsafe impl GlobalAlloc for System {
|
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
|
|
|
|
allocate_with_flags(layout, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
|
|
|
|
|
allocate_with_flags(layout, c::HEAP_ZERO_MEMORY)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
|
|
|
|
if layout.align() <= MIN_ALIGN {
|
|
|
|
|
let err = c::HeapFree(c::GetProcessHeap(), 0, ptr as c::LPVOID);
|
2019-11-27 10:28:39 -08:00
|
|
|
debug_assert!(err != 0, "Failed to free heap memory: {}", c::GetLastError());
|
2018-11-03 11:15:48 -07:00
|
|
|
} else {
|
|
|
|
|
let header = get_header(ptr);
|
|
|
|
|
let err = c::HeapFree(c::GetProcessHeap(), 0, header.0 as c::LPVOID);
|
2019-11-27 10:28:39 -08:00
|
|
|
debug_assert!(err != 0, "Failed to free heap memory: {}", c::GetLastError());
|
2018-11-03 11:15:48 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
|
|
|
|
if layout.align() <= MIN_ALIGN {
|
|
|
|
|
c::HeapReAlloc(c::GetProcessHeap(), 0, ptr as c::LPVOID, new_size) as *mut u8
|
|
|
|
|
} else {
|
|
|
|
|
realloc_fallback(self, ptr, layout, new_size)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|