]> git.lizzy.rs Git - rust.git/blob - src/liballoc_system/lib.rs
Auto merge of #43459 - ids1024:asrawfd, 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 #![deny(warnings)]
15 #![unstable(feature = "alloc_system",
16             reason = "this library is unlikely to be stabilized in its current \
17                       form or name",
18             issue = "27783")]
19 #![feature(global_allocator)]
20 #![feature(allocator_api)]
21 #![feature(alloc)]
22 #![feature(core_intrinsics)]
23 #![feature(staged_api)]
24 #![cfg_attr(any(unix, target_os = "redox"), feature(libc))]
25
26 // The minimum alignment guaranteed by the architecture. This value is used to
27 // add fast paths for low alignment values. In practice, the alignment is a
28 // constant at the call site and the branch will be optimized out.
29 #[cfg(all(any(target_arch = "x86",
30               target_arch = "arm",
31               target_arch = "mips",
32               target_arch = "powerpc",
33               target_arch = "powerpc64",
34               target_arch = "asmjs",
35               target_arch = "wasm32")))]
36 const MIN_ALIGN: usize = 8;
37 #[cfg(all(any(target_arch = "x86_64",
38               target_arch = "aarch64",
39               target_arch = "mips64",
40               target_arch = "s390x",
41               target_arch = "sparc64")))]
42 const MIN_ALIGN: usize = 16;
43
44 extern crate alloc;
45
46 use self::alloc::heap::{Alloc, AllocErr, Layout, Excess, CannotReallocInPlace};
47
48 #[unstable(feature = "allocator_api", issue = "32838")]
49 pub struct System;
50
51 #[unstable(feature = "allocator_api", issue = "32838")]
52 unsafe impl Alloc for System {
53     #[inline]
54     unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
55         (&*self).alloc(layout)
56     }
57
58     #[inline]
59     unsafe fn alloc_zeroed(&mut self, layout: Layout)
60         -> Result<*mut u8, AllocErr>
61     {
62         (&*self).alloc_zeroed(layout)
63     }
64
65     #[inline]
66     unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
67         (&*self).dealloc(ptr, layout)
68     }
69
70     #[inline]
71     unsafe fn realloc(&mut self,
72                       ptr: *mut u8,
73                       old_layout: Layout,
74                       new_layout: Layout) -> Result<*mut u8, AllocErr> {
75         (&*self).realloc(ptr, old_layout, new_layout)
76     }
77
78     fn oom(&mut self, err: AllocErr) -> ! {
79         (&*self).oom(err)
80     }
81
82     #[inline]
83     fn usable_size(&self, layout: &Layout) -> (usize, usize) {
84         (&self).usable_size(layout)
85     }
86
87     #[inline]
88     unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr> {
89         (&*self).alloc_excess(layout)
90     }
91
92     #[inline]
93     unsafe fn realloc_excess(&mut self,
94                              ptr: *mut u8,
95                              layout: Layout,
96                              new_layout: Layout) -> Result<Excess, AllocErr> {
97         (&*self).realloc_excess(ptr, layout, new_layout)
98     }
99
100     #[inline]
101     unsafe fn grow_in_place(&mut self,
102                             ptr: *mut u8,
103                             layout: Layout,
104                             new_layout: Layout) -> Result<(), CannotReallocInPlace> {
105         (&*self).grow_in_place(ptr, layout, new_layout)
106     }
107
108     #[inline]
109     unsafe fn shrink_in_place(&mut self,
110                               ptr: *mut u8,
111                               layout: Layout,
112                               new_layout: Layout) -> Result<(), CannotReallocInPlace> {
113         (&*self).shrink_in_place(ptr, layout, new_layout)
114     }
115 }
116
117 #[cfg(any(unix, target_os = "redox"))]
118 mod platform {
119     extern crate libc;
120
121     use core::cmp;
122     use core::ptr;
123
124     use MIN_ALIGN;
125     use System;
126     use alloc::heap::{Alloc, AllocErr, Layout};
127
128     #[unstable(feature = "allocator_api", issue = "32838")]
129     unsafe impl<'a> Alloc for &'a System {
130         #[inline]
131         unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
132             let ptr = if layout.align() <= MIN_ALIGN {
133                 libc::malloc(layout.size()) as *mut u8
134             } else {
135                 aligned_malloc(&layout)
136             };
137             if !ptr.is_null() {
138                 Ok(ptr)
139             } else {
140                 Err(AllocErr::Exhausted { request: layout })
141             }
142         }
143
144         #[inline]
145         unsafe fn alloc_zeroed(&mut self, layout: Layout)
146             -> Result<*mut u8, AllocErr>
147         {
148             if layout.align() <= MIN_ALIGN {
149                 let ptr = libc::calloc(layout.size(), 1) as *mut u8;
150                 if !ptr.is_null() {
151                     Ok(ptr)
152                 } else {
153                     Err(AllocErr::Exhausted { request: layout })
154                 }
155             } else {
156                 let ret = self.alloc(layout.clone());
157                 if let Ok(ptr) = ret {
158                     ptr::write_bytes(ptr, 0, layout.size());
159                 }
160                 ret
161             }
162         }
163
164         #[inline]
165         unsafe fn dealloc(&mut self, ptr: *mut u8, _layout: Layout) {
166             libc::free(ptr as *mut libc::c_void)
167         }
168
169         #[inline]
170         unsafe fn realloc(&mut self,
171                           ptr: *mut u8,
172                           old_layout: Layout,
173                           new_layout: Layout) -> Result<*mut u8, AllocErr> {
174             if old_layout.align() != new_layout.align() {
175                 return Err(AllocErr::Unsupported {
176                     details: "cannot change alignment on `realloc`",
177                 })
178             }
179
180             if new_layout.align() <= MIN_ALIGN {
181                 let ptr = libc::realloc(ptr as *mut libc::c_void, new_layout.size());
182                 if !ptr.is_null() {
183                     Ok(ptr as *mut u8)
184                 } else {
185                     Err(AllocErr::Exhausted { request: new_layout })
186                 }
187             } else {
188                 let res = self.alloc(new_layout.clone());
189                 if let Ok(new_ptr) = res {
190                     let size = cmp::min(old_layout.size(), new_layout.size());
191                     ptr::copy_nonoverlapping(ptr, new_ptr, size);
192                     self.dealloc(ptr, old_layout);
193                 }
194                 res
195             }
196         }
197
198         fn oom(&mut self, err: AllocErr) -> ! {
199             use core::fmt::{self, Write};
200
201             // Print a message to stderr before aborting to assist with
202             // debugging. It is critical that this code does not allocate any
203             // memory since we are in an OOM situation. Any errors are ignored
204             // while printing since there's nothing we can do about them and we
205             // are about to exit anyways.
206             drop(writeln!(Stderr, "fatal runtime error: {}", err));
207             unsafe {
208                 ::core::intrinsics::abort();
209             }
210
211             struct Stderr;
212
213             impl Write for Stderr {
214                 fn write_str(&mut self, s: &str) -> fmt::Result {
215                     unsafe {
216                         libc::write(libc::STDERR_FILENO,
217                                     s.as_ptr() as *const libc::c_void,
218                                     s.len());
219                     }
220                     Ok(())
221                 }
222             }
223         }
224     }
225
226     #[cfg(any(target_os = "android", target_os = "redox"))]
227     #[inline]
228     unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
229         // On android we currently target API level 9 which unfortunately
230         // doesn't have the `posix_memalign` API used below. Instead we use
231         // `memalign`, but this unfortunately has the property on some systems
232         // where the memory returned cannot be deallocated by `free`!
233         //
234         // Upon closer inspection, however, this appears to work just fine with
235         // Android, so for this platform we should be fine to call `memalign`
236         // (which is present in API level 9). Some helpful references could
237         // possibly be chromium using memalign [1], attempts at documenting that
238         // memalign + free is ok [2] [3], or the current source of chromium
239         // which still uses memalign on android [4].
240         //
241         // [1]: https://codereview.chromium.org/10796020/
242         // [2]: https://code.google.com/p/android/issues/detail?id=35391
243         // [3]: https://bugs.chromium.org/p/chromium/issues/detail?id=138579
244         // [4]: https://chromium.googlesource.com/chromium/src/base/+/master/
245         //                                       /memory/aligned_memory.cc
246         libc::memalign(layout.align(), layout.size()) as *mut u8
247     }
248
249     #[cfg(not(any(target_os = "android", target_os = "redox")))]
250     #[inline]
251     unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
252         let mut out = ptr::null_mut();
253         let ret = libc::posix_memalign(&mut out, layout.align(), layout.size());
254         if ret != 0 {
255             ptr::null_mut()
256         } else {
257             out as *mut u8
258         }
259     }
260 }
261
262 #[cfg(windows)]
263 #[allow(bad_style)]
264 mod platform {
265     use core::cmp;
266     use core::ptr;
267
268     use MIN_ALIGN;
269     use System;
270     use alloc::heap::{Alloc, AllocErr, Layout, CannotReallocInPlace};
271
272     type LPVOID = *mut u8;
273     type HANDLE = LPVOID;
274     type SIZE_T = usize;
275     type DWORD = u32;
276     type BOOL = i32;
277     type LPDWORD = *mut DWORD;
278     type LPOVERLAPPED = *mut u8;
279
280     const STD_ERROR_HANDLE: DWORD = -12i32 as DWORD;
281
282     extern "system" {
283         fn GetProcessHeap() -> HANDLE;
284         fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
285         fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID, dwBytes: SIZE_T) -> LPVOID;
286         fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
287         fn GetLastError() -> DWORD;
288         fn WriteFile(hFile: HANDLE,
289                      lpBuffer: LPVOID,
290                      nNumberOfBytesToWrite: DWORD,
291                      lpNumberOfBytesWritten: LPDWORD,
292                      lpOverlapped: LPOVERLAPPED)
293                      -> BOOL;
294         fn GetStdHandle(which: DWORD) -> HANDLE;
295     }
296
297     #[repr(C)]
298     struct Header(*mut u8);
299
300     const HEAP_ZERO_MEMORY: DWORD = 0x00000008;
301     const HEAP_REALLOC_IN_PLACE_ONLY: DWORD = 0x00000010;
302
303     unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
304         &mut *(ptr as *mut Header).offset(-1)
305     }
306
307     unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
308         let aligned = ptr.offset((align - (ptr as usize & (align - 1))) as isize);
309         *get_header(aligned) = Header(ptr);
310         aligned
311     }
312
313     #[inline]
314     unsafe fn allocate_with_flags(layout: Layout, flags: DWORD)
315         -> Result<*mut u8, AllocErr>
316     {
317         let ptr = if layout.align() <= MIN_ALIGN {
318             HeapAlloc(GetProcessHeap(), flags, layout.size())
319         } else {
320             let size = layout.size() + layout.align();
321             let ptr = HeapAlloc(GetProcessHeap(), flags, size);
322             if ptr.is_null() {
323                 ptr
324             } else {
325                 align_ptr(ptr, layout.align())
326             }
327         };
328         if ptr.is_null() {
329             Err(AllocErr::Exhausted { request: layout })
330         } else {
331             Ok(ptr as *mut u8)
332         }
333     }
334
335     #[unstable(feature = "allocator_api", issue = "32838")]
336     unsafe impl<'a> Alloc for &'a System {
337         #[inline]
338         unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
339             allocate_with_flags(layout, 0)
340         }
341
342         #[inline]
343         unsafe fn alloc_zeroed(&mut self, layout: Layout)
344             -> Result<*mut u8, AllocErr>
345         {
346             allocate_with_flags(layout, HEAP_ZERO_MEMORY)
347         }
348
349         #[inline]
350         unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
351             if layout.align() <= MIN_ALIGN {
352                 let err = HeapFree(GetProcessHeap(), 0, ptr as LPVOID);
353                 debug_assert!(err != 0, "Failed to free heap memory: {}",
354                               GetLastError());
355             } else {
356                 let header = get_header(ptr);
357                 let err = HeapFree(GetProcessHeap(), 0, header.0 as LPVOID);
358                 debug_assert!(err != 0, "Failed to free heap memory: {}",
359                               GetLastError());
360             }
361         }
362
363         #[inline]
364         unsafe fn realloc(&mut self,
365                           ptr: *mut u8,
366                           old_layout: Layout,
367                           new_layout: Layout) -> Result<*mut u8, AllocErr> {
368             if old_layout.align() != new_layout.align() {
369                 return Err(AllocErr::Unsupported {
370                     details: "cannot change alignment on `realloc`",
371                 })
372             }
373
374             if new_layout.align() <= MIN_ALIGN {
375                 let ptr = HeapReAlloc(GetProcessHeap(),
376                                       0,
377                                       ptr as LPVOID,
378                                       new_layout.size());
379                 if !ptr.is_null() {
380                     Ok(ptr as *mut u8)
381                 } else {
382                     Err(AllocErr::Exhausted { request: new_layout })
383                 }
384             } else {
385                 let res = self.alloc(new_layout.clone());
386                 if let Ok(new_ptr) = res {
387                     let size = cmp::min(old_layout.size(), new_layout.size());
388                     ptr::copy_nonoverlapping(ptr, new_ptr, size);
389                     self.dealloc(ptr, old_layout);
390                 }
391                 res
392             }
393         }
394
395         #[inline]
396         unsafe fn grow_in_place(&mut self,
397                                 ptr: *mut u8,
398                                 layout: Layout,
399                                 new_layout: Layout) -> Result<(), CannotReallocInPlace> {
400             self.shrink_in_place(ptr, layout, new_layout)
401         }
402
403         #[inline]
404         unsafe fn shrink_in_place(&mut self,
405                                   ptr: *mut u8,
406                                   old_layout: Layout,
407                                   new_layout: Layout) -> Result<(), CannotReallocInPlace> {
408             if old_layout.align() != new_layout.align() {
409                 return Err(CannotReallocInPlace)
410             }
411
412             let new = if new_layout.align() <= MIN_ALIGN {
413                 HeapReAlloc(GetProcessHeap(),
414                             HEAP_REALLOC_IN_PLACE_ONLY,
415                             ptr as LPVOID,
416                             new_layout.size())
417             } else {
418                 let header = get_header(ptr);
419                 HeapReAlloc(GetProcessHeap(),
420                             HEAP_REALLOC_IN_PLACE_ONLY,
421                             header.0 as LPVOID,
422                             new_layout.size() + new_layout.align())
423             };
424             if new.is_null() {
425                 Err(CannotReallocInPlace)
426             } else {
427                 Ok(())
428             }
429         }
430
431         fn oom(&mut self, err: AllocErr) -> ! {
432             use core::fmt::{self, Write};
433
434             // Same as with unix we ignore all errors here
435             drop(writeln!(Stderr, "fatal runtime error: {}", err));
436             unsafe {
437                 ::core::intrinsics::abort();
438             }
439
440             struct Stderr;
441
442             impl Write for Stderr {
443                 fn write_str(&mut self, s: &str) -> fmt::Result {
444                     unsafe {
445                         // WriteFile silently fails if it is passed an invalid
446                         // handle, so there is no need to check the result of
447                         // GetStdHandle.
448                         WriteFile(GetStdHandle(STD_ERROR_HANDLE),
449                                   s.as_ptr() as LPVOID,
450                                   s.len() as DWORD,
451                                   ptr::null_mut(),
452                                   ptr::null_mut());
453                     }
454                     Ok(())
455                 }
456             }
457         }
458     }
459 }