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 #![cfg_attr(stage0, feature(custom_attribute))]
12 #![crate_name = "alloc_system"]
13 #![crate_type = "rlib"]
16 #![cfg_attr(not(stage0), allocator)]
17 #![cfg_attr(stage0, allow(improper_ctypes))]
18 #![unstable(feature = "alloc_system",
19 reason = "this library is unlikely to be stabilized in its current \
22 #![feature(allocator)]
25 #![feature(staged_api)]
29 // The minimum alignment guaranteed by the architecture. This value is used to
30 // add fast paths for low alignment values. In practice, the alignment is a
31 // constant at the call site and the branch will be optimized out.
32 #[cfg(all(any(target_arch = "arm",
34 target_arch = "mipsel",
35 target_arch = "powerpc")))]
36 const MIN_ALIGN: usize = 8;
37 #[cfg(all(any(target_arch = "x86",
38 target_arch = "x86_64",
39 target_arch = "aarch64")))]
40 const MIN_ALIGN: usize = 16;
43 pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
44 unsafe { imp::allocate(size, align) }
48 pub extern "C" fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
49 unsafe { imp::deallocate(ptr, old_size, align) }
53 pub extern "C" fn __rust_reallocate(ptr: *mut u8,
58 unsafe { imp::reallocate(ptr, old_size, size, align) }
62 pub extern "C" fn __rust_reallocate_inplace(ptr: *mut u8,
67 unsafe { imp::reallocate_inplace(ptr, old_size, size, align) }
71 pub extern "C" fn __rust_usable_size(size: usize, align: usize) -> usize {
72 imp::usable_size(size, align)
83 // Apparently android doesn't have posix_memalign
84 #[cfg(target_os = "android")]
85 fn memalign(align: libc::size_t, size: libc::size_t) -> *mut libc::c_void;
87 #[cfg(not(target_os = "android"))]
88 fn posix_memalign(memptr: *mut *mut libc::c_void,
94 pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
95 if align <= MIN_ALIGN {
96 libc::malloc(size as libc::size_t) as *mut u8
98 #[cfg(target_os = "android")]
99 unsafe fn more_aligned_malloc(size: usize, align: usize) -> *mut u8 {
100 memalign(align as libc::size_t, size as libc::size_t) as *mut u8
102 #[cfg(not(target_os = "android"))]
103 unsafe fn more_aligned_malloc(size: usize, align: usize) -> *mut u8 {
104 let mut out = ptr::null_mut();
105 let ret = posix_memalign(&mut out, align as libc::size_t, size as libc::size_t);
112 more_aligned_malloc(size, align)
116 pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 {
117 if align <= MIN_ALIGN {
118 libc::realloc(ptr as *mut libc::c_void, size as libc::size_t) as *mut u8
120 let new_ptr = allocate(size, align);
121 ptr::copy(ptr, new_ptr, cmp::min(size, old_size));
122 deallocate(ptr, old_size, align);
127 pub unsafe fn reallocate_inplace(_ptr: *mut u8,
135 pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, _align: usize) {
136 libc::free(ptr as *mut libc::c_void)
139 pub fn usable_size(size: usize, _align: usize) -> usize {
149 type LPVOID = *mut u8;
150 type HANDLE = LPVOID;
156 fn GetProcessHeap() -> HANDLE;
157 fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
158 fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID, dwBytes: SIZE_T) -> LPVOID;
159 fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
163 struct Header(*mut u8);
165 const HEAP_REALLOC_IN_PLACE_ONLY: DWORD = 0x00000010;
167 unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
168 &mut *(ptr as *mut Header).offset(-1)
171 unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
172 let aligned = ptr.offset((align - (ptr as usize & (align - 1))) as isize);
173 *get_header(aligned) = Header(ptr);
177 pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
178 if align <= MIN_ALIGN {
179 HeapAlloc(GetProcessHeap(), 0, size as SIZE_T) as *mut u8
181 let ptr = HeapAlloc(GetProcessHeap(), 0, (size + align) as SIZE_T) as *mut u8;
185 align_ptr(ptr, align)
189 pub unsafe fn reallocate(ptr: *mut u8, _old_size: usize, size: usize, align: usize) -> *mut u8 {
190 if align <= MIN_ALIGN {
191 HeapReAlloc(GetProcessHeap(), 0, ptr as LPVOID, size as SIZE_T) as *mut u8
193 let header = get_header(ptr);
194 let new = HeapReAlloc(GetProcessHeap(),
197 (size + align) as SIZE_T) as *mut u8;
201 align_ptr(new, align)
205 pub unsafe fn reallocate_inplace(ptr: *mut u8,
210 if align <= MIN_ALIGN {
211 let new = HeapReAlloc(GetProcessHeap(),
212 HEAP_REALLOC_IN_PLACE_ONLY,
214 size as SIZE_T) as *mut u8;
225 pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, align: usize) {
226 if align <= MIN_ALIGN {
227 let err = HeapFree(GetProcessHeap(), 0, ptr as LPVOID);
228 debug_assert!(err != 0);
230 let header = get_header(ptr);
231 let err = HeapFree(GetProcessHeap(), 0, header.0 as LPVOID);
232 debug_assert!(err != 0);
236 pub fn usable_size(size: usize, _align: usize) -> usize {