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