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.
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.
12 #![allow(unused_attributes)]
13 #![unstable(feature = "alloc_system",
14 reason = "this library is unlikely to be stabilized in its current \
18 #![feature(allocator_api)]
19 #![feature(core_intrinsics)]
21 #![feature(staged_api)]
22 #![feature(rustc_attrs)]
24 all(target_arch = "wasm32", not(target_os = "emscripten")),
25 feature(integer_atomics, stdsimd)
27 #![cfg_attr(any(unix, target_os = "cloudabi", target_os = "redox"), feature(libc))]
28 #![rustc_alloc_kind = "lib"]
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",
35 target_arch = "powerpc",
36 target_arch = "powerpc64",
37 target_arch = "asmjs",
38 target_arch = "wasm32")))]
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")))]
47 const MIN_ALIGN: usize = 16;
49 use core::alloc::{Alloc, GlobalAlloc, AllocErr, Layout};
50 use core::ptr::NonNull;
52 /// The default memory allocator provided by the operating system.
54 /// This is based on `malloc` on Unix platforms and `HeapAlloc` on Windows,
55 /// plus related functions.
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.)
63 /// use std::alloc::System;
65 /// #[global_allocator]
66 /// static A: System = System;
69 /// let a = Box::new(4); // Allocates from the system allocator.
70 /// println!("{}", a);
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")]
79 #[unstable(feature = "allocator_api", issue = "32838")]
80 unsafe impl Alloc for System {
82 unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
83 NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr)
87 unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
88 NonNull::new(GlobalAlloc::alloc_zeroed(self, layout)).ok_or(AllocErr)
92 unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
93 GlobalAlloc::dealloc(self, ptr.as_ptr(), layout)
97 unsafe fn realloc(&mut self,
100 new_size: usize) -> Result<NonNull<u8>, AllocErr> {
101 NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
105 #[cfg(any(windows, unix, target_os = "cloudabi", target_os = "redox"))]
106 mod realloc_fallback {
107 use core::alloc::{GlobalAlloc, Layout};
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());
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);
128 #[cfg(any(unix, target_os = "cloudabi", target_os = "redox"))]
136 use core::alloc::{GlobalAlloc, Layout};
138 #[stable(feature = "alloc_system_type", since = "1.28.0")]
139 unsafe impl GlobalAlloc for System {
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
145 #[cfg(target_os = "macos")]
147 if layout.align() > (1 << 31) {
148 return ptr::null_mut()
151 aligned_malloc(&layout)
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
160 let ptr = self.alloc(layout.clone());
162 ptr::write_bytes(ptr, 0, layout.size());
169 unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
170 libc::free(ptr as *mut libc::c_void)
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
178 self.realloc_fallback(ptr, layout, new_size)
183 #[cfg(any(target_os = "android",
184 target_os = "hermit",
186 target_os = "solaris"))]
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`!
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].
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
209 #[cfg(not(any(target_os = "android",
210 target_os = "hermit",
212 target_os = "solaris")))]
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());
226 #[allow(nonstandard_style)]
230 use core::alloc::{GlobalAlloc, Layout};
232 type LPVOID = *mut u8;
233 type HANDLE = LPVOID;
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;
247 struct Header(*mut u8);
249 const HEAP_ZERO_MEMORY: DWORD = 0x00000008;
251 unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
252 &mut *(ptr as *mut Header).offset(-1)
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);
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())
266 let size = layout.size() + layout.align();
267 let ptr = HeapAlloc(GetProcessHeap(), flags, size);
271 align_ptr(ptr, layout.align())
277 #[stable(feature = "alloc_system_type", since = "1.28.0")]
278 unsafe impl GlobalAlloc for System {
280 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
281 allocate_with_flags(layout, 0)
285 unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
286 allocate_with_flags(layout, HEAP_ZERO_MEMORY)
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: {}",
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: {}",
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
308 self.realloc_fallback(ptr, layout, new_size)
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!
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
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).
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")))]
333 extern crate dlmalloc;
335 use core::alloc::{GlobalAlloc, Layout};
338 static mut DLMALLOC: dlmalloc::Dlmalloc = dlmalloc::DLMALLOC_INIT;
340 #[stable(feature = "alloc_system_type", since = "1.28.0")]
341 unsafe impl GlobalAlloc for System {
343 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
344 let _lock = lock::lock();
345 DLMALLOC.malloc(layout.size(), layout.align())
349 unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
350 let _lock = lock::lock();
351 DLMALLOC.calloc(layout.size(), layout.align())
355 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
356 let _lock = lock::lock();
357 DLMALLOC.free(ptr, layout.size(), layout.align())
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)
367 #[cfg(target_feature = "atomics")]
369 use core::arch::wasm32;
370 use core::sync::atomic::{AtomicI32, Ordering::SeqCst};
372 static LOCKED: AtomicI32 = AtomicI32::new(0);
376 pub fn lock() -> DropLock {
378 if LOCKED.swap(1, SeqCst) == 0 {
382 let r = wasm32::atomic::wait_i32(
383 &LOCKED as *const AtomicI32 as *mut i32,
387 debug_assert!(r == 0 || r == 1);
392 impl Drop for DropLock {
394 let r = LOCKED.swap(0, SeqCst);
395 debug_assert_eq!(r, 1);
397 wasm32::atomic::wake(
398 &LOCKED as *const AtomicI32 as *mut i32,
399 1, // only one thread
406 #[cfg(not(target_feature = "atomics"))]
408 pub fn lock() {} // no atomics, no threads, that's easy!