9
9
// except according to those terms.
10
10
11
11
// FIXME: #13994: port to the sized deallocation API when available
12
- // FIXME: #13996: mark the `allocate` and `reallocate` return value as `noalias` and `nonnull`
12
+ // FIXME: #13996: mark the `allocate` and `reallocate` return value as `noalias`
13
+ // and `nonnull`
13
14
14
15
use core:: intrinsics:: { abort, cttz32} ;
15
16
use core:: option:: { None , Option } ;
@@ -23,7 +24,8 @@ use libc::{c_char, c_int, c_void, size_t};
23
24
extern {
24
25
fn je_mallocx ( size : size_t , flags : c_int ) -> * mut c_void ;
25
26
fn je_rallocx ( ptr : * mut c_void , size : size_t , flags : c_int ) -> * mut c_void ;
26
- fn je_xallocx ( ptr : * mut c_void , size : size_t , extra : size_t , flags : c_int ) -> size_t ;
27
+ fn je_xallocx ( ptr : * mut c_void , size : size_t , extra : size_t ,
28
+ flags : c_int ) -> size_t ;
27
29
fn je_dallocx ( ptr : * mut c_void , flags : c_int ) ;
28
30
fn je_nallocx ( size : size_t , flags : c_int ) -> size_t ;
29
31
fn je_malloc_stats_print ( write_cb : Option < extern "C" fn ( cbopaque : * mut c_void , * c_char ) > ,
@@ -42,8 +44,9 @@ fn mallocx_align(a: uint) -> c_int { unsafe { cttz32(a as u32) as c_int } }
42
44
43
45
/// Return a pointer to `size` bytes of memory.
44
46
///
45
- /// Behavior is undefined if the requested size is 0 or the alignment is not a power of 2. The
46
- /// alignment must be no larger than the largest supported page size on the platform.
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.
47
50
#[ inline]
48
51
pub unsafe fn allocate ( size : uint , align : uint ) -> * mut u8 {
49
52
let ptr = je_mallocx ( size as size_t , mallocx_align ( align) ) as * mut u8 ;
@@ -53,62 +56,73 @@ pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
53
56
ptr
54
57
}
55
58
56
- /// Extend or shrink the allocation referenced by `ptr` to `size` bytes of memory.
59
+ /// Extend or shrink the allocation referenced by `ptr` to `size` bytes of
60
+ /// memory.
57
61
///
58
- /// Behavior is undefined if the requested size is 0 or the alignment is not a power of 2. The
59
- /// alignment must be no larger than the largest supported page size on the platform.
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.
60
65
///
61
- /// The `old_size` and `align` parameters are the parameters that were used to create the
62
- /// allocation referenced by `ptr`. The `old_size` parameter may also be the value returned by
63
- /// `usable_size` for the requested size.
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.
64
69
#[ inline]
65
70
#[ allow( unused_variable) ] // for the parameter names in the documentation
66
- pub unsafe fn reallocate ( ptr : * mut u8 , size : uint , align : uint , old_size : uint ) -> * mut u8 {
67
- let ptr = je_rallocx ( ptr as * mut c_void , size as size_t , mallocx_align ( align) ) as * mut u8 ;
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 ;
68
75
if ptr. is_null ( ) {
69
76
abort ( )
70
77
}
71
78
ptr
72
79
}
73
80
74
- /// Extend or shrink the allocation referenced by `ptr` to `size` bytes of memory in-place.
81
+ /// Extend or shrink the allocation referenced by `ptr` to `size` bytes of
82
+ /// memory in-place.
75
83
///
76
- /// Return true if successful, otherwise false if the allocation was not altered.
84
+ /// Return true if successful, otherwise false if the allocation was not
85
+ /// altered.
77
86
///
78
- /// Behavior is undefined if the requested size is 0 or the alignment is not a power of 2. The
79
- /// alignment must be no larger than the largest supported page size on the platform.
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.
80
90
///
81
91
/// The `old_size` and `align` parameters are the parameters that were used to
82
92
/// create the allocation referenced by `ptr`. The `old_size` parameter may be
83
93
/// any value in range_inclusive(requested_size, usable_size).
84
94
#[ inline]
85
95
#[ allow( unused_variable) ] // for the parameter names in the documentation
86
- pub unsafe fn reallocate_inplace ( ptr : * mut u8 , size : uint , align : uint , old_size : uint ) -> bool {
87
- je_xallocx ( ptr as * mut c_void , size as size_t , 0 , mallocx_align ( align) ) == size as size_t
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
88
100
}
89
101
90
102
/// Deallocate the memory referenced by `ptr`.
91
103
///
92
104
/// The `ptr` parameter must not be null.
93
105
///
94
- /// The `size` and `align` parameters are the parameters that were used to create the
95
- /// allocation referenced by `ptr`. The `size` parameter may also be the value returned by
96
- /// `usable_size` for the requested size.
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.
97
109
#[ inline]
98
110
#[ allow( unused_variable) ] // for the parameter names in the documentation
99
111
pub unsafe fn deallocate ( ptr : * mut u8 , size : uint , align : uint ) {
100
112
je_dallocx ( ptr as * mut c_void , mallocx_align ( align) )
101
113
}
102
114
103
- /// Return the usable size of an allocation created with the specified the `size` and `align`.
115
+ /// Return the usable size of an allocation created with the specified the
116
+ /// `size` and `align`.
104
117
#[ inline]
105
118
pub fn usable_size ( size : uint , align : uint ) -> uint {
106
119
unsafe { je_nallocx ( size as size_t , mallocx_align ( align) ) as uint }
107
120
}
108
121
109
122
/// Print implementation-defined allocator statistics.
110
123
///
111
- /// These statistics may be inconsistent if other threads use the allocator during the call.
124
+ /// These statistics may be inconsistent if other threads use the allocator
125
+ /// during the call.
112
126
#[ unstable]
113
127
pub fn stats_print ( ) {
114
128
unsafe {
@@ -145,7 +159,8 @@ unsafe fn exchange_free(ptr: *mut u8, size: uint, align: uint) {
145
159
#[ lang="closure_exchange_malloc" ]
146
160
#[ inline]
147
161
#[ allow( deprecated) ]
148
- unsafe fn closure_exchange_malloc ( drop_glue : fn ( * mut u8 ) , size : uint , align : uint ) -> * mut u8 {
162
+ unsafe fn closure_exchange_malloc ( drop_glue : fn ( * mut u8 ) , size : uint ,
163
+ align : uint ) -> * mut u8 {
149
164
let total_size = util:: get_box_size ( size, align) ;
150
165
let p = allocate ( total_size, 8 ) ;
151
166
0 commit comments