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 #![cfg_attr(not(stage0), deny(warnings))]
16 #![unstable(feature = "alloc_system",
17 reason = "this library is unlikely to be stabilized in its current \
20 #![feature(allocator)]
21 #![feature(staged_api)]
22 #![cfg_attr(unix, feature(libc))]
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",
30 target_arch = "powerpc",
31 target_arch = "powerpc64",
32 target_arch = "asmjs")))]
33 const MIN_ALIGN: usize = 8;
34 #[cfg(all(any(target_arch = "x86_64",
35 target_arch = "aarch64")))]
36 const MIN_ALIGN: usize = 16;
39 pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
40 unsafe { imp::allocate(size, align) }
44 pub extern "C" fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
45 unsafe { imp::deallocate(ptr, old_size, align) }
49 pub extern "C" fn __rust_reallocate(ptr: *mut u8,
54 unsafe { imp::reallocate(ptr, old_size, size, align) }
58 pub extern "C" fn __rust_reallocate_inplace(ptr: *mut u8,
63 unsafe { imp::reallocate_inplace(ptr, old_size, size, align) }
67 pub extern "C" fn __rust_usable_size(size: usize, align: usize) -> usize {
68 imp::usable_size(size, align)
79 pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
80 if align <= MIN_ALIGN {
81 libc::malloc(size as libc::size_t) as *mut u8
83 let mut out = ptr::null_mut();
84 let ret = libc::posix_memalign(&mut out, align as libc::size_t, size as libc::size_t);
93 pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 {
94 if align <= MIN_ALIGN {
95 libc::realloc(ptr as *mut libc::c_void, size as libc::size_t) as *mut u8
97 let new_ptr = allocate(size, align);
98 if !new_ptr.is_null() {
99 ptr::copy(ptr, new_ptr, cmp::min(size, old_size));
100 deallocate(ptr, old_size, align);
106 pub unsafe fn reallocate_inplace(_ptr: *mut u8,
114 pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, _align: usize) {
115 libc::free(ptr as *mut libc::c_void)
118 pub fn usable_size(size: usize, _align: usize) -> usize {
128 type LPVOID = *mut u8;
129 type HANDLE = LPVOID;
135 fn GetProcessHeap() -> HANDLE;
136 fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
137 fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID, dwBytes: SIZE_T) -> LPVOID;
138 fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
142 struct Header(*mut u8);
144 const HEAP_REALLOC_IN_PLACE_ONLY: DWORD = 0x00000010;
146 unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
147 &mut *(ptr as *mut Header).offset(-1)
150 unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
151 let aligned = ptr.offset((align - (ptr as usize & (align - 1))) as isize);
152 *get_header(aligned) = Header(ptr);
156 pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
157 if align <= MIN_ALIGN {
158 HeapAlloc(GetProcessHeap(), 0, size as SIZE_T) as *mut u8
160 let ptr = HeapAlloc(GetProcessHeap(), 0, (size + align) as SIZE_T) as *mut u8;
164 align_ptr(ptr, align)
168 pub unsafe fn reallocate(ptr: *mut u8, _old_size: usize, size: usize, align: usize) -> *mut u8 {
169 if align <= MIN_ALIGN {
170 HeapReAlloc(GetProcessHeap(), 0, ptr as LPVOID, size as SIZE_T) as *mut u8
172 let header = get_header(ptr);
173 let new = HeapReAlloc(GetProcessHeap(),
176 (size + align) as SIZE_T) as *mut u8;
180 align_ptr(new, align)
184 pub unsafe fn reallocate_inplace(ptr: *mut u8,
189 if align <= MIN_ALIGN {
190 let new = HeapReAlloc(GetProcessHeap(),
191 HEAP_REALLOC_IN_PLACE_ONLY,
193 size as SIZE_T) as *mut u8;
204 pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, align: usize) {
205 if align <= MIN_ALIGN {
206 let err = HeapFree(GetProcessHeap(), 0, ptr as LPVOID);
207 debug_assert!(err != 0);
209 let header = get_header(ptr);
210 let err = HeapFree(GetProcessHeap(), 0, header.0 as LPVOID);
211 debug_assert!(err != 0);
215 pub fn usable_size(size: usize, _align: usize) -> usize {