1 // Copyright 2014-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.
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 #![unstable(feature = "heap_api",
12 reason = "the precise API and guarantees it provides may be tweaked \
13 slightly, especially to possibly take into account the \
14 types being stored to make room for a future \
15 tracing garbage collector",
18 use core::{isize, usize};
20 #[allow(improper_ctypes)]
23 fn __rust_allocate(size: usize, align: usize) -> *mut u8;
24 fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize);
25 fn __rust_reallocate(ptr: *mut u8, old_size: usize, size: usize,
26 align: usize) -> *mut u8;
27 fn __rust_reallocate_inplace(ptr: *mut u8, old_size: usize, size: usize,
28 align: usize) -> usize;
29 fn __rust_usable_size(size: usize, align: usize) -> usize;
33 fn check_size_and_alignment(size: usize, align: usize) {
34 debug_assert!(size != 0);
35 debug_assert!(size <= isize::MAX as usize, "Tried to allocate too much: {} bytes", size);
36 debug_assert!(usize::is_power_of_two(align), "Invalid alignment of allocation: {}", align);
39 // FIXME: #13996: mark the `allocate` and `reallocate` return value as `noalias`
41 /// Return a pointer to `size` bytes of memory aligned to `align`.
43 /// On failure, return a null pointer.
45 /// Behavior is undefined if the requested size is 0 or the alignment is not a
46 /// power of 2. The alignment must be no larger than the largest supported page
47 /// size on the platform.
49 pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
50 check_size_and_alignment(size, align);
51 __rust_allocate(size, align)
54 /// Resize the allocation referenced by `ptr` to `size` bytes.
56 /// On failure, return a null pointer and leave the original allocation intact.
58 /// If the allocation was relocated, the memory at the passed-in pointer is
59 /// undefined after the call.
61 /// Behavior is undefined if the requested size is 0 or the alignment is not a
62 /// power of 2. The alignment must be no larger than the largest supported page
63 /// size on the platform.
65 /// The `old_size` and `align` parameters are the parameters that were used to
66 /// create the allocation referenced by `ptr`. The `old_size` parameter may be
67 /// any value in range_inclusive(requested_size, usable_size).
69 pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 {
70 check_size_and_alignment(size, align);
71 __rust_reallocate(ptr, old_size, size, align)
74 /// Resize the allocation referenced by `ptr` to `size` bytes.
76 /// If the operation succeeds, it returns `usable_size(size, align)` and if it
77 /// fails (or is a no-op) it returns `usable_size(old_size, align)`.
79 /// Behavior is undefined if the requested size is 0 or the alignment is not a
80 /// power of 2. The alignment must be no larger than the largest supported page
81 /// size on the platform.
83 /// The `old_size` and `align` parameters are the parameters that were used to
84 /// create the allocation referenced by `ptr`. The `old_size` parameter may be
85 /// any value in range_inclusive(requested_size, usable_size).
87 pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: usize, size: usize,
88 align: usize) -> usize {
89 check_size_and_alignment(size, align);
90 __rust_reallocate_inplace(ptr, old_size, size, align)
93 /// Deallocates the memory referenced by `ptr`.
95 /// The `ptr` parameter must not be null.
97 /// The `old_size` and `align` parameters are the parameters that were used to
98 /// create the allocation referenced by `ptr`. The `old_size` parameter may be
99 /// any value in range_inclusive(requested_size, usable_size).
101 pub unsafe fn deallocate(ptr: *mut u8, old_size: usize, align: usize) {
102 __rust_deallocate(ptr, old_size, align)
105 /// Returns the usable size of an allocation created with the specified the
106 /// `size` and `align`.
108 pub fn usable_size(size: usize, align: usize) -> usize {
109 unsafe { __rust_usable_size(size, align) }
112 /// An arbitrary non-null address to represent zero-size allocations.
114 /// This preserves the non-null invariant for types like `Box<T>`. The address
115 /// may overlap with non-zero-size memory allocations.
116 pub const EMPTY: *mut () = 0x1 as *mut ();
118 /// The allocator for unique pointers.
120 #[lang = "exchange_malloc"]
122 unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
126 let ptr = allocate(size, align);
127 if ptr.is_null() { ::oom() }
133 #[lang = "exchange_free"]
135 unsafe fn exchange_free(ptr: *mut u8, old_size: usize, align: usize) {
136 deallocate(ptr, old_size, align);
142 use self::test::Bencher;
147 fn basic_reallocate_inplace_noop() {
150 let ptr = heap::allocate(size, 8);
151 if ptr.is_null() { ::oom() }
152 let ret = heap::reallocate_inplace(ptr, size, size, 8);
153 heap::deallocate(ptr, size, 8);
154 assert_eq!(ret, heap::usable_size(size, 8));
159 fn alloc_owned_small(b: &mut Bencher) {
161 let _: Box<_> = box 10;