]> git.lizzy.rs Git - rust.git/blob - src/liballoc/heap.rs
alloc: Format heap.rs to 80-char max
[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 use core::intrinsics::{abort, cttz32};
16 use core::option::{None, Option};
17 use core::ptr::{RawPtr, mut_null, null};
18 use libc::{c_char, c_int, c_void, size_t};
19
20 #[cfg(not(test))] use core::raw;
21 #[cfg(not(test))] use util;
22
23 #[link(name = "jemalloc", kind = "static")]
24 extern {
25     fn je_mallocx(size: size_t, flags: c_int) -> *mut c_void;
26     fn je_rallocx(ptr: *mut c_void, size: size_t, flags: c_int) -> *mut c_void;
27     fn je_xallocx(ptr: *mut c_void, size: size_t, extra: size_t,
28                   flags: c_int) -> size_t;
29     fn je_dallocx(ptr: *mut c_void, flags: c_int);
30     fn je_nallocx(size: size_t, flags: c_int) -> size_t;
31     fn je_malloc_stats_print(write_cb: Option<extern "C" fn(cbopaque: *mut c_void, *c_char)>,
32                              cbopaque: *mut c_void,
33                              opts: *c_char);
34 }
35
36 // -lpthread needs to occur after -ljemalloc, the earlier argument isn't enough
37 #[cfg(not(windows), not(target_os = "android"))]
38 #[link(name = "pthread")]
39 extern {}
40
41 // MALLOCX_ALIGN(a) macro
42 #[inline(always)]
43 fn mallocx_align(a: uint) -> c_int { unsafe { cttz32(a as u32) as c_int } }
44
45 /// Return a pointer to `size` bytes of memory.
46 ///
47 /// Behavior is undefined if the requested size is 0 or the alignment is not a
48 /// power of 2. The alignment must be no larger than the largest supported page
49 /// size on the platform.
50 #[inline]
51 pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
52     let ptr = je_mallocx(size as size_t, mallocx_align(align)) as *mut u8;
53     if ptr.is_null() {
54         abort()
55     }
56     ptr
57 }
58
59 /// Extend or shrink the allocation referenced by `ptr` to `size` bytes of
60 /// memory.
61 ///
62 /// Behavior is undefined if the requested size is 0 or the alignment is not a
63 /// power of 2. The alignment must be no larger than the largest supported page
64 /// size on the platform.
65 ///
66 /// The `old_size` and `align` parameters are the parameters that were used to
67 /// create the allocation referenced by `ptr`. The `old_size` parameter may also
68 /// be the value returned by `usable_size` for the requested size.
69 #[inline]
70 #[allow(unused_variable)] // for the parameter names in the documentation
71 pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
72                          old_size: uint) -> *mut u8 {
73     let ptr = je_rallocx(ptr as *mut c_void, size as size_t,
74                          mallocx_align(align)) as *mut u8;
75     if ptr.is_null() {
76         abort()
77     }
78     ptr
79 }
80
81 /// Extend or shrink the allocation referenced by `ptr` to `size` bytes of
82 /// memory in-place.
83 ///
84 /// Return true if successful, otherwise false if the allocation was not
85 /// altered.
86 ///
87 /// Behavior is undefined if the requested size is 0 or the alignment is not a
88 /// power of 2. The alignment must be no larger than the largest supported page
89 /// size on the platform.
90 ///
91 /// The `old_size` and `align` parameters are the parameters that were used to
92 /// create the allocation referenced by `ptr`. The `old_size` parameter may be
93 /// any value in range_inclusive(requested_size, usable_size).
94 #[inline]
95 #[allow(unused_variable)] // for the parameter names in the documentation
96 pub unsafe fn reallocate_inplace(ptr: *mut u8, size: uint, align: uint,
97                                  old_size: uint) -> bool {
98     je_xallocx(ptr as *mut c_void, size as size_t, 0,
99                mallocx_align(align)) == size as size_t
100 }
101
102 /// Deallocate the memory referenced by `ptr`.
103 ///
104 /// The `ptr` parameter must not be null.
105 ///
106 /// The `size` and `align` parameters are the parameters that were used to
107 /// create the allocation referenced by `ptr`. The `size` parameter may also be
108 /// the value returned by `usable_size` for the requested size.
109 #[inline]
110 #[allow(unused_variable)] // for the parameter names in the documentation
111 pub unsafe fn deallocate(ptr: *mut u8, size: uint, align: uint) {
112     je_dallocx(ptr as *mut c_void, mallocx_align(align))
113 }
114
115 /// Return the usable size of an allocation created with the specified the
116 /// `size` and `align`.
117 #[inline]
118 pub fn usable_size(size: uint, align: uint) -> uint {
119     unsafe { je_nallocx(size as size_t, mallocx_align(align)) as uint }
120 }
121
122 /// Print implementation-defined allocator statistics.
123 ///
124 /// These statistics may be inconsistent if other threads use the allocator
125 /// during the call.
126 #[unstable]
127 pub fn stats_print() {
128     unsafe {
129         je_malloc_stats_print(None, mut_null(), null())
130     }
131 }
132
133 // The compiler never calls `exchange_free` on ~ZeroSizeType, so zero-size
134 // allocations can point to this `static`. It would be incorrect to use a null
135 // pointer, due to enums assuming types like unique pointers are never null.
136 pub static mut EMPTY: uint = 12345;
137
138 /// The allocator for unique pointers.
139 #[cfg(not(test))]
140 #[lang="exchange_malloc"]
141 #[inline]
142 unsafe fn exchange_malloc(size: uint, align: uint) -> *mut u8 {
143     if size == 0 {
144         &EMPTY as *uint as *mut u8
145     } else {
146         allocate(size, align)
147     }
148 }
149
150 #[cfg(not(test))]
151 #[lang="exchange_free"]
152 #[inline]
153 unsafe fn exchange_free(ptr: *mut u8, size: uint, align: uint) {
154     deallocate(ptr, size, align);
155 }
156
157 // FIXME: #7496
158 #[cfg(not(test))]
159 #[lang="closure_exchange_malloc"]
160 #[inline]
161 #[allow(deprecated)]
162 unsafe fn closure_exchange_malloc(drop_glue: fn(*mut u8), size: uint,
163                                   align: uint) -> *mut u8 {
164     let total_size = util::get_box_size(size, align);
165     let p = allocate(total_size, 8);
166
167     let alloc = p as *mut raw::Box<()>;
168     (*alloc).drop_glue = drop_glue;
169
170     alloc as *mut u8
171 }
172
173 #[cfg(test)]
174 mod bench {
175     extern crate test;
176     use self::test::Bencher;
177
178     #[bench]
179     fn alloc_owned_small(b: &mut Bencher) {
180         b.iter(|| {
181             box 10
182         })
183     }
184 }