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