2018-11-12 12:37:54 -06:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
#![no_std]
|
2020-04-03 04:54:18 -05:00
|
|
|
|
2018-11-12 12:37:54 -06:00
|
|
|
pub struct System;
|
2022-03-20 10:55:21 -05:00
|
|
|
|
2020-10-27 08:10:31 -05:00
|
|
|
#[cfg(any(windows, unix, target_os = "redox"))]
|
2018-11-12 12:37:54 -06:00
|
|
|
mod realloc_fallback {
|
|
|
|
use core::alloc::{GlobalAlloc, Layout};
|
|
|
|
use core::cmp;
|
|
|
|
use core::ptr;
|
|
|
|
impl super::System {
|
2022-03-20 10:55:21 -05:00
|
|
|
pub(crate) unsafe fn realloc_fallback(
|
|
|
|
&self,
|
|
|
|
ptr: *mut u8,
|
|
|
|
old_layout: Layout,
|
|
|
|
new_size: usize,
|
|
|
|
) -> *mut u8 {
|
2018-11-12 12:37:54 -06:00
|
|
|
// Docs for GlobalAlloc::realloc require this to be valid:
|
|
|
|
let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align());
|
|
|
|
let new_ptr = GlobalAlloc::alloc(self, new_layout);
|
|
|
|
if !new_ptr.is_null() {
|
|
|
|
let size = cmp::min(old_layout.size(), new_size);
|
|
|
|
ptr::copy_nonoverlapping(ptr, new_ptr, size);
|
|
|
|
GlobalAlloc::dealloc(self, ptr, old_layout);
|
|
|
|
}
|
|
|
|
new_ptr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 08:10:31 -05:00
|
|
|
#[cfg(any(unix, target_os = "redox"))]
|
2018-11-12 12:37:54 -06:00
|
|
|
mod platform {
|
2022-03-20 10:55:21 -05:00
|
|
|
use core::alloc::{GlobalAlloc, Layout};
|
|
|
|
use core::ffi::c_void;
|
2018-11-12 12:37:54 -06:00
|
|
|
use core::ptr;
|
|
|
|
use System;
|
2022-03-20 10:55:21 -05:00
|
|
|
extern "C" {
|
|
|
|
fn posix_memalign(memptr: *mut *mut c_void, align: usize, size: usize) -> i32;
|
|
|
|
fn free(p: *mut c_void);
|
|
|
|
}
|
2018-11-12 12:37:54 -06:00
|
|
|
unsafe impl GlobalAlloc for System {
|
|
|
|
#[inline]
|
|
|
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
2022-03-20 10:55:21 -05:00
|
|
|
aligned_malloc(&layout)
|
2018-11-12 12:37:54 -06:00
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
|
2022-03-20 10:55:21 -05:00
|
|
|
let ptr = self.alloc(layout.clone());
|
|
|
|
if !ptr.is_null() {
|
|
|
|
ptr::write_bytes(ptr, 0, layout.size());
|
2018-11-12 12:37:54 -06:00
|
|
|
}
|
2022-03-20 10:55:21 -05:00
|
|
|
ptr
|
2018-11-12 12:37:54 -06:00
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
|
2022-03-20 10:55:21 -05:00
|
|
|
free(ptr as *mut c_void)
|
2018-11-12 12:37:54 -06:00
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
2022-03-20 10:55:21 -05:00
|
|
|
self.realloc_fallback(ptr, layout, new_size)
|
2018-11-12 12:37:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
|
|
|
|
let mut out = ptr::null_mut();
|
2022-03-20 10:55:21 -05:00
|
|
|
let ret = posix_memalign(&mut out, layout.align(), layout.size());
|
|
|
|
if ret != 0 { ptr::null_mut() } else { out as *mut u8 }
|
2018-11-12 12:37:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
|
|
#[allow(nonstandard_style)]
|
|
|
|
mod platform {
|
|
|
|
use core::alloc::{GlobalAlloc, Layout};
|
2022-03-20 10:55:21 -05:00
|
|
|
use System;
|
2018-11-12 12:37:54 -06:00
|
|
|
type LPVOID = *mut u8;
|
|
|
|
type HANDLE = LPVOID;
|
|
|
|
type SIZE_T = usize;
|
|
|
|
type DWORD = u32;
|
|
|
|
type BOOL = i32;
|
|
|
|
extern "system" {
|
|
|
|
fn GetProcessHeap() -> HANDLE;
|
|
|
|
fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
|
|
|
|
fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID, dwBytes: SIZE_T) -> LPVOID;
|
|
|
|
fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
|
|
|
|
fn GetLastError() -> DWORD;
|
|
|
|
}
|
|
|
|
#[repr(C)]
|
|
|
|
struct Header(*mut u8);
|
|
|
|
const HEAP_ZERO_MEMORY: DWORD = 0x00000008;
|
|
|
|
unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
|
2022-08-19 04:33:06 -05:00
|
|
|
&mut *(ptr as *mut Header).sub(1)
|
2018-11-12 12:37:54 -06:00
|
|
|
}
|
|
|
|
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: DWORD) -> *mut u8 {
|
2022-03-20 10:55:21 -05:00
|
|
|
let size = layout.size() + layout.align();
|
|
|
|
let ptr = HeapAlloc(GetProcessHeap(), flags, size);
|
|
|
|
(if ptr.is_null() { ptr } else { align_ptr(ptr, layout.align()) }) as *mut u8
|
2018-11-12 12:37:54 -06:00
|
|
|
}
|
|
|
|
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, HEAP_ZERO_MEMORY)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
2022-03-20 10:55:21 -05:00
|
|
|
let header = get_header(ptr);
|
|
|
|
let err = HeapFree(GetProcessHeap(), 0, header.0 as LPVOID);
|
|
|
|
debug_assert!(err != 0, "Failed to free heap memory: {}", GetLastError());
|
2018-11-12 12:37:54 -06:00
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
2022-03-20 10:55:21 -05:00
|
|
|
self.realloc_fallback(ptr, layout, new_size)
|
2018-11-12 12:37:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|