]> git.lizzy.rs Git - rust.git/blob - src/liballoc_system/lib.rs
rustfmt: liballoc, liballoc_*, libarena
[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 #![cfg_attr(stage0, allow(improper_ctypes))]
18 #![unstable(feature = "alloc_system",
19             reason = "this library is unlikely to be stabilized in its current \
20                       form or name",
21             issue = "27783")]
22 #![feature(allocator)]
23 #![feature(libc)]
24 #![feature(no_std)]
25 #![feature(staged_api)]
26
27 extern crate libc;
28
29 // The minimum alignment guaranteed by the architecture. This value is used to
30 // add fast paths for low alignment values. In practice, the alignment is a
31 // constant at the call site and the branch will be optimized out.
32 #[cfg(all(any(target_arch = "arm",
33               target_arch = "mips",
34               target_arch = "mipsel",
35               target_arch = "powerpc")))]
36 const MIN_ALIGN: usize = 8;
37 #[cfg(all(any(target_arch = "x86",
38               target_arch = "x86_64",
39               target_arch = "aarch64")))]
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(unix)]
76 mod imp {
77     use core::cmp;
78     use core::ptr;
79     use libc;
80     use MIN_ALIGN;
81
82     extern "C" {
83         // Apparently android doesn't have posix_memalign
84         #[cfg(target_os = "android")]
85         fn memalign(align: libc::size_t, size: libc::size_t) -> *mut libc::c_void;
86
87         #[cfg(not(target_os = "android"))]
88         fn posix_memalign(memptr: *mut *mut libc::c_void,
89                           align: libc::size_t,
90                           size: libc::size_t)
91                           -> libc::c_int;
92     }
93
94     pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
95         if align <= MIN_ALIGN {
96             libc::malloc(size as libc::size_t) as *mut u8
97         } else {
98             #[cfg(target_os = "android")]
99             unsafe fn more_aligned_malloc(size: usize, align: usize) -> *mut u8 {
100                 memalign(align as libc::size_t, size as libc::size_t) as *mut u8
101             }
102             #[cfg(not(target_os = "android"))]
103             unsafe fn more_aligned_malloc(size: usize, align: usize) -> *mut u8 {
104                 let mut out = ptr::null_mut();
105                 let ret = posix_memalign(&mut out, align as libc::size_t, size as libc::size_t);
106                 if ret != 0 {
107                     ptr::null_mut()
108                 } else {
109                     out as *mut u8
110                 }
111             }
112             more_aligned_malloc(size, align)
113         }
114     }
115
116     pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 {
117         if align <= MIN_ALIGN {
118             libc::realloc(ptr as *mut libc::c_void, size as libc::size_t) as *mut u8
119         } else {
120             let new_ptr = allocate(size, align);
121             ptr::copy(ptr, new_ptr, cmp::min(size, old_size));
122             deallocate(ptr, old_size, align);
123             new_ptr
124         }
125     }
126
127     pub unsafe fn reallocate_inplace(_ptr: *mut u8,
128                                      old_size: usize,
129                                      _size: usize,
130                                      _align: usize)
131                                      -> usize {
132         old_size
133     }
134
135     pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, _align: usize) {
136         libc::free(ptr as *mut libc::c_void)
137     }
138
139     pub fn usable_size(size: usize, _align: usize) -> usize {
140         size
141     }
142 }
143
144 #[cfg(windows)]
145 #[allow(bad_style)]
146 mod imp {
147     use MIN_ALIGN;
148
149     type LPVOID = *mut u8;
150     type HANDLE = LPVOID;
151     type SIZE_T = usize;
152     type DWORD = u32;
153     type BOOL = i32;
154
155     extern "system" {
156         fn GetProcessHeap() -> HANDLE;
157         fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
158         fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID, dwBytes: SIZE_T) -> LPVOID;
159         fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
160     }
161
162     #[repr(C)]
163     struct Header(*mut u8);
164
165     const HEAP_REALLOC_IN_PLACE_ONLY: DWORD = 0x00000010;
166
167     unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
168         &mut *(ptr as *mut Header).offset(-1)
169     }
170
171     unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
172         let aligned = ptr.offset((align - (ptr as usize & (align - 1))) as isize);
173         *get_header(aligned) = Header(ptr);
174         aligned
175     }
176
177     pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
178         if align <= MIN_ALIGN {
179             HeapAlloc(GetProcessHeap(), 0, size as SIZE_T) as *mut u8
180         } else {
181             let ptr = HeapAlloc(GetProcessHeap(), 0, (size + align) as SIZE_T) as *mut u8;
182             if ptr.is_null() {
183                 return ptr;
184             }
185             align_ptr(ptr, align)
186         }
187     }
188
189     pub unsafe fn reallocate(ptr: *mut u8, _old_size: usize, size: usize, align: usize) -> *mut u8 {
190         if align <= MIN_ALIGN {
191             HeapReAlloc(GetProcessHeap(), 0, ptr as LPVOID, size as SIZE_T) as *mut u8
192         } else {
193             let header = get_header(ptr);
194             let new = HeapReAlloc(GetProcessHeap(),
195                                   0,
196                                   header.0 as LPVOID,
197                                   (size + align) as SIZE_T) as *mut u8;
198             if new.is_null() {
199                 return new;
200             }
201             align_ptr(new, align)
202         }
203     }
204
205     pub unsafe fn reallocate_inplace(ptr: *mut u8,
206                                      old_size: usize,
207                                      size: usize,
208                                      align: usize)
209                                      -> usize {
210         if align <= MIN_ALIGN {
211             let new = HeapReAlloc(GetProcessHeap(),
212                                   HEAP_REALLOC_IN_PLACE_ONLY,
213                                   ptr as LPVOID,
214                                   size as SIZE_T) as *mut u8;
215             if new.is_null() {
216                 old_size
217             } else {
218                 size
219             }
220         } else {
221             old_size
222         }
223     }
224
225     pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, align: usize) {
226         if align <= MIN_ALIGN {
227             let err = HeapFree(GetProcessHeap(), 0, ptr as LPVOID);
228             debug_assert!(err != 0);
229         } else {
230             let header = get_header(ptr);
231             let err = HeapFree(GetProcessHeap(), 0, header.0 as LPVOID);
232             debug_assert!(err != 0);
233         }
234     }
235
236     pub fn usable_size(size: usize, _align: usize) -> usize {
237         size
238     }
239 }