2014-12-12 17:39:27 -06:00
|
|
|
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
|
2014-04-25 01:19:34 -05:00
|
|
|
// 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.
|
|
|
|
|
2017-06-03 16:54:08 -05:00
|
|
|
#![unstable(feature = "allocator_api",
|
2015-06-09 13:52:41 -05:00
|
|
|
reason = "the precise API and guarantees it provides may be tweaked \
|
|
|
|
slightly, especially to possibly take into account the \
|
|
|
|
types being stored to make room for a future \
|
2015-08-13 00:19:08 -05:00
|
|
|
tracing garbage collector",
|
2017-06-03 16:54:08 -05:00
|
|
|
issue = "32838")]
|
2015-06-09 13:52:41 -05:00
|
|
|
|
2016-11-11 04:55:47 -06:00
|
|
|
use core::intrinsics::{min_align_of_val, size_of_val};
|
2018-04-19 20:24:53 -05:00
|
|
|
use core::ptr::{NonNull, Unique};
|
2017-06-03 16:54:08 -05:00
|
|
|
use core::usize;
|
2015-02-07 17:49:54 -06:00
|
|
|
|
2018-04-02 03:38:07 -05:00
|
|
|
#[doc(inline)]
|
2018-04-03 14:05:10 -05:00
|
|
|
pub use core::alloc::*;
|
2018-04-02 03:38:07 -05:00
|
|
|
|
2018-04-03 10:12:57 -05:00
|
|
|
#[cfg(stage0)]
|
2017-06-03 16:54:08 -05:00
|
|
|
extern "Rust" {
|
|
|
|
#[allocator]
|
2017-08-22 16:36:49 -05:00
|
|
|
#[rustc_allocator_nounwind]
|
2017-06-03 16:54:08 -05:00
|
|
|
fn __rust_alloc(size: usize, align: usize, err: *mut u8) -> *mut u8;
|
2018-04-04 11:57:48 -05:00
|
|
|
#[cold]
|
|
|
|
#[rustc_allocator_nounwind]
|
|
|
|
fn __rust_oom(err: *const u8) -> !;
|
2017-08-22 16:36:49 -05:00
|
|
|
#[rustc_allocator_nounwind]
|
2017-06-03 16:54:08 -05:00
|
|
|
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
|
2017-08-22 16:36:49 -05:00
|
|
|
#[rustc_allocator_nounwind]
|
2017-06-03 16:54:08 -05:00
|
|
|
fn __rust_realloc(ptr: *mut u8,
|
|
|
|
old_size: usize,
|
|
|
|
old_align: usize,
|
|
|
|
new_size: usize,
|
|
|
|
new_align: usize,
|
|
|
|
err: *mut u8) -> *mut u8;
|
2017-08-22 16:36:49 -05:00
|
|
|
#[rustc_allocator_nounwind]
|
2017-06-03 16:54:08 -05:00
|
|
|
fn __rust_alloc_zeroed(size: usize, align: usize, err: *mut u8) -> *mut u8;
|
2018-04-03 10:12:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(stage0))]
|
|
|
|
extern "Rust" {
|
|
|
|
#[allocator]
|
2017-08-22 16:36:49 -05:00
|
|
|
#[rustc_allocator_nounwind]
|
2018-04-03 10:12:57 -05:00
|
|
|
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
|
2017-08-22 16:36:49 -05:00
|
|
|
#[rustc_allocator_nounwind]
|
2018-04-03 10:12:57 -05:00
|
|
|
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
|
2017-08-22 16:36:49 -05:00
|
|
|
#[rustc_allocator_nounwind]
|
2018-04-03 10:12:57 -05:00
|
|
|
fn __rust_realloc(ptr: *mut u8,
|
|
|
|
old_size: usize,
|
|
|
|
align: usize,
|
|
|
|
new_size: usize) -> *mut u8;
|
2017-08-22 16:36:49 -05:00
|
|
|
#[rustc_allocator_nounwind]
|
2018-04-03 10:12:57 -05:00
|
|
|
fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
|
2015-02-07 17:49:54 -06:00
|
|
|
}
|
|
|
|
|
2017-05-23 07:47:41 -05:00
|
|
|
#[derive(Copy, Clone, Default, Debug)]
|
2018-04-03 07:43:34 -05:00
|
|
|
pub struct Global;
|
2017-05-23 07:47:41 -05:00
|
|
|
|
2018-04-03 07:43:34 -05:00
|
|
|
#[unstable(feature = "allocator_api", issue = "32838")]
|
|
|
|
#[rustc_deprecated(since = "1.27.0", reason = "type renamed to `Global`")]
|
2018-04-03 14:15:06 -05:00
|
|
|
pub type Heap = Global;
|
2018-04-03 07:43:34 -05:00
|
|
|
|
2018-04-03 14:15:06 -05:00
|
|
|
#[unstable(feature = "allocator_api", issue = "32838")]
|
|
|
|
#[rustc_deprecated(since = "1.27.0", reason = "type renamed to `Global`")]
|
|
|
|
#[allow(non_upper_case_globals)]
|
|
|
|
pub const Heap: Global = Global;
|
2018-04-03 07:43:34 -05:00
|
|
|
|
2018-04-04 12:15:22 -05:00
|
|
|
unsafe impl GlobalAlloc for Global {
|
2017-06-03 16:54:08 -05:00
|
|
|
#[inline]
|
2018-04-11 10:19:48 -05:00
|
|
|
unsafe fn alloc(&self, layout: Layout) -> *mut Opaque {
|
2018-04-03 10:12:57 -05:00
|
|
|
#[cfg(not(stage0))]
|
|
|
|
let ptr = __rust_alloc(layout.size(), layout.align());
|
|
|
|
#[cfg(stage0)]
|
|
|
|
let ptr = __rust_alloc(layout.size(), layout.align(), &mut 0);
|
2018-04-11 10:19:48 -05:00
|
|
|
ptr as *mut Opaque
|
2017-05-23 07:47:41 -05:00
|
|
|
}
|
|
|
|
|
2017-06-03 16:54:08 -05:00
|
|
|
#[inline]
|
2018-04-11 10:19:48 -05:00
|
|
|
unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) {
|
2018-04-04 12:15:22 -05:00
|
|
|
__rust_dealloc(ptr as *mut u8, layout.size(), layout.align())
|
2017-05-23 07:47:41 -05:00
|
|
|
}
|
|
|
|
|
2017-06-03 16:54:08 -05:00
|
|
|
#[inline]
|
2018-04-11 10:19:48 -05:00
|
|
|
unsafe fn realloc(&self, ptr: *mut Opaque, layout: Layout, new_size: usize) -> *mut Opaque {
|
2018-04-04 10:19:16 -05:00
|
|
|
#[cfg(not(stage0))]
|
2018-04-04 12:15:22 -05:00
|
|
|
let ptr = __rust_realloc(ptr as *mut u8, layout.size(), layout.align(), new_size);
|
2018-04-04 10:19:16 -05:00
|
|
|
#[cfg(stage0)]
|
2018-04-04 12:15:22 -05:00
|
|
|
let ptr = __rust_realloc(ptr as *mut u8, layout.size(), layout.align(),
|
2018-04-04 10:19:16 -05:00
|
|
|
new_size, layout.align(), &mut 0);
|
2018-04-11 10:19:48 -05:00
|
|
|
ptr as *mut Opaque
|
2017-05-23 07:47:41 -05:00
|
|
|
}
|
|
|
|
|
2017-06-03 16:54:08 -05:00
|
|
|
#[inline]
|
2018-04-11 10:19:48 -05:00
|
|
|
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut Opaque {
|
2018-04-03 10:12:57 -05:00
|
|
|
#[cfg(not(stage0))]
|
|
|
|
let ptr = __rust_alloc_zeroed(layout.size(), layout.align());
|
|
|
|
#[cfg(stage0)]
|
|
|
|
let ptr = __rust_alloc_zeroed(layout.size(), layout.align(), &mut 0);
|
2018-04-11 10:19:48 -05:00
|
|
|
ptr as *mut Opaque
|
2017-06-03 16:54:08 -05:00
|
|
|
}
|
2014-05-11 16:41:15 -05:00
|
|
|
}
|
|
|
|
|
2018-04-04 12:15:22 -05:00
|
|
|
unsafe impl Alloc for Global {
|
|
|
|
#[inline]
|
2018-04-11 10:19:48 -05:00
|
|
|
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<Opaque>, AllocErr> {
|
2018-04-11 09:28:37 -05:00
|
|
|
NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr)
|
2018-04-04 12:15:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-04-11 10:19:48 -05:00
|
|
|
unsafe fn dealloc(&mut self, ptr: NonNull<Opaque>, layout: Layout) {
|
2018-04-02 18:51:02 -05:00
|
|
|
GlobalAlloc::dealloc(self, ptr.as_ptr(), layout)
|
2018-04-04 12:15:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
unsafe fn realloc(&mut self,
|
2018-04-11 10:19:48 -05:00
|
|
|
ptr: NonNull<Opaque>,
|
2018-04-04 12:15:22 -05:00
|
|
|
layout: Layout,
|
|
|
|
new_size: usize)
|
2018-04-11 10:19:48 -05:00
|
|
|
-> Result<NonNull<Opaque>, AllocErr>
|
2018-04-04 12:15:22 -05:00
|
|
|
{
|
2018-04-11 09:28:37 -05:00
|
|
|
NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
|
2018-04-04 12:15:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-04-11 10:19:48 -05:00
|
|
|
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<Opaque>, AllocErr> {
|
2018-04-11 09:28:37 -05:00
|
|
|
NonNull::new(GlobalAlloc::alloc_zeroed(self, layout)).ok_or(AllocErr)
|
2018-04-04 12:15:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-06 21:03:14 -05:00
|
|
|
/// The allocator for unique pointers.
|
2016-12-15 19:00:19 -06:00
|
|
|
// This function must not unwind. If it does, MIR trans will fail.
|
2014-05-12 01:51:00 -05:00
|
|
|
#[cfg(not(test))]
|
2015-05-09 14:50:28 -05:00
|
|
|
#[lang = "exchange_malloc"]
|
2014-05-06 21:03:14 -05:00
|
|
|
#[inline]
|
2015-02-09 01:00:46 -06:00
|
|
|
unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
|
2014-05-06 21:03:14 -05:00
|
|
|
if size == 0 {
|
2017-05-04 13:48:58 -05:00
|
|
|
align as *mut u8
|
2014-05-06 21:03:14 -05:00
|
|
|
} else {
|
2017-06-03 16:54:08 -05:00
|
|
|
let layout = Layout::from_size_align_unchecked(size, align);
|
2018-04-04 12:15:22 -05:00
|
|
|
let ptr = Global.alloc(layout);
|
|
|
|
if !ptr.is_null() {
|
|
|
|
ptr as *mut u8
|
|
|
|
} else {
|
2018-04-20 23:05:13 -05:00
|
|
|
oom()
|
2018-04-04 12:15:22 -05:00
|
|
|
}
|
2014-05-06 21:03:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 20:24:53 -05:00
|
|
|
#[cfg(stage0)]
|
|
|
|
#[lang = "box_free"]
|
|
|
|
#[inline]
|
|
|
|
unsafe fn old_box_free<T: ?Sized>(ptr: *mut T) {
|
|
|
|
box_free(Unique::new_unchecked(ptr))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(not(any(test, stage0)), lang = "box_free")]
|
2016-01-28 15:59:00 -06:00
|
|
|
#[inline]
|
2018-04-19 20:24:53 -05:00
|
|
|
pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
|
|
|
|
let ptr = ptr.as_ptr();
|
2016-11-11 04:55:47 -06:00
|
|
|
let size = size_of_val(&*ptr);
|
|
|
|
let align = min_align_of_val(&*ptr);
|
2016-01-28 15:59:00 -06:00
|
|
|
// We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.
|
|
|
|
if size != 0 {
|
2017-06-03 16:54:08 -05:00
|
|
|
let layout = Layout::from_size_align_unchecked(size, align);
|
2018-04-11 10:19:48 -05:00
|
|
|
Global.dealloc(ptr as *mut Opaque, layout);
|
2016-01-28 15:59:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 23:05:13 -05:00
|
|
|
#[cfg(stage0)]
|
|
|
|
pub fn oom() -> ! {
|
|
|
|
unsafe { ::core::intrinsics::abort() }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(stage0))]
|
|
|
|
pub fn oom() -> ! {
|
|
|
|
extern {
|
|
|
|
#[lang = "oom"]
|
|
|
|
fn oom_impl() -> !;
|
|
|
|
}
|
|
|
|
unsafe { oom_impl() }
|
|
|
|
}
|
|
|
|
|
2014-05-06 21:03:14 -05:00
|
|
|
#[cfg(test)]
|
2015-04-24 10:30:41 -05:00
|
|
|
mod tests {
|
2014-05-06 21:03:14 -05:00
|
|
|
extern crate test;
|
|
|
|
use self::test::Bencher;
|
2015-02-17 14:41:32 -06:00
|
|
|
use boxed::Box;
|
2018-04-20 23:05:13 -05:00
|
|
|
use alloc::{Global, Alloc, Layout, oom};
|
2014-05-06 21:03:14 -05:00
|
|
|
|
2017-03-09 19:53:01 -06:00
|
|
|
#[test]
|
|
|
|
fn allocate_zeroed() {
|
|
|
|
unsafe {
|
2017-06-03 16:54:08 -05:00
|
|
|
let layout = Layout::from_size_align(1024, 1).unwrap();
|
2018-04-03 07:43:34 -05:00
|
|
|
let ptr = Global.alloc_zeroed(layout.clone())
|
2018-04-20 23:05:13 -05:00
|
|
|
.unwrap_or_else(|_| oom());
|
2017-03-09 19:53:01 -06:00
|
|
|
|
2018-04-02 18:51:02 -05:00
|
|
|
let mut i = ptr.cast::<u8>().as_ptr();
|
|
|
|
let end = i.offset(layout.size() as isize);
|
2017-03-09 19:53:01 -06:00
|
|
|
while i < end {
|
|
|
|
assert_eq!(*i, 0);
|
|
|
|
i = i.offset(1);
|
|
|
|
}
|
2018-04-03 07:43:34 -05:00
|
|
|
Global.dealloc(ptr, layout);
|
2014-10-02 02:29:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-06 21:03:14 -05:00
|
|
|
#[bench]
|
|
|
|
fn alloc_owned_small(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
2015-02-17 14:41:32 -06:00
|
|
|
let _: Box<_> = box 10;
|
2014-05-06 21:03:14 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|