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