1 // Copyright 2017 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.
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.
13 #![unstable(issue = "32838", feature = "allocator_api")]
15 pub use alloc::heap::{Heap, Alloc, Layout, Excess, CannotReallocInPlace, AllocErr};
17 pub use alloc_system::System;
19 #[cfg(all(not(stage0), not(test)))]
21 pub mod __default_lib_allocator {
22 use super::{System, Layout, Alloc, AllocErr};
25 // for symbol names src/librustc/middle/allocator.rs
26 // for signatures src/librustc_allocator/lib.rs
28 // linkage directives are provided as part of the current compiler allocator
32 pub unsafe extern fn __rdl_alloc(size: usize,
34 err: *mut u8) -> *mut u8 {
35 let layout = Layout::from_size_align_unchecked(size, align);
36 match System.alloc(layout) {
39 ptr::write(err as *mut AllocErr, e);
46 pub unsafe extern fn __rdl_oom(err: *const u8) -> ! {
47 System.oom((*(err as *const AllocErr)).clone())
51 pub unsafe extern fn __rdl_dealloc(ptr: *mut u8,
54 System.dealloc(ptr, Layout::from_size_align_unchecked(size, align))
58 pub unsafe extern fn __rdl_usable_size(layout: *const u8,
61 let pair = System.usable_size(&*(layout as *const Layout));
67 pub unsafe extern fn __rdl_realloc(ptr: *mut u8,
72 err: *mut u8) -> *mut u8 {
73 let old_layout = Layout::from_size_align_unchecked(old_size, old_align);
74 let new_layout = Layout::from_size_align_unchecked(new_size, new_align);
75 match System.realloc(ptr, old_layout, new_layout) {
78 ptr::write(err as *mut AllocErr, e);
85 pub unsafe extern fn __rdl_alloc_zeroed(size: usize,
87 err: *mut u8) -> *mut u8 {
88 let layout = Layout::from_size_align_unchecked(size, align);
89 match System.alloc_zeroed(layout) {
92 ptr::write(err as *mut AllocErr, e);
99 pub unsafe extern fn __rdl_alloc_excess(size: usize,
102 err: *mut u8) -> *mut u8 {
103 let layout = Layout::from_size_align_unchecked(size, align);
104 match System.alloc_excess(layout) {
110 ptr::write(err as *mut AllocErr, e);
117 pub unsafe extern fn __rdl_realloc_excess(ptr: *mut u8,
123 err: *mut u8) -> *mut u8 {
124 let old_layout = Layout::from_size_align_unchecked(old_size, old_align);
125 let new_layout = Layout::from_size_align_unchecked(new_size, new_align);
126 match System.realloc_excess(ptr, old_layout, new_layout) {
132 ptr::write(err as *mut AllocErr, e);
139 pub unsafe extern fn __rdl_grow_in_place(ptr: *mut u8,
143 new_align: usize) -> u8 {
144 let old_layout = Layout::from_size_align_unchecked(old_size, old_align);
145 let new_layout = Layout::from_size_align_unchecked(new_size, new_align);
146 match System.grow_in_place(ptr, old_layout, new_layout) {
153 pub unsafe extern fn __rdl_shrink_in_place(ptr: *mut u8,
157 new_align: usize) -> u8 {
158 let old_layout = Layout::from_size_align_unchecked(old_size, old_align);
159 let new_layout = Layout::from_size_align_unchecked(new_size, new_align);
160 match System.shrink_in_place(ptr, old_layout, new_layout) {