]> git.lizzy.rs Git - rust.git/blob - src/liballoc_system/lib.rs
Rollup merge of #38491 - GuillaumeGomez:builder_docs, r=frewsxcv
[rust.git] / src / liballoc_system / lib.rs
1 // Copyright 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 #![crate_name = "alloc_system"]
12 #![crate_type = "rlib"]
13 #![no_std]
14 #![allocator]
15 #![cfg_attr(not(stage0), deny(warnings))]
16 #![unstable(feature = "alloc_system",
17             reason = "this library is unlikely to be stabilized in its current \
18                       form or name",
19             issue = "27783")]
20 #![feature(allocator)]
21 #![feature(staged_api)]
22 #![cfg_attr(any(unix, target_os = "redox"), feature(libc))]
23
24 // The minimum alignment guaranteed by the architecture. This value is used to
25 // add fast paths for low alignment values. In practice, the alignment is a
26 // constant at the call site and the branch will be optimized out.
27 #[cfg(all(any(target_arch = "x86",
28               target_arch = "arm",
29               target_arch = "mips",
30               target_arch = "powerpc",
31               target_arch = "powerpc64",
32               target_arch = "asmjs",
33               target_arch = "wasm32")))]
34 const MIN_ALIGN: usize = 8;
35 #[cfg(all(any(target_arch = "x86_64",
36               target_arch = "aarch64",
37               target_arch = "mips64",
38               target_arch = "s390x")))]
39 const MIN_ALIGN: usize = 16;
40
41 #[no_mangle]
42 pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
43     unsafe { imp::allocate(size, align) }
44 }
45
46 #[no_mangle]
47 pub extern "C" fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
48     unsafe { imp::deallocate(ptr, old_size, align) }
49 }
50
51 #[no_mangle]
52 pub extern "C" fn __rust_reallocate(ptr: *mut u8,
53                                     old_size: usize,
54                                     size: usize,
55                                     align: usize)
56                                     -> *mut u8 {
57     unsafe { imp::reallocate(ptr, old_size, size, align) }
58 }
59
60 #[no_mangle]
61 pub extern "C" fn __rust_reallocate_inplace(ptr: *mut u8,
62                                             old_size: usize,
63                                             size: usize,
64                                             align: usize)
65                                             -> usize {
66     unsafe { imp::reallocate_inplace(ptr, old_size, size, align) }
67 }
68
69 #[no_mangle]
70 pub extern "C" fn __rust_usable_size(size: usize, align: usize) -> usize {
71     imp::usable_size(size, align)
72 }
73
74 #[cfg(any(unix, target_os = "redox"))]
75 mod imp {
76     extern crate libc;
77
78     use core::cmp;
79     use core::ptr;
80     use MIN_ALIGN;
81
82     pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
83         if align <= MIN_ALIGN {
84             libc::malloc(size as libc::size_t) as *mut u8
85         } else {
86             aligned_malloc(size, align)
87         }
88     }
89
90     #[cfg(any(target_os = "android", target_os = "redox"))]
91     unsafe fn aligned_malloc(size: usize, align: usize) -> *mut u8 {
92         // On android we currently target API level 9 which unfortunately
93         // doesn't have the `posix_memalign` API used below. Instead we use
94         // `memalign`, but this unfortunately has the property on some systems
95         // where the memory returned cannot be deallocated by `free`!
96         //
97         // Upon closer inspection, however, this appears to work just fine with
98         // Android, so for this platform we should be fine to call `memalign`
99         // (which is present in API level 9). Some helpful references could
100         // possibly be chromium using memalign [1], attempts at documenting that
101         // memalign + free is ok [2] [3], or the current source of chromium
102         // which still uses memalign on android [4].
103         //
104         // [1]: https://codereview.chromium.org/10796020/
105         // [2]: https://code.google.com/p/android/issues/detail?id=35391
106         // [3]: https://bugs.chromium.org/p/chromium/issues/detail?id=138579
107         // [4]: https://chromium.googlesource.com/chromium/src/base/+/master/
108         //                                       /memory/aligned_memory.cc
109         libc::memalign(align as libc::size_t, size as libc::size_t) as *mut u8
110     }
111
112     #[cfg(not(any(target_os = "android", target_os = "redox")))]
113     unsafe fn aligned_malloc(size: usize, align: usize) -> *mut u8 {
114         let mut out = ptr::null_mut();
115         let ret = libc::posix_memalign(&mut out, align as libc::size_t, size as libc::size_t);
116         if ret != 0 {
117             ptr::null_mut()
118         } else {
119             out as *mut u8
120         }
121     }
122
123     pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 {
124         if align <= MIN_ALIGN {
125             libc::realloc(ptr as *mut libc::c_void, size as libc::size_t) as *mut u8
126         } else {
127             let new_ptr = allocate(size, align);
128             if !new_ptr.is_null() {
129                 ptr::copy(ptr, new_ptr, cmp::min(size, old_size));
130                 deallocate(ptr, old_size, align);
131             }
132             new_ptr
133         }
134     }
135
136     pub unsafe fn reallocate_inplace(_ptr: *mut u8,
137                                      old_size: usize,
138                                      _size: usize,
139                                      _align: usize)
140                                      -> usize {
141         old_size
142     }
143
144     pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, _align: usize) {
145         libc::free(ptr as *mut libc::c_void)
146     }
147
148     pub fn usable_size(size: usize, _align: usize) -> usize {
149         size
150     }
151 }
152
153 #[cfg(windows)]
154 #[allow(bad_style)]
155 mod imp {
156     use MIN_ALIGN;
157
158     type LPVOID = *mut u8;
159     type HANDLE = LPVOID;
160     type SIZE_T = usize;
161     type DWORD = u32;
162     type BOOL = i32;
163
164     extern "system" {
165         fn GetProcessHeap() -> HANDLE;
166         fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
167         fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID, dwBytes: SIZE_T) -> LPVOID;
168         fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
169         fn GetLastError() -> DWORD;
170     }
171
172     #[repr(C)]
173     struct Header(*mut u8);
174
175     const HEAP_REALLOC_IN_PLACE_ONLY: DWORD = 0x00000010;
176
177     unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
178         &mut *(ptr as *mut Header).offset(-1)
179     }
180
181     unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
182         let aligned = ptr.offset((align - (ptr as usize & (align - 1))) as isize);
183         *get_header(aligned) = Header(ptr);
184         aligned
185     }
186
187     pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
188         if align <= MIN_ALIGN {
189             HeapAlloc(GetProcessHeap(), 0, size as SIZE_T) as *mut u8
190         } else {
191             let ptr = HeapAlloc(GetProcessHeap(), 0, (size + align) as SIZE_T) as *mut u8;
192             if ptr.is_null() {
193                 return ptr;
194             }
195             align_ptr(ptr, align)
196         }
197     }
198
199     pub unsafe fn reallocate(ptr: *mut u8, _old_size: usize, size: usize, align: usize) -> *mut u8 {
200         if align <= MIN_ALIGN {
201             HeapReAlloc(GetProcessHeap(), 0, ptr as LPVOID, size as SIZE_T) as *mut u8
202         } else {
203             let header = get_header(ptr);
204             let new = HeapReAlloc(GetProcessHeap(),
205                                   0,
206                                   header.0 as LPVOID,
207                                   (size + align) as SIZE_T) as *mut u8;
208             if new.is_null() {
209                 return new;
210             }
211             align_ptr(new, align)
212         }
213     }
214
215     pub unsafe fn reallocate_inplace(ptr: *mut u8,
216                                      old_size: usize,
217                                      size: usize,
218                                      align: usize)
219                                      -> usize {
220         if align <= MIN_ALIGN {
221             let new = HeapReAlloc(GetProcessHeap(),
222                                   HEAP_REALLOC_IN_PLACE_ONLY,
223                                   ptr as LPVOID,
224                                   size as SIZE_T) as *mut u8;
225             if new.is_null() { old_size } else { size }
226         } else {
227             old_size
228         }
229     }
230
231     pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, align: usize) {
232         if align <= MIN_ALIGN {
233             let err = HeapFree(GetProcessHeap(), 0, ptr as LPVOID);
234             debug_assert!(err != 0, "Failed to free heap memory: {}", GetLastError());
235         } else {
236             let header = get_header(ptr);
237             let err = HeapFree(GetProcessHeap(), 0, header.0 as LPVOID);
238             debug_assert!(err != 0, "Failed to free heap memory: {}", GetLastError());
239         }
240     }
241
242     pub fn usable_size(size: usize, _align: usize) -> usize {
243         size
244     }
245 }