|
| 1 | +// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +#![unstable(feature = "allocator_api", |
| 12 | + reason = "the precise API and guarantees it provides may be tweaked \ |
| 13 | + slightly, especially to possibly take into account the \ |
| 14 | + types being stored to make room for a future \ |
| 15 | + tracing garbage collector", |
| 16 | + issue = "32838")] |
| 17 | + |
| 18 | +use core::intrinsics::{min_align_of_val, size_of_val}; |
| 19 | +use core::ptr::NonNull; |
| 20 | +use core::usize; |
| 21 | + |
| 22 | +#[doc(inline)] |
| 23 | +pub use core::alloc::*; |
| 24 | + |
| 25 | +#[cfg(stage0)] |
| 26 | +extern "Rust" { |
| 27 | + #[allocator] |
| 28 | + #[rustc_allocator_nounwind] |
| 29 | + fn __rust_alloc(size: usize, align: usize, err: *mut u8) -> *mut u8; |
| 30 | + #[cold] |
| 31 | + #[rustc_allocator_nounwind] |
| 32 | + fn __rust_oom(err: *const u8) -> !; |
| 33 | + #[rustc_allocator_nounwind] |
| 34 | + fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize); |
| 35 | + #[rustc_allocator_nounwind] |
| 36 | + fn __rust_realloc(ptr: *mut u8, |
| 37 | + old_size: usize, |
| 38 | + old_align: usize, |
| 39 | + new_size: usize, |
| 40 | + new_align: usize, |
| 41 | + err: *mut u8) -> *mut u8; |
| 42 | + #[rustc_allocator_nounwind] |
| 43 | + fn __rust_alloc_zeroed(size: usize, align: usize, err: *mut u8) -> *mut u8; |
| 44 | +} |
| 45 | + |
| 46 | +#[cfg(not(stage0))] |
| 47 | +extern "Rust" { |
| 48 | + #[allocator] |
| 49 | + #[rustc_allocator_nounwind] |
| 50 | + fn __rust_alloc(size: usize, align: usize) -> *mut u8; |
| 51 | + #[cold] |
| 52 | + #[rustc_allocator_nounwind] |
| 53 | + fn __rust_oom() -> !; |
| 54 | + #[rustc_allocator_nounwind] |
| 55 | + fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize); |
| 56 | + #[rustc_allocator_nounwind] |
| 57 | + fn __rust_realloc(ptr: *mut u8, |
| 58 | + old_size: usize, |
| 59 | + align: usize, |
| 60 | + new_size: usize) -> *mut u8; |
| 61 | + #[rustc_allocator_nounwind] |
| 62 | + fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8; |
| 63 | +} |
| 64 | + |
| 65 | +#[derive(Copy, Clone, Default, Debug)] |
| 66 | +pub struct Global; |
| 67 | + |
| 68 | +#[unstable(feature = "allocator_api", issue = "32838")] |
| 69 | +#[rustc_deprecated(since = "1.27.0", reason = "type renamed to `Global`")] |
| 70 | +pub type Heap = Global; |
| 71 | + |
| 72 | +#[unstable(feature = "allocator_api", issue = "32838")] |
| 73 | +#[rustc_deprecated(since = "1.27.0", reason = "type renamed to `Global`")] |
| 74 | +#[allow(non_upper_case_globals)] |
| 75 | +pub const Heap: Global = Global; |
| 76 | + |
| 77 | +unsafe impl GlobalAlloc for Global { |
| 78 | + #[inline] |
| 79 | + unsafe fn alloc(&self, layout: Layout) -> *mut Opaque { |
| 80 | + #[cfg(not(stage0))] |
| 81 | + let ptr = __rust_alloc(layout.size(), layout.align()); |
| 82 | + #[cfg(stage0)] |
| 83 | + let ptr = __rust_alloc(layout.size(), layout.align(), &mut 0); |
| 84 | + ptr as *mut Opaque |
| 85 | + } |
| 86 | + |
| 87 | + #[inline] |
| 88 | + unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) { |
| 89 | + __rust_dealloc(ptr as *mut u8, layout.size(), layout.align()) |
| 90 | + } |
| 91 | + |
| 92 | + #[inline] |
| 93 | + unsafe fn realloc(&self, ptr: *mut Opaque, layout: Layout, new_size: usize) -> *mut Opaque { |
| 94 | + #[cfg(not(stage0))] |
| 95 | + let ptr = __rust_realloc(ptr as *mut u8, layout.size(), layout.align(), new_size); |
| 96 | + #[cfg(stage0)] |
| 97 | + let ptr = __rust_realloc(ptr as *mut u8, layout.size(), layout.align(), |
| 98 | + new_size, layout.align(), &mut 0); |
| 99 | + ptr as *mut Opaque |
| 100 | + } |
| 101 | + |
| 102 | + #[inline] |
| 103 | + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut Opaque { |
| 104 | + #[cfg(not(stage0))] |
| 105 | + let ptr = __rust_alloc_zeroed(layout.size(), layout.align()); |
| 106 | + #[cfg(stage0)] |
| 107 | + let ptr = __rust_alloc_zeroed(layout.size(), layout.align(), &mut 0); |
| 108 | + ptr as *mut Opaque |
| 109 | + } |
| 110 | + |
| 111 | + #[inline] |
| 112 | + fn oom(&self) -> ! { |
| 113 | + unsafe { |
| 114 | + #[cfg(not(stage0))] |
| 115 | + __rust_oom(); |
| 116 | + #[cfg(stage0)] |
| 117 | + __rust_oom(&mut 0); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +unsafe impl Alloc for Global { |
| 123 | + #[inline] |
| 124 | + unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<Opaque>, AllocErr> { |
| 125 | + NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr) |
| 126 | + } |
| 127 | + |
| 128 | + #[inline] |
| 129 | + unsafe fn dealloc(&mut self, ptr: NonNull<Opaque>, layout: Layout) { |
| 130 | + GlobalAlloc::dealloc(self, ptr.as_ptr(), layout) |
| 131 | + } |
| 132 | + |
| 133 | + #[inline] |
| 134 | + unsafe fn realloc(&mut self, |
| 135 | + ptr: NonNull<Opaque>, |
| 136 | + layout: Layout, |
| 137 | + new_size: usize) |
| 138 | + -> Result<NonNull<Opaque>, AllocErr> |
| 139 | + { |
| 140 | + NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr) |
| 141 | + } |
| 142 | + |
| 143 | + #[inline] |
| 144 | + unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<Opaque>, AllocErr> { |
| 145 | + NonNull::new(GlobalAlloc::alloc_zeroed(self, layout)).ok_or(AllocErr) |
| 146 | + } |
| 147 | + |
| 148 | + #[inline] |
| 149 | + fn oom(&mut self) -> ! { |
| 150 | + GlobalAlloc::oom(self) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +/// The allocator for unique pointers. |
| 155 | +// This function must not unwind. If it does, MIR trans will fail. |
| 156 | +#[cfg(not(test))] |
| 157 | +#[lang = "exchange_malloc"] |
| 158 | +#[inline] |
| 159 | +unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 { |
| 160 | + if size == 0 { |
| 161 | + align as *mut u8 |
| 162 | + } else { |
| 163 | + let layout = Layout::from_size_align_unchecked(size, align); |
| 164 | + let ptr = Global.alloc(layout); |
| 165 | + if !ptr.is_null() { |
| 166 | + ptr as *mut u8 |
| 167 | + } else { |
| 168 | + Global.oom() |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +#[cfg_attr(not(test), lang = "box_free")] |
| 174 | +#[inline] |
| 175 | +pub(crate) unsafe fn box_free<T: ?Sized>(ptr: *mut T) { |
| 176 | + let size = size_of_val(&*ptr); |
| 177 | + let align = min_align_of_val(&*ptr); |
| 178 | + // We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary. |
| 179 | + if size != 0 { |
| 180 | + let layout = Layout::from_size_align_unchecked(size, align); |
| 181 | + Global.dealloc(ptr as *mut Opaque, layout); |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +#[cfg(test)] |
| 186 | +mod tests { |
| 187 | + extern crate test; |
| 188 | + use self::test::Bencher; |
| 189 | + use boxed::Box; |
| 190 | + use alloc::{Global, Alloc, Layout}; |
| 191 | + |
| 192 | + #[test] |
| 193 | + fn allocate_zeroed() { |
| 194 | + unsafe { |
| 195 | + let layout = Layout::from_size_align(1024, 1).unwrap(); |
| 196 | + let ptr = Global.alloc_zeroed(layout.clone()) |
| 197 | + .unwrap_or_else(|_| Global.oom()); |
| 198 | + |
| 199 | + let mut i = ptr.cast::<u8>().as_ptr(); |
| 200 | + let end = i.offset(layout.size() as isize); |
| 201 | + while i < end { |
| 202 | + assert_eq!(*i, 0); |
| 203 | + i = i.offset(1); |
| 204 | + } |
| 205 | + Global.dealloc(ptr, layout); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + #[bench] |
| 210 | + fn alloc_owned_small(b: &mut Bencher) { |
| 211 | + b.iter(|| { |
| 212 | + let _: Box<_> = box 10; |
| 213 | + }) |
| 214 | + } |
| 215 | +} |
0 commit comments