1 // Copyright 2017 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 pub unsafe extern fn __rust_alloc(size: usize,
14 err: *mut u8) -> *mut u8 {
15 let p = imp::allocate(size, align);
23 pub unsafe extern fn __rust_oom(_err: *const u8) -> ! {
24 ::core::intrinsics::abort()
28 pub unsafe extern fn __rust_dealloc(ptr: *mut u8,
31 imp::deallocate(ptr, size, align)
35 pub unsafe extern fn __rust_usable_size(size: usize,
44 pub unsafe extern fn __rust_realloc(ptr: *mut u8,
49 err: *mut u8) -> *mut u8 {
50 if new_align != old_align {
53 let p = imp::reallocate(ptr, old_size, new_size, new_align);
61 pub unsafe extern fn __rust_alloc_zeroed(size: usize,
63 err: *mut u8) -> *mut u8 {
64 let p = imp::allocate_zeroed(size, align);
72 pub unsafe extern fn __rust_alloc_excess(_size: usize,
75 err: *mut u8) -> *mut u8 {
80 pub unsafe extern fn __rust_realloc_excess(_ptr: *mut u8,
86 err: *mut u8) -> *mut u8 {
91 pub unsafe extern fn __rust_grow_in_place(_ptr: *mut u8,
95 _new_align: usize) -> u8 {
100 pub unsafe extern fn __rust_shrink_in_place(_ptr: *mut u8,
104 _new_align: usize) -> u8 {
108 #[cfg(any(unix, target_os = "redox"))]
116 pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
117 if align <= MIN_ALIGN {
118 libc::malloc(size as libc::size_t) as *mut u8
120 aligned_malloc(size, align)
124 #[cfg(any(target_os = "android", target_os = "redox"))]
125 unsafe fn aligned_malloc(size: usize, align: usize) -> *mut u8 {
126 // On android we currently target API level 9 which unfortunately
127 // doesn't have the `posix_memalign` API used below. Instead we use
128 // `memalign`, but this unfortunately has the property on some systems
129 // where the memory returned cannot be deallocated by `free`!
131 // Upon closer inspection, however, this appears to work just fine with
132 // Android, so for this platform we should be fine to call `memalign`
133 // (which is present in API level 9). Some helpful references could
134 // possibly be chromium using memalign [1], attempts at documenting that
135 // memalign + free is ok [2] [3], or the current source of chromium
136 // which still uses memalign on android [4].
138 // [1]: https://codereview.chromium.org/10796020/
139 // [2]: https://code.google.com/p/android/issues/detail?id=35391
140 // [3]: https://bugs.chromium.org/p/chromium/issues/detail?id=138579
141 // [4]: https://chromium.googlesource.com/chromium/src/base/+/master/
142 // /memory/aligned_memory.cc
143 libc::memalign(align as libc::size_t, size as libc::size_t) as *mut u8
146 #[cfg(not(any(target_os = "android", target_os = "redox")))]
147 unsafe fn aligned_malloc(size: usize, align: usize) -> *mut u8 {
148 let mut out = ptr::null_mut();
149 let ret = libc::posix_memalign(&mut out, align as libc::size_t, size as libc::size_t);
157 pub unsafe fn allocate_zeroed(size: usize, align: usize) -> *mut u8 {
158 if align <= MIN_ALIGN {
159 libc::calloc(size as libc::size_t, 1) as *mut u8
161 let ptr = aligned_malloc(size, align);
163 ptr::write_bytes(ptr, 0, size);
169 pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 {
170 if align <= MIN_ALIGN {
171 libc::realloc(ptr as *mut libc::c_void, size as libc::size_t) as *mut u8
173 let new_ptr = allocate(size, align);
174 if !new_ptr.is_null() {
175 ptr::copy(ptr, new_ptr, cmp::min(size, old_size));
176 deallocate(ptr, old_size, align);
182 pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, _align: usize) {
183 libc::free(ptr as *mut libc::c_void)
191 use core::ptr::copy_nonoverlapping;
194 type LPVOID = *mut u8;
195 type HANDLE = LPVOID;
201 fn GetProcessHeap() -> HANDLE;
202 fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
203 fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID, dwBytes: SIZE_T) -> LPVOID;
204 fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
205 fn GetLastError() -> DWORD;
209 struct Header(*mut u8);
212 const HEAP_ZERO_MEMORY: DWORD = 0x00000008;
214 unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
215 &mut *(ptr as *mut Header).offset(-1)
218 unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
219 let aligned = ptr.offset((align - (ptr as usize & (align - 1))) as isize);
220 *get_header(aligned) = Header(ptr);
225 unsafe fn allocate_with_flags(size: usize, align: usize, flags: DWORD) -> *mut u8 {
226 if align <= MIN_ALIGN {
227 HeapAlloc(GetProcessHeap(), flags, size as SIZE_T) as *mut u8
229 let ptr = HeapAlloc(GetProcessHeap(), flags, (size + align) as SIZE_T) as *mut u8;
233 align_ptr(ptr, align)
237 pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
238 allocate_with_flags(size, align, 0)
241 pub unsafe fn allocate_zeroed(size: usize, align: usize) -> *mut u8 {
242 allocate_with_flags(size, align, HEAP_ZERO_MEMORY)
245 pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 {
246 if align <= MIN_ALIGN {
247 HeapReAlloc(GetProcessHeap(), 0, ptr as LPVOID, size as SIZE_T) as *mut u8
249 let new = allocate(size, align);
251 copy_nonoverlapping(ptr, new, min(size, old_size));
252 deallocate(ptr, old_size, align);
258 pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, align: usize) {
259 if align <= MIN_ALIGN {
260 let err = HeapFree(GetProcessHeap(), 0, ptr as LPVOID);
261 debug_assert!(err != 0, "Failed to free heap memory: {}", GetLastError());
263 let header = get_header(ptr);
264 let err = HeapFree(GetProcessHeap(), 0, header.0 as LPVOID);
265 debug_assert!(err != 0, "Failed to free heap memory: {}", GetLastError());