@@ -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 also
32
- /// be the value returned by ` usable_size` for the requested size .
31
+ /// create the allocation referenced by `ptr`. The `old_size` parameter may be
32
+ /// any value in range_inclusive(requested_size, usable_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
- /// Returns true if successful, otherwise false if the allocation was not
42
- /// altered .
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)` .
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 ) -> bool {
52
+ pub unsafe fn reallocate_inplace ( ptr : * mut u8 , old_size : uint , size : uint , align : uint ) -> uint {
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 `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 .
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) .
63
63
#[ inline]
64
- pub unsafe fn deallocate ( ptr : * mut u8 , size : uint , align : uint ) {
65
- imp:: deallocate ( ptr, size , align)
64
+ pub unsafe fn deallocate ( ptr : * mut u8 , old_size : uint , align : uint ) {
65
+ imp:: deallocate ( ptr, old_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 , size : uint , align : uint ) {
106
- deallocate ( ptr, size , align) ;
105
+ unsafe fn exchange_free ( ptr : * mut u8 , old_size : uint , align : uint ) {
106
+ deallocate ( ptr, old_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, size: uint, align: uint) {
112
112
#[ cfg( any( target_arch = "arm" ,
113
113
target_arch = "mips" ,
114
114
target_arch = "mipsel" ) ) ]
115
- static MIN_ALIGN : uint = 8 ;
115
+ const MIN_ALIGN : uint = 8 ;
116
116
#[ cfg( any( target_arch = "x86" ,
117
117
target_arch = "x86_64" ) ) ]
118
- static MIN_ALIGN : uint = 16 ;
118
+ const MIN_ALIGN : uint = 16 ;
119
119
120
120
#[ cfg( jemalloc) ]
121
121
mod imp {
@@ -178,22 +178,16 @@ 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 ) -> bool {
181
+ pub unsafe fn reallocate_inplace ( ptr : * mut u8 , _old_size : uint , size : uint ,
182
+ align : uint ) -> uint {
183
183
let flags = align_to_flags ( align) ;
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
- }
184
+ je_xallocx ( ptr as * mut c_void , size as size_t , 0 , flags) as uint
191
185
}
192
186
193
187
#[ inline]
194
- pub unsafe fn deallocate ( ptr : * mut u8 , size : uint , align : uint ) {
188
+ pub unsafe fn deallocate ( ptr : * mut u8 , old_size : uint , align : uint ) {
195
189
let flags = align_to_flags ( align) ;
196
- je_sdallocx ( ptr as * mut c_void , size as size_t , flags)
190
+ je_sdallocx ( ptr as * mut c_void , old_size as size_t , flags)
197
191
}
198
192
199
193
#[ inline]
@@ -213,8 +207,8 @@ mod imp {
213
207
mod imp {
214
208
use core:: cmp;
215
209
use core:: ptr;
210
+ use core:: ptr:: RawPtr ;
216
211
use libc;
217
- use libc_heap;
218
212
use super :: MIN_ALIGN ;
219
213
220
214
extern {
@@ -226,7 +220,11 @@ mod imp {
226
220
#[ inline]
227
221
pub unsafe fn allocate ( size : uint , align : uint ) -> * mut u8 {
228
222
if align <= MIN_ALIGN {
229
- libc_heap:: malloc_raw ( size)
223
+ let ptr = libc:: malloc ( size as libc:: size_t ) ;
224
+ if ptr. is_null ( ) {
225
+ :: oom ( ) ;
226
+ }
227
+ ptr as * mut u8
230
228
} else {
231
229
let mut out = 0 as * mut libc:: c_void ;
232
230
let ret = posix_memalign ( & mut out,
@@ -242,7 +240,11 @@ mod imp {
242
240
#[ inline]
243
241
pub unsafe fn reallocate ( ptr : * mut u8 , old_size : uint , size : uint , align : uint ) -> * mut u8 {
244
242
if align <= MIN_ALIGN {
245
- libc_heap:: realloc_raw ( ptr, size)
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
246
248
} else {
247
249
let new_ptr = allocate ( size, align) ;
248
250
ptr:: copy_memory ( new_ptr, ptr as * const u8 , cmp:: min ( size, old_size) ) ;
@@ -252,13 +254,13 @@ mod imp {
252
254
}
253
255
254
256
#[ inline]
255
- pub unsafe fn reallocate_inplace ( _ptr : * mut u8 , old_size : uint , size : uint ,
256
- _align : uint ) -> bool {
257
- size == old_size
257
+ pub unsafe fn reallocate_inplace ( _ptr : * mut u8 , old_size : uint , _size : uint ,
258
+ _align : uint ) -> uint {
259
+ old_size
258
260
}
259
261
260
262
#[ inline]
261
- pub unsafe fn deallocate ( ptr : * mut u8 , _size : uint , _align : uint ) {
263
+ pub unsafe fn deallocate ( ptr : * mut u8 , _old_size : uint , _align : uint ) {
262
264
libc:: free ( ptr as * mut libc:: c_void )
263
265
}
264
266
@@ -274,7 +276,6 @@ mod imp {
274
276
mod imp {
275
277
use libc:: { c_void, size_t} ;
276
278
use libc;
277
- use libc_heap;
278
279
use core:: ptr:: RawPtr ;
279
280
use super :: MIN_ALIGN ;
280
281
@@ -288,7 +289,11 @@ mod imp {
288
289
#[ inline]
289
290
pub unsafe fn allocate ( size : uint , align : uint ) -> * mut u8 {
290
291
if align <= MIN_ALIGN {
291
- libc_heap:: malloc_raw ( size)
292
+ let ptr = libc:: malloc ( size as size_t ) ;
293
+ if ptr. is_null ( ) {
294
+ :: oom ( ) ;
295
+ }
296
+ ptr as * mut u8
292
297
} else {
293
298
let ptr = _aligned_malloc ( size as size_t , align as size_t ) ;
294
299
if ptr. is_null ( ) {
@@ -301,7 +306,11 @@ mod imp {
301
306
#[ inline]
302
307
pub unsafe fn reallocate ( ptr : * mut u8 , _old_size : uint , size : uint , align : uint ) -> * mut u8 {
303
308
if align <= MIN_ALIGN {
304
- libc_heap:: realloc_raw ( ptr, size)
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
305
314
} else {
306
315
let ptr = _aligned_realloc ( ptr as * mut c_void , size as size_t ,
307
316
align as size_t ) ;
@@ -313,13 +322,13 @@ mod imp {
313
322
}
314
323
315
324
#[ inline]
316
- pub unsafe fn reallocate_inplace ( _ptr : * mut u8 , old_size : uint , size : uint ,
317
- _align : uint ) -> bool {
318
- size == old_size
325
+ pub unsafe fn reallocate_inplace ( _ptr : * mut u8 , old_size : uint , _size : uint ,
326
+ _align : uint ) -> uint {
327
+ old_size
319
328
}
320
329
321
330
#[ inline]
322
- pub unsafe fn deallocate ( ptr : * mut u8 , _size : uint , align : uint ) {
331
+ pub unsafe fn deallocate ( ptr : * mut u8 , _old_size : uint , align : uint ) {
323
332
if align <= MIN_ALIGN {
324
333
libc:: free ( ptr as * mut libc:: c_void )
325
334
} else {
@@ -348,7 +357,7 @@ mod test {
348
357
let ptr = heap:: allocate ( size, 8 ) ;
349
358
let ret = heap:: reallocate_inplace ( ptr, size, size, 8 ) ;
350
359
heap:: deallocate ( ptr, size, 8 ) ;
351
- assert ! ( ret) ;
360
+ assert_eq ! ( ret, heap :: usable_size ( size , 8 ) ) ;
352
361
}
353
362
}
354
363
0 commit comments