@@ -28,8 +28,8 @@ pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
28
28
/// size on the platform.
29
29
///
30
30
/// The `old_size` and `align` parameters are the parameters that were used to
31
- /// create the allocation referenced by `ptr`. The `old_size` parameter may be
32
- /// any value in range_inclusive(requested_size, usable_size) .
31
+ /// create the allocation referenced by `ptr`. The `old_size` parameter may also
32
+ /// be the value returned by ` usable_size` for the requested size .
33
33
#[ inline]
34
34
pub unsafe fn reallocate ( ptr : * mut u8 , old_size : uint , size : uint , align : uint ) -> * mut u8 {
35
35
imp:: reallocate ( ptr, old_size, size, align)
@@ -38,8 +38,8 @@ pub unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint)
38
38
/// Extends or shrinks the allocation referenced by `ptr` to `size` bytes of
39
39
/// memory in-place.
40
40
///
41
- /// If the operation succeeds, it returns `usable_size(size, align)` and if it
42
- /// fails (or is a no-op) it returns `usable_size(old_size, align)` .
41
+ /// Returns true if successful, otherwise false if the allocation was not
42
+ /// altered .
43
43
///
44
44
/// Behavior is undefined if the requested size is 0 or the alignment is not a
45
45
/// power of 2. The alignment must be no larger than the largest supported page
@@ -49,20 +49,20 @@ pub unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint)
49
49
/// create the allocation referenced by `ptr`. The `old_size` parameter may be
50
50
/// any value in range_inclusive(requested_size, usable_size).
51
51
#[ inline]
52
- pub unsafe fn reallocate_inplace ( ptr : * mut u8 , old_size : uint , size : uint , align : uint ) -> uint {
52
+ pub unsafe fn reallocate_inplace ( ptr : * mut u8 , old_size : uint , size : uint , align : uint ) -> bool {
53
53
imp:: reallocate_inplace ( ptr, old_size, size, align)
54
54
}
55
55
56
56
/// Deallocates the memory referenced by `ptr`.
57
57
///
58
58
/// The `ptr` parameter must not be null.
59
59
///
60
- /// The `old_size ` and `align` parameters are the parameters that were used to
61
- /// create the allocation referenced by `ptr`. The `old_size ` parameter may be
62
- /// any value in range_inclusive(requested_size, usable_size) .
60
+ /// The `size ` and `align` parameters are the parameters that were used to
61
+ /// create the allocation referenced by `ptr`. The `size ` parameter may also be
62
+ /// the value returned by ` usable_size` for the requested size .
63
63
#[ inline]
64
- pub unsafe fn deallocate ( ptr : * mut u8 , old_size : uint , align : uint ) {
65
- imp:: deallocate ( ptr, old_size , align)
64
+ pub unsafe fn deallocate ( ptr : * mut u8 , size : uint , align : uint ) {
65
+ imp:: deallocate ( ptr, size , align)
66
66
}
67
67
68
68
/// Returns the usable size of an allocation created with the specified the
@@ -102,8 +102,8 @@ unsafe fn exchange_malloc(size: uint, align: uint) -> *mut u8 {
102
102
#[ cfg( not( test) ) ]
103
103
#[ lang="exchange_free" ]
104
104
#[ inline]
105
- unsafe fn exchange_free ( ptr : * mut u8 , old_size : uint , align : uint ) {
106
- deallocate ( ptr, old_size , align) ;
105
+ unsafe fn exchange_free ( ptr : * mut u8 , size : uint , align : uint ) {
106
+ deallocate ( ptr, size , align) ;
107
107
}
108
108
109
109
// The minimum alignment guaranteed by the architecture. This value is used to
@@ -112,10 +112,10 @@ unsafe fn exchange_free(ptr: *mut u8, old_size: uint, align: uint) {
112
112
#[ cfg( any( target_arch = "arm" ,
113
113
target_arch = "mips" ,
114
114
target_arch = "mipsel" ) ) ]
115
- const MIN_ALIGN : uint = 8 ;
115
+ static MIN_ALIGN : uint = 8 ;
116
116
#[ cfg( any( target_arch = "x86" ,
117
117
target_arch = "x86_64" ) ) ]
118
- const MIN_ALIGN : uint = 16 ;
118
+ static MIN_ALIGN : uint = 16 ;
119
119
120
120
#[ cfg( jemalloc) ]
121
121
mod imp {
@@ -178,16 +178,22 @@ mod imp {
178
178
}
179
179
180
180
#[ inline]
181
- pub unsafe fn reallocate_inplace ( ptr : * mut u8 , _old_size : uint , size : uint ,
182
- align : uint ) -> uint {
181
+ pub unsafe fn reallocate_inplace ( ptr : * mut u8 , old_size : uint , size : uint ,
182
+ align : uint ) -> bool {
183
183
let flags = align_to_flags ( align) ;
184
- je_xallocx ( ptr as * mut c_void , size as size_t , 0 , flags) as uint
184
+ let new_size = je_xallocx ( ptr as * mut c_void , size as size_t , 0 , flags) as uint ;
185
+ // checking for failure to shrink is tricky
186
+ if size < old_size {
187
+ usable_size ( size, align) == new_size as uint
188
+ } else {
189
+ new_size >= size
190
+ }
185
191
}
186
192
187
193
#[ inline]
188
- pub unsafe fn deallocate ( ptr : * mut u8 , old_size : uint , align : uint ) {
194
+ pub unsafe fn deallocate ( ptr : * mut u8 , size : uint , align : uint ) {
189
195
let flags = align_to_flags ( align) ;
190
- je_sdallocx ( ptr as * mut c_void , old_size as size_t , flags)
196
+ je_sdallocx ( ptr as * mut c_void , size as size_t , flags)
191
197
}
192
198
193
199
#[ inline]
@@ -207,8 +213,8 @@ mod imp {
207
213
mod imp {
208
214
use core:: cmp;
209
215
use core:: ptr;
210
- use core:: ptr:: RawPtr ;
211
216
use libc;
217
+ use libc_heap;
212
218
use super :: MIN_ALIGN ;
213
219
214
220
extern {
@@ -220,11 +226,7 @@ mod imp {
220
226
#[ inline]
221
227
pub unsafe fn allocate ( size : uint , align : uint ) -> * mut u8 {
222
228
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
229
+ libc_heap:: malloc_raw ( size)
228
230
} else {
229
231
let mut out = 0 as * mut libc:: c_void ;
230
232
let ret = posix_memalign ( & mut out,
@@ -240,11 +242,7 @@ mod imp {
240
242
#[ inline]
241
243
pub unsafe fn reallocate ( ptr : * mut u8 , old_size : uint , size : uint , align : uint ) -> * mut u8 {
242
244
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
245
+ libc_heap:: realloc_raw ( ptr, size)
248
246
} else {
249
247
let new_ptr = allocate ( size, align) ;
250
248
ptr:: copy_memory ( new_ptr, ptr as * const u8 , cmp:: min ( size, old_size) ) ;
@@ -254,13 +252,13 @@ mod imp {
254
252
}
255
253
256
254
#[ inline]
257
- pub unsafe fn reallocate_inplace ( _ptr : * mut u8 , old_size : uint , _size : uint ,
258
- _align : uint ) -> uint {
259
- old_size
255
+ pub unsafe fn reallocate_inplace ( _ptr : * mut u8 , old_size : uint , size : uint ,
256
+ _align : uint ) -> bool {
257
+ size == old_size
260
258
}
261
259
262
260
#[ inline]
263
- pub unsafe fn deallocate ( ptr : * mut u8 , _old_size : uint , _align : uint ) {
261
+ pub unsafe fn deallocate ( ptr : * mut u8 , _size : uint , _align : uint ) {
264
262
libc:: free ( ptr as * mut libc:: c_void )
265
263
}
266
264
@@ -276,6 +274,7 @@ mod imp {
276
274
mod imp {
277
275
use libc:: { c_void, size_t} ;
278
276
use libc;
277
+ use libc_heap;
279
278
use core:: ptr:: RawPtr ;
280
279
use super :: MIN_ALIGN ;
281
280
@@ -289,11 +288,7 @@ mod imp {
289
288
#[ inline]
290
289
pub unsafe fn allocate ( size : uint , align : uint ) -> * mut u8 {
291
290
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
291
+ libc_heap:: malloc_raw ( size)
297
292
} else {
298
293
let ptr = _aligned_malloc ( size as size_t , align as size_t ) ;
299
294
if ptr. is_null ( ) {
@@ -306,11 +301,7 @@ mod imp {
306
301
#[ inline]
307
302
pub unsafe fn reallocate ( ptr : * mut u8 , _old_size : uint , size : uint , align : uint ) -> * mut u8 {
308
303
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
304
+ libc_heap:: realloc_raw ( ptr, size)
314
305
} else {
315
306
let ptr = _aligned_realloc ( ptr as * mut c_void , size as size_t ,
316
307
align as size_t ) ;
@@ -322,13 +313,13 @@ mod imp {
322
313
}
323
314
324
315
#[ inline]
325
- pub unsafe fn reallocate_inplace ( _ptr : * mut u8 , old_size : uint , _size : uint ,
326
- _align : uint ) -> uint {
327
- old_size
316
+ pub unsafe fn reallocate_inplace ( _ptr : * mut u8 , old_size : uint , size : uint ,
317
+ _align : uint ) -> bool {
318
+ size == old_size
328
319
}
329
320
330
321
#[ inline]
331
- pub unsafe fn deallocate ( ptr : * mut u8 , _old_size : uint , align : uint ) {
322
+ pub unsafe fn deallocate ( ptr : * mut u8 , _size : uint , align : uint ) {
332
323
if align <= MIN_ALIGN {
333
324
libc:: free ( ptr as * mut libc:: c_void )
334
325
} else {
@@ -357,7 +348,7 @@ mod test {
357
348
let ptr = heap:: allocate ( size, 8 ) ;
358
349
let ret = heap:: reallocate_inplace ( ptr, size, size, 8 ) ;
359
350
heap:: deallocate ( ptr, size, 8 ) ;
360
- assert_eq ! ( ret, heap :: usable_size ( size , 8 ) ) ;
351
+ assert ! ( ret) ;
361
352
}
362
353
}
363
354
0 commit comments