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