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