]> git.lizzy.rs Git - rust.git/blob - src/liballoc/heap.rs
b4d0057778a1a395255300163a03192be6a6c743
[rust.git] / src / liballoc / heap.rs
1 // Copyright 2014 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 // FIXME: #13994: port to the sized deallocation API when available
12 // FIXME: #13996: mark the `allocate` and `reallocate` return value as `noalias`
13 //                and `nonnull`
14
15 #[cfg(not(test))] use core::raw;
16 #[cfg(not(test))] use util;
17
18 /// Return a pointer to `size` bytes of memory.
19 ///
20 /// Behavior is undefined if the requested size is 0 or the alignment is not a
21 /// power of 2. The alignment must be no larger than the largest supported page
22 /// size on the platform.
23 #[inline]
24 pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
25     imp::allocate(size, align)
26 }
27
28 /// Extend or shrink the allocation referenced by `ptr` to `size` bytes of
29 /// memory.
30 ///
31 /// Behavior is undefined if the requested size is 0 or the alignment is not a
32 /// power of 2. The alignment must be no larger than the largest supported page
33 /// size on the platform.
34 ///
35 /// The `old_size` and `align` parameters are the parameters that were used to
36 /// create the allocation referenced by `ptr`. The `old_size` parameter may also
37 /// be the value returned by `usable_size` for the requested size.
38 #[inline]
39 pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
40                          old_size: uint) -> *mut u8 {
41     imp::reallocate(ptr, size, align, old_size)
42 }
43
44 /// Extend or shrink the allocation referenced by `ptr` to `size` bytes of
45 /// memory in-place.
46 ///
47 /// Return true if successful, otherwise false if the allocation was not
48 /// altered.
49 ///
50 /// Behavior is undefined if the requested size is 0 or the alignment is not a
51 /// power of 2. The alignment must be no larger than the largest supported page
52 /// size on the platform.
53 ///
54 /// The `old_size` and `align` parameters are the parameters that were used to
55 /// create the allocation referenced by `ptr`. The `old_size` parameter may be
56 /// any value in range_inclusive(requested_size, usable_size).
57 #[inline]
58 pub unsafe fn reallocate_inplace(ptr: *mut u8, size: uint, align: uint,
59                                  old_size: uint) -> bool {
60     imp::reallocate_inplace(ptr, size, align, old_size)
61 }
62
63 /// Deallocate the memory referenced by `ptr`.
64 ///
65 /// The `ptr` parameter must not be null.
66 ///
67 /// The `size` and `align` parameters are the parameters that were used to
68 /// create the allocation referenced by `ptr`. The `size` parameter may also be
69 /// the value returned by `usable_size` for the requested size.
70 #[inline]
71 pub unsafe fn deallocate(ptr: *mut u8, size: uint, align: uint) {
72     imp::deallocate(ptr, size, align)
73 }
74
75 /// Return the usable size of an allocation created with the specified the
76 /// `size` and `align`.
77 #[inline]
78 pub fn usable_size(size: uint, align: uint) -> uint {
79     imp::usable_size(size, align)
80 }
81
82 /// Print implementation-defined allocator statistics.
83 ///
84 /// These statistics may be inconsistent if other threads use the allocator
85 /// during the call.
86 #[unstable]
87 pub fn stats_print() {
88     imp::stats_print();
89 }
90
91 // The compiler never calls `exchange_free` on ~ZeroSizeType, so zero-size
92 // allocations can point to this `static`. It would be incorrect to use a null
93 // pointer, due to enums assuming types like unique pointers are never null.
94 pub static mut EMPTY: uint = 12345;
95
96 /// The allocator for unique pointers.
97 #[cfg(not(test))]
98 #[lang="exchange_malloc"]
99 #[inline]
100 unsafe fn exchange_malloc(size: uint, align: uint) -> *mut u8 {
101     if size == 0 {
102         &EMPTY as *uint as *mut u8
103     } else {
104         allocate(size, align)
105     }
106 }
107
108 #[cfg(not(test))]
109 #[lang="exchange_free"]
110 #[inline]
111 unsafe fn exchange_free(ptr: *mut u8, size: uint, align: uint) {
112     deallocate(ptr, size, align);
113 }
114
115 // FIXME: #7496
116 #[cfg(not(test))]
117 #[lang="closure_exchange_malloc"]
118 #[inline]
119 #[allow(deprecated)]
120 unsafe fn closure_exchange_malloc(drop_glue: fn(*mut u8), size: uint,
121                                   align: uint) -> *mut u8 {
122     let total_size = util::get_box_size(size, align);
123     let p = allocate(total_size, 8);
124
125     let alloc = p as *mut raw::Box<()>;
126     (*alloc).drop_glue = drop_glue;
127
128     alloc as *mut u8
129 }
130
131 #[cfg(jemalloc)]
132 mod imp {
133     use core::option::{None, Option};
134     use core::ptr::{RawPtr, mut_null, null};
135     use core::num::Int;
136     use libc::{c_char, c_int, c_void, size_t};
137
138     #[link(name = "jemalloc", kind = "static")]
139     extern {
140         fn je_mallocx(size: size_t, flags: c_int) -> *mut c_void;
141         fn je_rallocx(ptr: *mut c_void, size: size_t,
142                       flags: c_int) -> *mut c_void;
143         fn je_xallocx(ptr: *mut c_void, size: size_t, extra: size_t,
144                       flags: c_int) -> size_t;
145         fn je_dallocx(ptr: *mut c_void, flags: c_int);
146         fn je_nallocx(size: size_t, flags: c_int) -> size_t;
147         fn je_malloc_stats_print(write_cb: Option<extern "C" fn(cbopaque: *mut c_void, *c_char)>,
148                                  cbopaque: *mut c_void,
149                                  opts: *c_char);
150     }
151
152     // -lpthread needs to occur after -ljemalloc, the earlier argument isn't enough
153     #[cfg(not(windows), not(target_os = "android"))]
154     #[link(name = "pthread")]
155     extern {}
156
157     // MALLOCX_ALIGN(a) macro
158     #[inline(always)]
159     fn mallocx_align(a: uint) -> c_int { a.trailing_zeros() as c_int }
160
161     #[inline]
162     pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
163         let ptr = je_mallocx(size as size_t, mallocx_align(align)) as *mut u8;
164         if ptr.is_null() {
165             ::oom()
166         }
167         ptr
168     }
169
170     #[inline]
171     pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
172                              _old_size: uint) -> *mut u8 {
173         let ptr = je_rallocx(ptr as *mut c_void, size as size_t,
174                              mallocx_align(align)) as *mut u8;
175         if ptr.is_null() {
176             ::oom()
177         }
178         ptr
179     }
180
181     #[inline]
182     pub unsafe fn reallocate_inplace(ptr: *mut u8, size: uint, align: uint,
183                                      _old_size: uint) -> bool {
184         je_xallocx(ptr as *mut c_void, size as size_t, 0,
185                    mallocx_align(align)) == size as size_t
186     }
187
188     #[inline]
189     pub unsafe fn deallocate(ptr: *mut u8, _size: uint, align: uint) {
190         je_dallocx(ptr as *mut c_void, mallocx_align(align))
191     }
192
193     #[inline]
194     pub fn usable_size(size: uint, align: uint) -> uint {
195         unsafe { je_nallocx(size as size_t, mallocx_align(align)) as uint }
196     }
197
198     pub fn stats_print() {
199         unsafe {
200             je_malloc_stats_print(None, mut_null(), null())
201         }
202     }
203 }
204
205 #[cfg(not(jemalloc), unix)]
206 mod imp {
207     use core::mem;
208     use core::ptr;
209     use libc;
210     use libc_heap;
211
212     extern {
213         fn posix_memalign(memptr: *mut *mut libc::c_void,
214                           align: libc::size_t,
215                           size: libc::size_t) -> libc::c_int;
216     }
217
218     #[inline]
219     pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
220         // The posix_memalign manpage states
221         //
222         //      alignment [...] must be a power of and a multiple of
223         //      sizeof(void *)
224         //
225         // The `align` parameter to this function is the *minimum* alignment for
226         // a block of memory, so we special case everything under `*uint` to
227         // just pass it to malloc, which is guaranteed to align to at least the
228         // size of `*uint`.
229         if align < mem::size_of::<*uint>() {
230             libc_heap::malloc_raw(size)
231         } else {
232             let mut out = 0 as *mut libc::c_void;
233             let ret = posix_memalign(&mut out,
234                                      align as libc::size_t,
235                                      size as libc::size_t);
236             if ret != 0 {
237                 ::oom();
238             }
239             out as *mut u8
240         }
241     }
242
243     #[inline]
244     pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
245                              old_size: uint) -> *mut u8 {
246         let new_ptr = allocate(size, align);
247         ptr::copy_memory(new_ptr, ptr as *u8, old_size);
248         deallocate(ptr, old_size, align);
249         return new_ptr;
250     }
251
252     #[inline]
253     pub unsafe fn reallocate_inplace(_ptr: *mut u8, _size: uint, _align: uint,
254                                      _old_size: uint) -> bool {
255         false
256     }
257
258     #[inline]
259     pub unsafe fn deallocate(ptr: *mut u8, _size: uint, _align: uint) {
260         libc::free(ptr as *mut libc::c_void)
261     }
262
263     #[inline]
264     pub fn usable_size(size: uint, _align: uint) -> uint {
265         size
266     }
267
268     pub fn stats_print() {
269     }
270 }
271
272 #[cfg(not(jemalloc), windows)]
273 mod imp {
274     use libc::{c_void, size_t};
275     use core::ptr::RawPtr;
276
277     extern {
278         fn _aligned_malloc(size: size_t, align: size_t) -> *mut c_void;
279         fn _aligned_realloc(block: *mut c_void, size: size_t,
280                             align: size_t) -> *mut c_void;
281         fn _aligned_free(ptr: *mut c_void);
282     }
283
284     #[inline]
285     pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
286         let ptr = _aligned_malloc(size as size_t, align as size_t);
287         if ptr.is_null() {
288             ::oom();
289         }
290         ptr as *mut u8
291     }
292
293     #[inline]
294     pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
295                              _old_size: uint) -> *mut u8 {
296         let ptr = _aligned_realloc(ptr as *mut c_void, size as size_t,
297                                    align as size_t);
298         if ptr.is_null() {
299             ::oom();
300         }
301         ptr as *mut u8
302     }
303
304     #[inline]
305     pub unsafe fn reallocate_inplace(_ptr: *mut u8, _size: uint, _align: uint,
306                                      _old_size: uint) -> bool {
307         false
308     }
309
310     #[inline]
311     pub unsafe fn deallocate(ptr: *mut u8, _size: uint, _align: uint) {
312         _aligned_free(ptr as *mut c_void)
313     }
314
315     #[inline]
316     pub fn usable_size(size: uint, _align: uint) -> uint {
317         size
318     }
319
320     pub fn stats_print() {}
321 }
322
323 #[cfg(test)]
324 mod bench {
325     extern crate test;
326     use self::test::Bencher;
327
328     #[bench]
329     fn alloc_owned_small(b: &mut Bencher) {
330         b.iter(|| {
331             box 10
332         })
333     }
334 }