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 #![unstable(feature = "alloc_system",
18 reason = "this library is unlikely to be stabilized in its current \
21 #![feature(allocator)]
24 #![feature(staged_api)]
28 // The minimum alignment guaranteed by the architecture. This value is used to
29 // add fast paths for low alignment values. In practice, the alignment is a
30 // constant at the call site and the branch will be optimized out.
31 #[cfg(all(any(target_arch = "arm",
33 target_arch = "mipsel",
34 target_arch = "powerpc")))]
35 const MIN_ALIGN: usize = 8;
36 #[cfg(all(any(target_arch = "x86",
37 target_arch = "x86_64",
38 target_arch = "aarch64")))]
39 const MIN_ALIGN: usize = 16;
42 pub extern fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
43 unsafe { imp::allocate(size, align) }
47 pub extern fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
48 unsafe { imp::deallocate(ptr, old_size, align) }
52 pub extern fn __rust_reallocate(ptr: *mut u8, old_size: usize, size: usize,
53 align: usize) -> *mut u8 {
54 unsafe { imp::reallocate(ptr, old_size, size, align) }
58 pub extern fn __rust_reallocate_inplace(ptr: *mut u8, old_size: usize,
59 size: usize, align: usize) -> usize {
60 unsafe { imp::reallocate_inplace(ptr, old_size, size, align) }
64 pub extern fn __rust_usable_size(size: usize, align: usize) -> usize {
65 imp::usable_size(size, align)
76 // Apparently android doesn't have posix_memalign
77 #[cfg(target_os = "android")]
78 fn memalign(align: libc::size_t, size: libc::size_t) -> *mut libc::c_void;
80 #[cfg(not(target_os = "android"))]
81 fn posix_memalign(memptr: *mut *mut libc::c_void,
83 size: libc::size_t) -> libc::c_int;
86 pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
87 if align <= MIN_ALIGN {
88 libc::malloc(size as libc::size_t) as *mut u8
90 #[cfg(target_os = "android")]
91 unsafe fn more_aligned_malloc(size: usize, align: usize) -> *mut u8 {
92 memalign(align as libc::size_t, size as libc::size_t) as *mut u8
94 #[cfg(not(target_os = "android"))]
95 unsafe fn more_aligned_malloc(size: usize, align: usize) -> *mut u8 {
96 let mut out = ptr::null_mut();
97 let ret = posix_memalign(&mut out,
98 align as libc::size_t,
99 size as libc::size_t);
106 more_aligned_malloc(size, align)
110 pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize,
111 align: usize) -> *mut u8 {
112 if align <= MIN_ALIGN {
113 libc::realloc(ptr as *mut libc::c_void, size as libc::size_t) as *mut u8
115 let new_ptr = allocate(size, align);
116 ptr::copy(ptr, new_ptr, cmp::min(size, old_size));
117 deallocate(ptr, old_size, align);
122 pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: usize, _size: usize,
123 _align: usize) -> usize {
127 pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, _align: usize) {
128 libc::free(ptr as *mut libc::c_void)
131 pub fn usable_size(size: usize, _align: usize) -> usize {
138 use libc::{BOOL, DWORD, HANDLE, LPVOID, SIZE_T};
142 fn GetProcessHeap() -> HANDLE;
143 fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
144 fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID,
145 dwBytes: SIZE_T) -> LPVOID;
146 fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
150 struct Header(*mut u8);
152 const HEAP_REALLOC_IN_PLACE_ONLY: DWORD = 0x00000010;
154 unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
155 &mut *(ptr as *mut Header).offset(-1)
158 unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
159 let aligned = ptr.offset((align - (ptr as usize & (align - 1))) as isize);
160 *get_header(aligned) = Header(ptr);
164 pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
165 if align <= MIN_ALIGN {
166 HeapAlloc(GetProcessHeap(), 0, size as SIZE_T) as *mut u8
168 let ptr = HeapAlloc(GetProcessHeap(), 0,
169 (size + align) as SIZE_T) as *mut u8;
170 if ptr.is_null() { return ptr }
171 align_ptr(ptr, align)
175 pub unsafe fn reallocate(ptr: *mut u8, _old_size: usize, size: usize,
176 align: usize) -> *mut u8 {
177 if align <= MIN_ALIGN {
178 HeapReAlloc(GetProcessHeap(), 0, ptr as LPVOID, size as SIZE_T) as *mut u8
180 let header = get_header(ptr);
181 let new = HeapReAlloc(GetProcessHeap(), 0, header.0 as LPVOID,
182 (size + align) as SIZE_T) as *mut u8;
183 if new.is_null() { return new }
184 align_ptr(new, align)
188 pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: usize, size: usize,
189 align: usize) -> usize {
190 if align <= MIN_ALIGN {
191 let new = HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY,
192 ptr as LPVOID, size as SIZE_T) as *mut u8;
193 if new.is_null() { old_size } else { size }
199 pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, align: usize) {
200 if align <= MIN_ALIGN {
201 let err = HeapFree(GetProcessHeap(), 0, ptr as LPVOID);
202 debug_assert!(err != 0);
204 let header = get_header(ptr);
205 let err = HeapFree(GetProcessHeap(), 0, header.0 as LPVOID);
206 debug_assert!(err != 0);
210 pub fn usable_size(size: usize, _align: usize) -> usize {