]> git.lizzy.rs Git - rust.git/blob - src/liballoc_system/lib.rs
save-analysis: make sure we save the def for the last segment of a path
[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 #![no_std]
12 #![allow(unused_attributes)]
13 #![unstable(feature = "alloc_system",
14             reason = "this library is unlikely to be stabilized in its current \
15                       form or name",
16             issue = "32838")]
17
18 #![feature(allocator_api)]
19 #![feature(core_intrinsics)]
20 #![feature(nll)]
21 #![feature(staged_api)]
22 #![feature(rustc_attrs)]
23 #![cfg_attr(
24     all(target_arch = "wasm32", not(target_os = "emscripten")),
25     feature(integer_atomics, stdsimd)
26 )]
27 #![cfg_attr(any(unix, target_os = "cloudabi", target_os = "redox"), feature(libc))]
28 #![rustc_alloc_kind = "lib"]
29
30 // The minimum alignment guaranteed by the architecture. This value is used to
31 // add fast paths for low alignment values.
32 #[cfg(all(any(target_arch = "x86",
33               target_arch = "arm",
34               target_arch = "mips",
35               target_arch = "powerpc",
36               target_arch = "powerpc64",
37               target_arch = "asmjs",
38               target_arch = "wasm32")))]
39 #[allow(dead_code)]
40 const MIN_ALIGN: usize = 8;
41 #[cfg(all(any(target_arch = "x86_64",
42               target_arch = "aarch64",
43               target_arch = "mips64",
44               target_arch = "s390x",
45               target_arch = "sparc64")))]
46 #[allow(dead_code)]
47 const MIN_ALIGN: usize = 16;
48
49 use core::alloc::{Alloc, GlobalAlloc, AllocErr, Layout};
50 use core::ptr::NonNull;
51
52 /// The default memory allocator provided by the operating system.
53 ///
54 /// This is based on `malloc` on Unix platforms and `HeapAlloc` on Windows,
55 /// plus related functions.
56 ///
57 /// This type can be used in a `static` item
58 /// with the `#[global_allocator]` attribute
59 /// to force the global allocator to be the system’s one.
60 /// (The default is jemalloc for executables, on some platforms.)
61 ///
62 /// ```rust
63 /// use std::alloc::System;
64 ///
65 /// #[global_allocator]
66 /// static A: System = System;
67 ///
68 /// fn main() {
69 ///     let a = Box::new(4); // Allocates from the system allocator.
70 ///     println!("{}", a);
71 /// }
72 /// ```
73 ///
74 /// It can also be used directly to allocate memory
75 /// independently of the standard library’s global allocator.
76 #[stable(feature = "alloc_system_type", since = "1.28.0")]
77 pub struct System;
78
79 #[unstable(feature = "allocator_api", issue = "32838")]
80 unsafe impl Alloc for System {
81     #[inline]
82     unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
83         NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr)
84     }
85
86     #[inline]
87     unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
88         NonNull::new(GlobalAlloc::alloc_zeroed(self, layout)).ok_or(AllocErr)
89     }
90
91     #[inline]
92     unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
93         GlobalAlloc::dealloc(self, ptr.as_ptr(), layout)
94     }
95
96     #[inline]
97     unsafe fn realloc(&mut self,
98                       ptr: NonNull<u8>,
99                       layout: Layout,
100                       new_size: usize) -> Result<NonNull<u8>, AllocErr> {
101         NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
102     }
103 }
104
105 #[cfg(any(windows, unix, target_os = "cloudabi", target_os = "redox"))]
106 mod realloc_fallback {
107     use core::alloc::{GlobalAlloc, Layout};
108     use core::cmp;
109     use core::ptr;
110
111     impl super::System {
112         pub(crate) unsafe fn realloc_fallback(&self, ptr: *mut u8, old_layout: Layout,
113                                               new_size: usize) -> *mut u8 {
114             // Docs for GlobalAlloc::realloc require this to be valid:
115             let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align());
116
117             let new_ptr = GlobalAlloc::alloc(self, new_layout);
118             if !new_ptr.is_null() {
119                 let size = cmp::min(old_layout.size(), new_size);
120                 ptr::copy_nonoverlapping(ptr, new_ptr, size);
121                 GlobalAlloc::dealloc(self, ptr, old_layout);
122             }
123             new_ptr
124         }
125     }
126 }
127
128 #[cfg(any(unix, target_os = "cloudabi", target_os = "redox"))]
129 mod platform {
130     extern crate libc;
131
132     use core::ptr;
133
134     use MIN_ALIGN;
135     use System;
136     use core::alloc::{GlobalAlloc, Layout};
137
138     #[stable(feature = "alloc_system_type", since = "1.28.0")]
139     unsafe impl GlobalAlloc for System {
140         #[inline]
141         unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
142             if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
143                 libc::malloc(layout.size()) as *mut u8
144             } else {
145                 #[cfg(target_os = "macos")]
146                 {
147                     if layout.align() > (1 << 31) {
148                         return ptr::null_mut()
149                     }
150                 }
151                 aligned_malloc(&layout)
152             }
153         }
154
155         #[inline]
156         unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
157             if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
158                 libc::calloc(layout.size(), 1) as *mut u8
159             } else {
160                 let ptr = self.alloc(layout.clone());
161                 if !ptr.is_null() {
162                     ptr::write_bytes(ptr, 0, layout.size());
163                 }
164                 ptr
165             }
166         }
167
168         #[inline]
169         unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
170             libc::free(ptr as *mut libc::c_void)
171         }
172
173         #[inline]
174         unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
175             if layout.align() <= MIN_ALIGN && layout.align() <= new_size {
176                 libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8
177             } else {
178                 self.realloc_fallback(ptr, layout, new_size)
179             }
180         }
181     }
182
183     #[cfg(any(target_os = "android",
184               target_os = "hermit",
185               target_os = "redox",
186               target_os = "solaris"))]
187     #[inline]
188     unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
189         // On android we currently target API level 9 which unfortunately
190         // doesn't have the `posix_memalign` API used below. Instead we use
191         // `memalign`, but this unfortunately has the property on some systems
192         // where the memory returned cannot be deallocated by `free`!
193         //
194         // Upon closer inspection, however, this appears to work just fine with
195         // Android, so for this platform we should be fine to call `memalign`
196         // (which is present in API level 9). Some helpful references could
197         // possibly be chromium using memalign [1], attempts at documenting that
198         // memalign + free is ok [2] [3], or the current source of chromium
199         // which still uses memalign on android [4].
200         //
201         // [1]: https://codereview.chromium.org/10796020/
202         // [2]: https://code.google.com/p/android/issues/detail?id=35391
203         // [3]: https://bugs.chromium.org/p/chromium/issues/detail?id=138579
204         // [4]: https://chromium.googlesource.com/chromium/src/base/+/master/
205         //                                       /memory/aligned_memory.cc
206         libc::memalign(layout.align(), layout.size()) as *mut u8
207     }
208
209     #[cfg(not(any(target_os = "android",
210                   target_os = "hermit",
211                   target_os = "redox",
212                   target_os = "solaris")))]
213     #[inline]
214     unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
215         let mut out = ptr::null_mut();
216         let ret = libc::posix_memalign(&mut out, layout.align(), layout.size());
217         if ret != 0 {
218             ptr::null_mut()
219         } else {
220             out as *mut u8
221         }
222     }
223 }
224
225 #[cfg(windows)]
226 #[allow(nonstandard_style)]
227 mod platform {
228     use MIN_ALIGN;
229     use System;
230     use core::alloc::{GlobalAlloc, Layout};
231
232     type LPVOID = *mut u8;
233     type HANDLE = LPVOID;
234     type SIZE_T = usize;
235     type DWORD = u32;
236     type BOOL = i32;
237
238     extern "system" {
239         fn GetProcessHeap() -> HANDLE;
240         fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
241         fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID, dwBytes: SIZE_T) -> LPVOID;
242         fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
243         fn GetLastError() -> DWORD;
244     }
245
246     #[repr(C)]
247     struct Header(*mut u8);
248
249     const HEAP_ZERO_MEMORY: DWORD = 0x00000008;
250
251     unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
252         &mut *(ptr as *mut Header).offset(-1)
253     }
254
255     unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
256         let aligned = ptr.add(align - (ptr as usize & (align - 1)));
257         *get_header(aligned) = Header(ptr);
258         aligned
259     }
260
261     #[inline]
262     unsafe fn allocate_with_flags(layout: Layout, flags: DWORD) -> *mut u8 {
263         let ptr = if layout.align() <= MIN_ALIGN {
264             HeapAlloc(GetProcessHeap(), flags, layout.size())
265         } else {
266             let size = layout.size() + layout.align();
267             let ptr = HeapAlloc(GetProcessHeap(), flags, size);
268             if ptr.is_null() {
269                 ptr
270             } else {
271                 align_ptr(ptr, layout.align())
272             }
273         };
274         ptr as *mut u8
275     }
276
277     #[stable(feature = "alloc_system_type", since = "1.28.0")]
278     unsafe impl GlobalAlloc for System {
279         #[inline]
280         unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
281             allocate_with_flags(layout, 0)
282         }
283
284         #[inline]
285         unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
286             allocate_with_flags(layout, HEAP_ZERO_MEMORY)
287         }
288
289         #[inline]
290         unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
291             if layout.align() <= MIN_ALIGN {
292                 let err = HeapFree(GetProcessHeap(), 0, ptr as LPVOID);
293                 debug_assert!(err != 0, "Failed to free heap memory: {}",
294                               GetLastError());
295             } else {
296                 let header = get_header(ptr);
297                 let err = HeapFree(GetProcessHeap(), 0, header.0 as LPVOID);
298                 debug_assert!(err != 0, "Failed to free heap memory: {}",
299                               GetLastError());
300             }
301         }
302
303         #[inline]
304         unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
305             if layout.align() <= MIN_ALIGN {
306                 HeapReAlloc(GetProcessHeap(), 0, ptr as LPVOID, new_size) as *mut u8
307             } else {
308                 self.realloc_fallback(ptr, layout, new_size)
309             }
310         }
311     }
312 }
313
314 // This is an implementation of a global allocator on the wasm32 platform when
315 // emscripten is not in use. In that situation there's no actual runtime for us
316 // to lean on for allocation, so instead we provide our own!
317 //
318 // The wasm32 instruction set has two instructions for getting the current
319 // amount of memory and growing the amount of memory. These instructions are the
320 // foundation on which we're able to build an allocator, so we do so! Note that
321 // the instructions are also pretty "global" and this is the "global" allocator
322 // after all!
323 //
324 // The current allocator here is the `dlmalloc` crate which we've got included
325 // in the rust-lang/rust repository as a submodule. The crate is a port of
326 // dlmalloc.c from C to Rust and is basically just so we can have "pure Rust"
327 // for now which is currently technically required (can't link with C yet).
328 //
329 // The crate itself provides a global allocator which on wasm has no
330 // synchronization as there are no threads!
331 #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
332 mod platform {
333     extern crate dlmalloc;
334
335     use core::alloc::{GlobalAlloc, Layout};
336     use System;
337
338     static mut DLMALLOC: dlmalloc::Dlmalloc = dlmalloc::DLMALLOC_INIT;
339
340     #[stable(feature = "alloc_system_type", since = "1.28.0")]
341     unsafe impl GlobalAlloc for System {
342         #[inline]
343         unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
344             let _lock = lock::lock();
345             DLMALLOC.malloc(layout.size(), layout.align())
346         }
347
348         #[inline]
349         unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
350             let _lock = lock::lock();
351             DLMALLOC.calloc(layout.size(), layout.align())
352         }
353
354         #[inline]
355         unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
356             let _lock = lock::lock();
357             DLMALLOC.free(ptr, layout.size(), layout.align())
358         }
359
360         #[inline]
361         unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
362             let _lock = lock::lock();
363             DLMALLOC.realloc(ptr, layout.size(), layout.align(), new_size)
364         }
365     }
366
367     #[cfg(target_feature = "atomics")]
368     mod lock {
369         use core::arch::wasm32;
370         use core::sync::atomic::{AtomicI32, Ordering::SeqCst};
371
372         static LOCKED: AtomicI32 = AtomicI32::new(0);
373
374         pub struct DropLock;
375
376         pub fn lock() -> DropLock {
377             loop {
378                 if LOCKED.swap(1, SeqCst) == 0 {
379                     return DropLock
380                 }
381                 unsafe {
382                     let r = wasm32::atomic::wait_i32(
383                         &LOCKED as *const AtomicI32 as *mut i32,
384                         1,  // expected value
385                         -1, // timeout
386                     );
387                     debug_assert!(r == 0 || r == 1);
388                 }
389             }
390         }
391
392         impl Drop for DropLock {
393             fn drop(&mut self) {
394                 let r = LOCKED.swap(0, SeqCst);
395                 debug_assert_eq!(r, 1);
396                 unsafe {
397                     wasm32::atomic::wake(
398                         &LOCKED as *const AtomicI32 as *mut i32,
399                         1, // only one thread
400                     );
401                 }
402             }
403         }
404     }
405
406     #[cfg(not(target_feature = "atomics"))]
407     mod lock {
408         pub fn lock() {} // no atomics, no threads, that's easy!
409     }
410 }