]> git.lizzy.rs Git - rust.git/blob - src/liballoc_system/lib.rs
Auto merge of #27827 - w00ns:for-loop-expn-issue-27639, 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 #![cfg_attr(stage0, feature(custom_attribute))]
12 #![crate_name = "alloc_system"]
13 #![crate_type = "rlib"]
14 #![staged_api]
15 #![no_std]
16 #![cfg_attr(not(stage0), allocator)]
17 #![unstable(feature = "alloc_system",
18             reason = "this library is unlikely to be stabilized in its current \
19                       form or name")]
20 #![feature(allocator)]
21 #![feature(libc)]
22 #![feature(no_std)]
23 #![feature(staged_api)]
24
25 extern crate libc;
26
27 // The minimum alignment guaranteed by the architecture. This value is used to
28 // add fast paths for low alignment values. In practice, the alignment is a
29 // constant at the call site and the branch will be optimized out.
30 #[cfg(all(any(target_arch = "arm",
31               target_arch = "mips",
32               target_arch = "mipsel",
33               target_arch = "powerpc")))]
34 const MIN_ALIGN: usize = 8;
35 #[cfg(all(any(target_arch = "x86",
36               target_arch = "x86_64",
37               target_arch = "aarch64")))]
38 const MIN_ALIGN: usize = 16;
39
40 #[no_mangle]
41 pub extern fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
42     unsafe { imp::allocate(size, align) }
43 }
44
45 #[no_mangle]
46 pub extern fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
47     unsafe { imp::deallocate(ptr, old_size, align) }
48 }
49
50 #[no_mangle]
51 pub extern fn __rust_reallocate(ptr: *mut u8, old_size: usize, size: usize,
52                                 align: usize) -> *mut u8 {
53     unsafe { imp::reallocate(ptr, old_size, size, align) }
54 }
55
56 #[no_mangle]
57 pub extern fn __rust_reallocate_inplace(ptr: *mut u8, old_size: usize,
58                                         size: usize, align: usize) -> usize {
59     unsafe { imp::reallocate_inplace(ptr, old_size, size, align) }
60 }
61
62 #[no_mangle]
63 pub extern fn __rust_usable_size(size: usize, align: usize) -> usize {
64     imp::usable_size(size, align)
65 }
66
67 #[cfg(unix)]
68 mod imp {
69     use core::cmp;
70     use core::ptr;
71     use libc;
72     use MIN_ALIGN;
73
74     extern {
75         // Apparently android doesn't have posix_memalign
76         #[cfg(target_os = "android")]
77         fn memalign(align: libc::size_t, size: libc::size_t) -> *mut libc::c_void;
78
79         #[cfg(not(target_os = "android"))]
80         fn posix_memalign(memptr: *mut *mut libc::c_void,
81                           align: libc::size_t,
82                           size: libc::size_t) -> libc::c_int;
83     }
84
85     pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
86         if align <= MIN_ALIGN {
87             libc::malloc(size as libc::size_t) as *mut u8
88         } else {
89             #[cfg(target_os = "android")]
90             unsafe fn more_aligned_malloc(size: usize, align: usize) -> *mut u8 {
91                 memalign(align as libc::size_t, size as libc::size_t) as *mut u8
92             }
93             #[cfg(not(target_os = "android"))]
94             unsafe fn more_aligned_malloc(size: usize, align: usize) -> *mut u8 {
95                 let mut out = ptr::null_mut();
96                 let ret = posix_memalign(&mut out,
97                                          align as libc::size_t,
98                                          size as libc::size_t);
99                 if ret != 0 {
100                     ptr::null_mut()
101                 } else {
102                     out as *mut u8
103                 }
104             }
105             more_aligned_malloc(size, align)
106         }
107     }
108
109     pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize,
110                              align: usize) -> *mut u8 {
111         if align <= MIN_ALIGN {
112             libc::realloc(ptr as *mut libc::c_void, size as libc::size_t) as *mut u8
113         } else {
114             let new_ptr = allocate(size, align);
115             ptr::copy(ptr, new_ptr, cmp::min(size, old_size));
116             deallocate(ptr, old_size, align);
117             new_ptr
118         }
119     }
120
121     pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: usize, _size: usize,
122                                      _align: usize) -> usize {
123         old_size
124     }
125
126     pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, _align: usize) {
127         libc::free(ptr as *mut libc::c_void)
128     }
129
130     pub fn usable_size(size: usize, _align: usize) -> usize {
131         size
132     }
133 }
134
135 #[cfg(windows)]
136 mod imp {
137     use libc::{BOOL, DWORD, HANDLE, LPVOID, SIZE_T};
138     use MIN_ALIGN;
139
140     extern "system" {
141         fn GetProcessHeap() -> HANDLE;
142         fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
143         fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID,
144                        dwBytes: SIZE_T) -> LPVOID;
145         fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
146     }
147
148     #[repr(C)]
149     struct Header(*mut u8);
150
151     const HEAP_REALLOC_IN_PLACE_ONLY: DWORD = 0x00000010;
152
153     unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
154         &mut *(ptr as *mut Header).offset(-1)
155     }
156
157     unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
158         let aligned = ptr.offset((align - (ptr as usize & (align - 1))) as isize);
159         *get_header(aligned) = Header(ptr);
160         aligned
161     }
162
163     pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
164         if align <= MIN_ALIGN {
165             HeapAlloc(GetProcessHeap(), 0, size as SIZE_T) as *mut u8
166         } else {
167             let ptr = HeapAlloc(GetProcessHeap(), 0,
168                                 (size + align) as SIZE_T) as *mut u8;
169             if ptr.is_null() { return ptr }
170             align_ptr(ptr, align)
171         }
172     }
173
174     pub unsafe fn reallocate(ptr: *mut u8, _old_size: usize, size: usize,
175                              align: usize) -> *mut u8 {
176         if align <= MIN_ALIGN {
177             HeapReAlloc(GetProcessHeap(), 0, ptr as LPVOID, size as SIZE_T) as *mut u8
178         } else {
179             let header = get_header(ptr);
180             let new = HeapReAlloc(GetProcessHeap(), 0, header.0 as LPVOID,
181                                   (size + align) as SIZE_T) as *mut u8;
182             if new.is_null() { return new }
183             align_ptr(new, align)
184         }
185     }
186
187     pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: usize, size: usize,
188                                      align: usize) -> usize {
189         if align <= MIN_ALIGN {
190             let new = HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY,
191                                   ptr as LPVOID, size as SIZE_T) as *mut u8;
192             if new.is_null() { old_size } else { size }
193         } else {
194             old_size
195         }
196     }
197
198     pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, align: usize) {
199         if align <= MIN_ALIGN {
200             let err = HeapFree(GetProcessHeap(), 0, ptr as LPVOID);
201             debug_assert!(err != 0);
202         } else {
203             let header = get_header(ptr);
204             let err = HeapFree(GetProcessHeap(), 0, header.0 as LPVOID);
205             debug_assert!(err != 0);
206         }
207     }
208
209     pub fn usable_size(size: usize, _align: usize) -> usize {
210         size
211     }
212 }