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.
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.
11 // FIXME: #13994: port to the sized deallocation API when available
12 // FIXME: #13996: mark the `allocate` and `reallocate` return value as `noalias`
15 #[cfg(not(test))] use core::raw;
16 #[cfg(not(test))] use util;
18 /// Return a pointer to `size` bytes of memory.
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.
24 pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
25 imp::allocate(size, align)
28 /// Extend or shrink the allocation referenced by `ptr` to `size` bytes of
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.
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.
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)
44 /// Extend or shrink the allocation referenced by `ptr` to `size` bytes of
47 /// Return true if successful, otherwise false if the allocation was not
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.
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).
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)
63 /// Deallocate the memory referenced by `ptr`.
65 /// The `ptr` parameter must not be null.
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.
71 pub unsafe fn deallocate(ptr: *mut u8, size: uint, align: uint) {
72 imp::deallocate(ptr, size, align)
75 /// Return the usable size of an allocation created with the specified the
76 /// `size` and `align`.
78 pub fn usable_size(size: uint, align: uint) -> uint {
79 imp::usable_size(size, align)
82 /// Print implementation-defined allocator statistics.
84 /// These statistics may be inconsistent if other threads use the allocator
87 pub fn stats_print() {
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;
96 /// The allocator for unique pointers.
98 #[lang="exchange_malloc"]
100 unsafe fn exchange_malloc(size: uint, align: uint) -> *mut u8 {
102 &EMPTY as *uint as *mut u8
104 allocate(size, align)
109 #[lang="exchange_free"]
111 unsafe fn exchange_free(ptr: *mut u8, size: uint, align: uint) {
112 deallocate(ptr, size, align);
117 #[lang="closure_exchange_malloc"]
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);
125 let alloc = p as *mut raw::Box<()>;
126 (*alloc).drop_glue = drop_glue;
133 use core::option::{None, Option};
134 use core::ptr::{RawPtr, mut_null, null};
136 use libc::{c_char, c_int, c_void, size_t};
138 #[link(name = "jemalloc", kind = "static")]
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,
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")]
157 // MALLOCX_ALIGN(a) macro
159 fn mallocx_align(a: uint) -> c_int { a.trailing_zeros() as c_int }
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;
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;
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
189 pub unsafe fn deallocate(ptr: *mut u8, _size: uint, align: uint) {
190 je_dallocx(ptr as *mut c_void, mallocx_align(align))
194 pub fn usable_size(size: uint, align: uint) -> uint {
195 unsafe { je_nallocx(size as size_t, mallocx_align(align)) as uint }
198 pub fn stats_print() {
200 je_malloc_stats_print(None, mut_null(), null())
205 #[cfg(not(jemalloc), unix)]
213 fn posix_memalign(memptr: *mut *mut libc::c_void,
215 size: libc::size_t) -> libc::c_int;
219 pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
220 // The posix_memalign manpage states
222 // alignment [...] must be a power of and a multiple of
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
229 if align < mem::size_of::<*uint>() {
230 libc_heap::malloc_raw(size)
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);
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);
253 pub unsafe fn reallocate_inplace(_ptr: *mut u8, _size: uint, _align: uint,
254 _old_size: uint) -> bool {
259 pub unsafe fn deallocate(ptr: *mut u8, _size: uint, _align: uint) {
260 libc::free(ptr as *mut libc::c_void)
264 pub fn usable_size(size: uint, _align: uint) -> uint {
268 pub fn stats_print() {
272 #[cfg(not(jemalloc), windows)]
274 use libc::{c_void, size_t};
275 use core::ptr::RawPtr;
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);
285 pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
286 let ptr = _aligned_malloc(size as size_t, align as size_t);
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,
305 pub unsafe fn reallocate_inplace(_ptr: *mut u8, _size: uint, _align: uint,
306 _old_size: uint) -> bool {
311 pub unsafe fn deallocate(ptr: *mut u8, _size: uint, _align: uint) {
312 _aligned_free(ptr as *mut c_void)
316 pub fn usable_size(size: uint, _align: uint) -> uint {
320 pub fn stats_print() {}
326 use self::test::Bencher;
329 fn alloc_owned_small(b: &mut Bencher) {