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.
11 #![crate_name = "alloc_system"]
12 #![crate_type = "rlib"]
15 #![unstable(feature = "alloc_system",
16 reason = "this library is unlikely to be stabilized in its current \
19 #![feature(global_allocator)]
20 #![feature(allocator_api)]
22 #![feature(core_intrinsics)]
23 #![feature(staged_api)]
24 #![cfg_attr(any(unix, target_os = "redox"), feature(libc))]
26 // The minimum alignment guaranteed by the architecture. This value is used to
27 // add fast paths for low alignment values. In practice, the alignment is a
28 // constant at the call site and the branch will be optimized out.
29 #[cfg(all(any(target_arch = "x86",
32 target_arch = "powerpc",
33 target_arch = "powerpc64",
34 target_arch = "asmjs",
35 target_arch = "wasm32")))]
36 const MIN_ALIGN: usize = 8;
37 #[cfg(all(any(target_arch = "x86_64",
38 target_arch = "aarch64",
39 target_arch = "mips64",
40 target_arch = "s390x",
41 target_arch = "sparc64")))]
42 const MIN_ALIGN: usize = 16;
46 use self::alloc::heap::{Alloc, AllocErr, Layout, Excess, CannotReallocInPlace};
48 #[unstable(feature = "allocator_api", issue = "32838")]
51 #[unstable(feature = "allocator_api", issue = "32838")]
52 unsafe impl Alloc for System {
54 unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
55 (&*self).alloc(layout)
59 unsafe fn alloc_zeroed(&mut self, layout: Layout)
60 -> Result<*mut u8, AllocErr>
62 (&*self).alloc_zeroed(layout)
66 unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
67 (&*self).dealloc(ptr, layout)
71 unsafe fn realloc(&mut self,
74 new_layout: Layout) -> Result<*mut u8, AllocErr> {
75 (&*self).realloc(ptr, old_layout, new_layout)
78 fn oom(&mut self, err: AllocErr) -> ! {
83 fn usable_size(&self, layout: &Layout) -> (usize, usize) {
84 (&self).usable_size(layout)
88 unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr> {
89 (&*self).alloc_excess(layout)
93 unsafe fn realloc_excess(&mut self,
96 new_layout: Layout) -> Result<Excess, AllocErr> {
97 (&*self).realloc_excess(ptr, layout, new_layout)
101 unsafe fn grow_in_place(&mut self,
104 new_layout: Layout) -> Result<(), CannotReallocInPlace> {
105 (&*self).grow_in_place(ptr, layout, new_layout)
109 unsafe fn shrink_in_place(&mut self,
112 new_layout: Layout) -> Result<(), CannotReallocInPlace> {
113 (&*self).shrink_in_place(ptr, layout, new_layout)
117 #[cfg(any(unix, target_os = "redox"))]
126 use alloc::heap::{Alloc, AllocErr, Layout};
128 #[unstable(feature = "allocator_api", issue = "32838")]
129 unsafe impl<'a> Alloc for &'a System {
131 unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
132 let ptr = if layout.align() <= MIN_ALIGN {
133 libc::malloc(layout.size()) as *mut u8
135 aligned_malloc(&layout)
140 Err(AllocErr::Exhausted { request: layout })
145 unsafe fn alloc_zeroed(&mut self, layout: Layout)
146 -> Result<*mut u8, AllocErr>
148 if layout.align() <= MIN_ALIGN {
149 let ptr = libc::calloc(layout.size(), 1) as *mut u8;
153 Err(AllocErr::Exhausted { request: layout })
156 let ret = self.alloc(layout.clone());
157 if let Ok(ptr) = ret {
158 ptr::write_bytes(ptr, 0, layout.size());
165 unsafe fn dealloc(&mut self, ptr: *mut u8, _layout: Layout) {
166 libc::free(ptr as *mut libc::c_void)
170 unsafe fn realloc(&mut self,
173 new_layout: Layout) -> Result<*mut u8, AllocErr> {
174 if old_layout.align() != new_layout.align() {
175 return Err(AllocErr::Unsupported {
176 details: "cannot change alignment on `realloc`",
180 if new_layout.align() <= MIN_ALIGN {
181 let ptr = libc::realloc(ptr as *mut libc::c_void, new_layout.size());
185 Err(AllocErr::Exhausted { request: new_layout })
188 let res = self.alloc(new_layout.clone());
189 if let Ok(new_ptr) = res {
190 let size = cmp::min(old_layout.size(), new_layout.size());
191 ptr::copy_nonoverlapping(ptr, new_ptr, size);
192 self.dealloc(ptr, old_layout);
198 fn oom(&mut self, err: AllocErr) -> ! {
199 use core::fmt::{self, Write};
201 // Print a message to stderr before aborting to assist with
202 // debugging. It is critical that this code does not allocate any
203 // memory since we are in an OOM situation. Any errors are ignored
204 // while printing since there's nothing we can do about them and we
205 // are about to exit anyways.
206 drop(writeln!(Stderr, "fatal runtime error: {}", err));
208 ::core::intrinsics::abort();
213 impl Write for Stderr {
214 fn write_str(&mut self, s: &str) -> fmt::Result {
216 libc::write(libc::STDERR_FILENO,
217 s.as_ptr() as *const libc::c_void,
226 #[cfg(any(target_os = "android", target_os = "redox"))]
228 unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
229 // On android we currently target API level 9 which unfortunately
230 // doesn't have the `posix_memalign` API used below. Instead we use
231 // `memalign`, but this unfortunately has the property on some systems
232 // where the memory returned cannot be deallocated by `free`!
234 // Upon closer inspection, however, this appears to work just fine with
235 // Android, so for this platform we should be fine to call `memalign`
236 // (which is present in API level 9). Some helpful references could
237 // possibly be chromium using memalign [1], attempts at documenting that
238 // memalign + free is ok [2] [3], or the current source of chromium
239 // which still uses memalign on android [4].
241 // [1]: https://codereview.chromium.org/10796020/
242 // [2]: https://code.google.com/p/android/issues/detail?id=35391
243 // [3]: https://bugs.chromium.org/p/chromium/issues/detail?id=138579
244 // [4]: https://chromium.googlesource.com/chromium/src/base/+/master/
245 // /memory/aligned_memory.cc
246 libc::memalign(layout.align(), layout.size()) as *mut u8
249 #[cfg(not(any(target_os = "android", target_os = "redox")))]
251 unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
252 let mut out = ptr::null_mut();
253 let ret = libc::posix_memalign(&mut out, layout.align(), layout.size());
270 use alloc::heap::{Alloc, AllocErr, Layout, CannotReallocInPlace};
272 type LPVOID = *mut u8;
273 type HANDLE = LPVOID;
277 type LPDWORD = *mut DWORD;
278 type LPOVERLAPPED = *mut u8;
280 const STD_ERROR_HANDLE: DWORD = -12i32 as DWORD;
283 fn GetProcessHeap() -> HANDLE;
284 fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
285 fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID, dwBytes: SIZE_T) -> LPVOID;
286 fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
287 fn GetLastError() -> DWORD;
288 fn WriteFile(hFile: HANDLE,
290 nNumberOfBytesToWrite: DWORD,
291 lpNumberOfBytesWritten: LPDWORD,
292 lpOverlapped: LPOVERLAPPED)
294 fn GetStdHandle(which: DWORD) -> HANDLE;
298 struct Header(*mut u8);
300 const HEAP_ZERO_MEMORY: DWORD = 0x00000008;
301 const HEAP_REALLOC_IN_PLACE_ONLY: DWORD = 0x00000010;
303 unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
304 &mut *(ptr as *mut Header).offset(-1)
307 unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
308 let aligned = ptr.offset((align - (ptr as usize & (align - 1))) as isize);
309 *get_header(aligned) = Header(ptr);
314 unsafe fn allocate_with_flags(layout: Layout, flags: DWORD)
315 -> Result<*mut u8, AllocErr>
317 let ptr = if layout.align() <= MIN_ALIGN {
318 HeapAlloc(GetProcessHeap(), flags, layout.size())
320 let size = layout.size() + layout.align();
321 let ptr = HeapAlloc(GetProcessHeap(), flags, size);
325 align_ptr(ptr, layout.align())
329 Err(AllocErr::Exhausted { request: layout })
335 #[unstable(feature = "allocator_api", issue = "32838")]
336 unsafe impl<'a> Alloc for &'a System {
338 unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
339 allocate_with_flags(layout, 0)
343 unsafe fn alloc_zeroed(&mut self, layout: Layout)
344 -> Result<*mut u8, AllocErr>
346 allocate_with_flags(layout, HEAP_ZERO_MEMORY)
350 unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
351 if layout.align() <= MIN_ALIGN {
352 let err = HeapFree(GetProcessHeap(), 0, ptr as LPVOID);
353 debug_assert!(err != 0, "Failed to free heap memory: {}",
356 let header = get_header(ptr);
357 let err = HeapFree(GetProcessHeap(), 0, header.0 as LPVOID);
358 debug_assert!(err != 0, "Failed to free heap memory: {}",
364 unsafe fn realloc(&mut self,
367 new_layout: Layout) -> Result<*mut u8, AllocErr> {
368 if old_layout.align() != new_layout.align() {
369 return Err(AllocErr::Unsupported {
370 details: "cannot change alignment on `realloc`",
374 if new_layout.align() <= MIN_ALIGN {
375 let ptr = HeapReAlloc(GetProcessHeap(),
382 Err(AllocErr::Exhausted { request: new_layout })
385 let res = self.alloc(new_layout.clone());
386 if let Ok(new_ptr) = res {
387 let size = cmp::min(old_layout.size(), new_layout.size());
388 ptr::copy_nonoverlapping(ptr, new_ptr, size);
389 self.dealloc(ptr, old_layout);
396 unsafe fn grow_in_place(&mut self,
399 new_layout: Layout) -> Result<(), CannotReallocInPlace> {
400 self.shrink_in_place(ptr, layout, new_layout)
404 unsafe fn shrink_in_place(&mut self,
407 new_layout: Layout) -> Result<(), CannotReallocInPlace> {
408 if old_layout.align() != new_layout.align() {
409 return Err(CannotReallocInPlace)
412 let new = if new_layout.align() <= MIN_ALIGN {
413 HeapReAlloc(GetProcessHeap(),
414 HEAP_REALLOC_IN_PLACE_ONLY,
418 let header = get_header(ptr);
419 HeapReAlloc(GetProcessHeap(),
420 HEAP_REALLOC_IN_PLACE_ONLY,
422 new_layout.size() + new_layout.align())
425 Err(CannotReallocInPlace)
431 fn oom(&mut self, err: AllocErr) -> ! {
432 use core::fmt::{self, Write};
434 // Same as with unix we ignore all errors here
435 drop(writeln!(Stderr, "fatal runtime error: {}", err));
437 ::core::intrinsics::abort();
442 impl Write for Stderr {
443 fn write_str(&mut self, s: &str) -> fmt::Result {
445 // WriteFile silently fails if it is passed an invalid
446 // handle, so there is no need to check the result of
448 WriteFile(GetStdHandle(STD_ERROR_HANDLE),
449 s.as_ptr() as LPVOID,