]> git.proxmox.com Git - rustc.git/blob - src/libstd/heap.rs
New upstream version 1.26.2+dfsg1
[rustc.git] / src / libstd / heap.rs
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.
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 //! dox
12
13 #![unstable(issue = "32838", feature = "allocator_api")]
14
15 pub use alloc::heap::Heap;
16 pub use alloc_system::System;
17 pub use core::heap::*;
18
19 #[cfg(not(test))]
20 #[doc(hidden)]
21 #[allow(unused_attributes)]
22 pub mod __default_lib_allocator {
23 use super::{System, Layout, Alloc, AllocErr};
24 use ptr;
25
26 // for symbol names src/librustc/middle/allocator.rs
27 // for signatures src/librustc_allocator/lib.rs
28
29 // linkage directives are provided as part of the current compiler allocator
30 // ABI
31
32 #[no_mangle]
33 #[rustc_std_internal_symbol]
34 pub unsafe extern fn __rdl_alloc(size: usize,
35 align: usize,
36 err: *mut u8) -> *mut u8 {
37 let layout = Layout::from_size_align_unchecked(size, align);
38 match System.alloc(layout) {
39 Ok(p) => p,
40 Err(e) => {
41 ptr::write(err as *mut AllocErr, e);
42 0 as *mut u8
43 }
44 }
45 }
46
47 #[no_mangle]
48 #[rustc_std_internal_symbol]
49 pub unsafe extern fn __rdl_oom(err: *const u8) -> ! {
50 System.oom((*(err as *const AllocErr)).clone())
51 }
52
53 #[no_mangle]
54 #[rustc_std_internal_symbol]
55 pub unsafe extern fn __rdl_dealloc(ptr: *mut u8,
56 size: usize,
57 align: usize) {
58 System.dealloc(ptr, Layout::from_size_align_unchecked(size, align))
59 }
60
61 #[no_mangle]
62 #[rustc_std_internal_symbol]
63 pub unsafe extern fn __rdl_usable_size(layout: *const u8,
64 min: *mut usize,
65 max: *mut usize) {
66 let pair = System.usable_size(&*(layout as *const Layout));
67 *min = pair.0;
68 *max = pair.1;
69 }
70
71 #[no_mangle]
72 #[rustc_std_internal_symbol]
73 pub unsafe extern fn __rdl_realloc(ptr: *mut u8,
74 old_size: usize,
75 old_align: usize,
76 new_size: usize,
77 new_align: usize,
78 err: *mut u8) -> *mut u8 {
79 let old_layout = Layout::from_size_align_unchecked(old_size, old_align);
80 let new_layout = Layout::from_size_align_unchecked(new_size, new_align);
81 match System.realloc(ptr, old_layout, new_layout) {
82 Ok(p) => p,
83 Err(e) => {
84 ptr::write(err as *mut AllocErr, e);
85 0 as *mut u8
86 }
87 }
88 }
89
90 #[no_mangle]
91 #[rustc_std_internal_symbol]
92 pub unsafe extern fn __rdl_alloc_zeroed(size: usize,
93 align: usize,
94 err: *mut u8) -> *mut u8 {
95 let layout = Layout::from_size_align_unchecked(size, align);
96 match System.alloc_zeroed(layout) {
97 Ok(p) => p,
98 Err(e) => {
99 ptr::write(err as *mut AllocErr, e);
100 0 as *mut u8
101 }
102 }
103 }
104
105 #[no_mangle]
106 #[rustc_std_internal_symbol]
107 pub unsafe extern fn __rdl_alloc_excess(size: usize,
108 align: usize,
109 excess: *mut usize,
110 err: *mut u8) -> *mut u8 {
111 let layout = Layout::from_size_align_unchecked(size, align);
112 match System.alloc_excess(layout) {
113 Ok(p) => {
114 *excess = p.1;
115 p.0
116 }
117 Err(e) => {
118 ptr::write(err as *mut AllocErr, e);
119 0 as *mut u8
120 }
121 }
122 }
123
124 #[no_mangle]
125 #[rustc_std_internal_symbol]
126 pub unsafe extern fn __rdl_realloc_excess(ptr: *mut u8,
127 old_size: usize,
128 old_align: usize,
129 new_size: usize,
130 new_align: usize,
131 excess: *mut usize,
132 err: *mut u8) -> *mut u8 {
133 let old_layout = Layout::from_size_align_unchecked(old_size, old_align);
134 let new_layout = Layout::from_size_align_unchecked(new_size, new_align);
135 match System.realloc_excess(ptr, old_layout, new_layout) {
136 Ok(p) => {
137 *excess = p.1;
138 p.0
139 }
140 Err(e) => {
141 ptr::write(err as *mut AllocErr, e);
142 0 as *mut u8
143 }
144 }
145 }
146
147 #[no_mangle]
148 #[rustc_std_internal_symbol]
149 pub unsafe extern fn __rdl_grow_in_place(ptr: *mut u8,
150 old_size: usize,
151 old_align: usize,
152 new_size: usize,
153 new_align: usize) -> u8 {
154 let old_layout = Layout::from_size_align_unchecked(old_size, old_align);
155 let new_layout = Layout::from_size_align_unchecked(new_size, new_align);
156 match System.grow_in_place(ptr, old_layout, new_layout) {
157 Ok(()) => 1,
158 Err(_) => 0,
159 }
160 }
161
162 #[no_mangle]
163 #[rustc_std_internal_symbol]
164 pub unsafe extern fn __rdl_shrink_in_place(ptr: *mut u8,
165 old_size: usize,
166 old_align: usize,
167 new_size: usize,
168 new_align: usize) -> u8 {
169 let old_layout = Layout::from_size_align_unchecked(old_size, old_align);
170 let new_layout = Layout::from_size_align_unchecked(new_size, new_align);
171 match System.shrink_in_place(ptr, old_layout, new_layout) {
172 Ok(()) => 1,
173 Err(_) => 0,
174 }
175 }
176 }