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