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