]> git.lizzy.rs Git - rust.git/blob - src/liballoc_system/lib.rs
Rollup merge of #36190 - 0xmohit:pr/issue-31216, r=alexcrichton
[rust.git] / src / liballoc_system / lib.rs
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.
4 //
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.
10
11 #![crate_name = "alloc_system"]
12 #![crate_type = "rlib"]
13 #![no_std]
14 #![allocator]
15 #![cfg_attr(not(stage0), deny(warnings))]
16 #![unstable(feature = "alloc_system",
17             reason = "this library is unlikely to be stabilized in its current \
18                       form or name",
19             issue = "27783")]
20 #![feature(allocator)]
21 #![feature(staged_api)]
22 #![cfg_attr(unix, feature(libc))]
23
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",
28               target_arch = "arm",
29               target_arch = "mips",
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               target_arch = "mips64")))]
37 const MIN_ALIGN: usize = 16;
38
39 #[no_mangle]
40 pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
41     unsafe { imp::allocate(size, align) }
42 }
43
44 #[no_mangle]
45 pub extern "C" fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
46     unsafe { imp::deallocate(ptr, old_size, align) }
47 }
48
49 #[no_mangle]
50 pub extern "C" fn __rust_reallocate(ptr: *mut u8,
51                                     old_size: usize,
52                                     size: usize,
53                                     align: usize)
54                                     -> *mut u8 {
55     unsafe { imp::reallocate(ptr, old_size, size, align) }
56 }
57
58 #[no_mangle]
59 pub extern "C" fn __rust_reallocate_inplace(ptr: *mut u8,
60                                             old_size: usize,
61                                             size: usize,
62                                             align: usize)
63                                             -> usize {
64     unsafe { imp::reallocate_inplace(ptr, old_size, size, align) }
65 }
66
67 #[no_mangle]
68 pub extern "C" fn __rust_usable_size(size: usize, align: usize) -> usize {
69     imp::usable_size(size, align)
70 }
71
72 #[cfg(unix)]
73 mod imp {
74     extern crate libc;
75
76     use core::cmp;
77     use core::ptr;
78     use MIN_ALIGN;
79
80     pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
81         if align <= MIN_ALIGN {
82             libc::malloc(size as libc::size_t) as *mut u8
83         } else {
84             aligned_malloc(size, align)
85         }
86     }
87
88     #[cfg(target_os = "android")]
89     unsafe fn aligned_malloc(size: usize, align: usize) -> *mut u8 {
90         // On android we currently target API level 9 which unfortunately
91         // doesn't have the `posix_memalign` API used below. Instead we use
92         // `memalign`, but this unfortunately has the property on some systems
93         // where the memory returned cannot be deallocated by `free`!
94         //
95         // Upon closer inspection, however, this appears to work just fine with
96         // Android, so for this platform we should be fine to call `memalign`
97         // (which is present in API level 9). Some helpful references could
98         // possibly be chromium using memalign [1], attempts at documenting that
99         // memalign + free is ok [2] [3], or the current source of chromium
100         // which still uses memalign on android [4].
101         //
102         // [1]: https://codereview.chromium.org/10796020/
103         // [2]: https://code.google.com/p/android/issues/detail?id=35391
104         // [3]: https://bugs.chromium.org/p/chromium/issues/detail?id=138579
105         // [4]: https://chromium.googlesource.com/chromium/src/base/+/master/
106         //                                       /memory/aligned_memory.cc
107         libc::memalign(align as libc::size_t, size as libc::size_t) as *mut u8
108     }
109
110     #[cfg(not(target_os = "android"))]
111     unsafe fn aligned_malloc(size: usize, align: usize) -> *mut u8 {
112         let mut out = ptr::null_mut();
113         let ret = libc::posix_memalign(&mut out, align as libc::size_t, size as libc::size_t);
114         if ret != 0 {
115             ptr::null_mut()
116         } else {
117             out as *mut u8
118         }
119     }
120
121     pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 {
122         if align <= MIN_ALIGN {
123             libc::realloc(ptr as *mut libc::c_void, size as libc::size_t) as *mut u8
124         } else {
125             let new_ptr = allocate(size, align);
126             if !new_ptr.is_null() {
127                 ptr::copy(ptr, new_ptr, cmp::min(size, old_size));
128                 deallocate(ptr, old_size, align);
129             }
130             new_ptr
131         }
132     }
133
134     pub unsafe fn reallocate_inplace(_ptr: *mut u8,
135                                      old_size: usize,
136                                      _size: usize,
137                                      _align: usize)
138                                      -> usize {
139         old_size
140     }
141
142     pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, _align: usize) {
143         libc::free(ptr as *mut libc::c_void)
144     }
145
146     pub fn usable_size(size: usize, _align: usize) -> usize {
147         size
148     }
149 }
150
151 #[cfg(windows)]
152 #[allow(bad_style)]
153 mod imp {
154     use MIN_ALIGN;
155
156     type LPVOID = *mut u8;
157     type HANDLE = LPVOID;
158     type SIZE_T = usize;
159     type DWORD = u32;
160     type BOOL = i32;
161
162     extern "system" {
163         fn GetProcessHeap() -> HANDLE;
164         fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
165         fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID, dwBytes: SIZE_T) -> LPVOID;
166         fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
167     }
168
169     #[repr(C)]
170     struct Header(*mut u8);
171
172     const HEAP_REALLOC_IN_PLACE_ONLY: DWORD = 0x00000010;
173
174     unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
175         &mut *(ptr as *mut Header).offset(-1)
176     }
177
178     unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
179         let aligned = ptr.offset((align - (ptr as usize & (align - 1))) as isize);
180         *get_header(aligned) = Header(ptr);
181         aligned
182     }
183
184     pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
185         if align <= MIN_ALIGN {
186             HeapAlloc(GetProcessHeap(), 0, size as SIZE_T) as *mut u8
187         } else {
188             let ptr = HeapAlloc(GetProcessHeap(), 0, (size + align) as SIZE_T) as *mut u8;
189             if ptr.is_null() {
190                 return ptr;
191             }
192             align_ptr(ptr, align)
193         }
194     }
195
196     pub unsafe fn reallocate(ptr: *mut u8, _old_size: usize, size: usize, align: usize) -> *mut u8 {
197         if align <= MIN_ALIGN {
198             HeapReAlloc(GetProcessHeap(), 0, ptr as LPVOID, size as SIZE_T) as *mut u8
199         } else {
200             let header = get_header(ptr);
201             let new = HeapReAlloc(GetProcessHeap(),
202                                   0,
203                                   header.0 as LPVOID,
204                                   (size + align) as SIZE_T) as *mut u8;
205             if new.is_null() {
206                 return new;
207             }
208             align_ptr(new, align)
209         }
210     }
211
212     pub unsafe fn reallocate_inplace(ptr: *mut u8,
213                                      old_size: usize,
214                                      size: usize,
215                                      align: usize)
216                                      -> usize {
217         if align <= MIN_ALIGN {
218             let new = HeapReAlloc(GetProcessHeap(),
219                                   HEAP_REALLOC_IN_PLACE_ONLY,
220                                   ptr as LPVOID,
221                                   size as SIZE_T) as *mut u8;
222             if new.is_null() {
223                 old_size
224             } else {
225                 size
226             }
227         } else {
228             old_size
229         }
230     }
231
232     pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, align: usize) {
233         if align <= MIN_ALIGN {
234             let err = HeapFree(GetProcessHeap(), 0, ptr as LPVOID);
235             debug_assert!(err != 0);
236         } else {
237             let header = get_header(ptr);
238             let err = HeapFree(GetProcessHeap(), 0, header.0 as LPVOID);
239             debug_assert!(err != 0);
240         }
241     }
242
243     pub fn usable_size(size: usize, _align: usize) -> usize {
244         size
245     }
246 }