8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
+ use core:: ptr:: RawPtr ;
12
+
11
13
// FIXME: #13996: mark the `allocate` and `reallocate` return value as `noalias`
12
14
13
- /// Returns a pointer to `size` bytes of memory.
15
+ /// Return a pointer to `size` bytes of memory aligned to `align`.
16
+ ///
17
+ /// On failure, return a null pointer.
14
18
///
15
19
/// Behavior is undefined if the requested size is 0 or the alignment is not a
16
20
/// power of 2. The alignment must be no larger than the largest supported page
@@ -20,8 +24,9 @@ pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
20
24
imp:: allocate ( size, align)
21
25
}
22
26
23
- /// Extends or shrinks the allocation referenced by `ptr` to `size` bytes of
24
- /// memory.
27
+ /// Resize the allocation referenced by `ptr` to `size` bytes.
28
+ ///
29
+ /// On failure, return a null pointer and leave the original allocation intact.
25
30
///
26
31
/// Behavior is undefined if the requested size is 0 or the alignment is not a
27
32
/// power of 2. The alignment must be no larger than the largest supported page
@@ -35,8 +40,7 @@ pub unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint)
35
40
imp:: reallocate ( ptr, old_size, size, align)
36
41
}
37
42
38
- /// Extends or shrinks the allocation referenced by `ptr` to `size` bytes of
39
- /// memory in-place.
43
+ /// Resize the allocation referenced by `ptr` to `size` bytes.
40
44
///
41
45
/// If the operation succeeds, it returns `usable_size(size, align)` and if it
42
46
/// fails (or is a no-op) it returns `usable_size(old_size, align)`.
@@ -95,7 +99,9 @@ unsafe fn exchange_malloc(size: uint, align: uint) -> *mut u8 {
95
99
if size == 0 {
96
100
EMPTY as * mut u8
97
101
} else {
98
- allocate ( size, align)
102
+ let ptr = allocate ( size, align) ;
103
+ if ptr. is_null ( ) { :: oom ( ) }
104
+ ptr
99
105
}
100
106
}
101
107
@@ -120,7 +126,7 @@ const MIN_ALIGN: uint = 16;
120
126
#[ cfg( jemalloc) ]
121
127
mod imp {
122
128
use core:: option:: { None , Option } ;
123
- use core:: ptr:: { RawPtr , null_mut, null} ;
129
+ use core:: ptr:: { null_mut, null} ;
124
130
use core:: num:: Int ;
125
131
use libc:: { c_char, c_int, c_void, size_t} ;
126
132
use super :: MIN_ALIGN ;
@@ -131,10 +137,8 @@ mod imp {
131
137
132
138
extern {
133
139
fn je_mallocx ( size : size_t , flags : c_int ) -> * mut c_void ;
134
- fn je_rallocx ( ptr : * mut c_void , size : size_t ,
135
- flags : c_int ) -> * mut c_void ;
136
- fn je_xallocx ( ptr : * mut c_void , size : size_t , extra : size_t ,
137
- flags : c_int ) -> size_t ;
140
+ fn je_rallocx ( ptr : * mut c_void , size : size_t , flags : c_int ) -> * mut c_void ;
141
+ fn je_xallocx ( ptr : * mut c_void , size : size_t , extra : size_t , flags : c_int ) -> size_t ;
138
142
fn je_sdallocx ( ptr : * mut c_void , size : size_t , flags : c_int ) ;
139
143
fn je_nallocx ( size : size_t , flags : c_int ) -> size_t ;
140
144
fn je_malloc_stats_print ( write_cb : Option < extern "C" fn ( cbopaque : * mut c_void ,
@@ -160,21 +164,13 @@ mod imp {
160
164
#[ inline]
161
165
pub unsafe fn allocate ( size : uint , align : uint ) -> * mut u8 {
162
166
let flags = align_to_flags ( align) ;
163
- let ptr = je_mallocx ( size as size_t , flags) as * mut u8 ;
164
- if ptr. is_null ( ) {
165
- :: oom ( )
166
- }
167
- ptr
167
+ je_mallocx ( size as size_t , flags) as * mut u8
168
168
}
169
169
170
170
#[ inline]
171
171
pub unsafe fn reallocate ( ptr : * mut u8 , _old_size : uint , size : uint , align : uint ) -> * mut u8 {
172
172
let flags = align_to_flags ( align) ;
173
- let ptr = je_rallocx ( ptr as * mut c_void , size as size_t , flags) as * mut u8 ;
174
- if ptr. is_null ( ) {
175
- :: oom ( )
176
- }
177
- ptr
173
+ je_rallocx ( ptr as * mut c_void , size as size_t , flags) as * mut u8
178
174
}
179
175
180
176
#[ inline]
@@ -207,7 +203,6 @@ mod imp {
207
203
mod imp {
208
204
use core:: cmp;
209
205
use core:: ptr;
210
- use core:: ptr:: RawPtr ;
211
206
use libc;
212
207
use super :: MIN_ALIGN ;
213
208
@@ -220,31 +215,24 @@ mod imp {
220
215
#[ inline]
221
216
pub unsafe fn allocate ( size : uint , align : uint ) -> * mut u8 {
222
217
if align <= MIN_ALIGN {
223
- let ptr = libc:: malloc ( size as libc:: size_t ) ;
224
- if ptr. is_null ( ) {
225
- :: oom ( ) ;
226
- }
227
- ptr as * mut u8
218
+ libc:: malloc ( size as libc:: size_t ) as * mut u8
228
219
} else {
229
220
let mut out = 0 as * mut libc:: c_void ;
230
221
let ret = posix_memalign ( & mut out,
231
222
align as libc:: size_t ,
232
223
size as libc:: size_t ) ;
233
224
if ret != 0 {
234
- :: oom ( ) ;
225
+ ptr:: null_mut ( )
226
+ } else {
227
+ out as * mut u8
235
228
}
236
- out as * mut u8
237
229
}
238
230
}
239
231
240
232
#[ inline]
241
233
pub unsafe fn reallocate ( ptr : * mut u8 , old_size : uint , size : uint , align : uint ) -> * mut u8 {
242
234
if align <= MIN_ALIGN {
243
- let ptr = libc:: realloc ( ptr as * mut libc:: c_void , size as libc:: size_t ) ;
244
- if ptr. is_null ( ) {
245
- :: oom ( ) ;
246
- }
247
- ptr as * mut u8
235
+ libc:: realloc ( ptr as * mut libc:: c_void , size as libc:: size_t ) as * mut u8
248
236
} else {
249
237
let new_ptr = allocate ( size, align) ;
250
238
ptr:: copy_memory ( new_ptr, ptr as * const u8 , cmp:: min ( size, old_size) ) ;
@@ -276,7 +264,6 @@ mod imp {
276
264
mod imp {
277
265
use libc:: { c_void, size_t} ;
278
266
use libc;
279
- use core:: ptr:: RawPtr ;
280
267
use super :: MIN_ALIGN ;
281
268
282
269
extern {
@@ -289,35 +276,18 @@ mod imp {
289
276
#[ inline]
290
277
pub unsafe fn allocate ( size : uint , align : uint ) -> * mut u8 {
291
278
if align <= MIN_ALIGN {
292
- let ptr = libc:: malloc ( size as size_t ) ;
293
- if ptr. is_null ( ) {
294
- :: oom ( ) ;
295
- }
296
- ptr as * mut u8
279
+ libc:: malloc ( size as size_t ) as * mut u8
297
280
} else {
298
- let ptr = _aligned_malloc ( size as size_t , align as size_t ) ;
299
- if ptr. is_null ( ) {
300
- :: oom ( ) ;
301
- }
302
- ptr as * mut u8
281
+ _aligned_malloc ( size as size_t , align as size_t ) as * mut u8
303
282
}
304
283
}
305
284
306
285
#[ inline]
307
286
pub unsafe fn reallocate ( ptr : * mut u8 , _old_size : uint , size : uint , align : uint ) -> * mut u8 {
308
287
if align <= MIN_ALIGN {
309
- let ptr = libc:: realloc ( ptr as * mut c_void , size as size_t ) ;
310
- if ptr. is_null ( ) {
311
- :: oom ( ) ;
312
- }
313
- ptr as * mut u8
288
+ libc:: realloc ( ptr as * mut c_void , size as size_t ) as * mut u8
314
289
} else {
315
- let ptr = _aligned_realloc ( ptr as * mut c_void , size as size_t ,
316
- align as size_t ) ;
317
- if ptr. is_null ( ) {
318
- :: oom ( ) ;
319
- }
320
- ptr as * mut u8
290
+ _aligned_realloc ( ptr as * mut c_void , size as size_t , align as size_t ) as * mut u8
321
291
}
322
292
}
323
293
@@ -348,13 +318,15 @@ mod imp {
348
318
mod test {
349
319
extern crate test;
350
320
use self :: test:: Bencher ;
321
+ use core:: ptr:: RawPtr ;
351
322
use heap;
352
323
353
324
#[ test]
354
325
fn basic_reallocate_inplace_noop ( ) {
355
326
unsafe {
356
327
let size = 4000 ;
357
328
let ptr = heap:: allocate ( size, 8 ) ;
329
+ if ptr. is_null ( ) { :: oom ( ) }
358
330
let ret = heap:: reallocate_inplace ( ptr, size, size, 8 ) ;
359
331
heap:: deallocate ( ptr, size, 8 ) ;
360
332
assert_eq ! ( ret, heap:: usable_size( size, 8 ) ) ;
0 commit comments