8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- use libc:: { c_char, c_void, size_t, uintptr_t, free, malloc} ;
11
+ use libc:: { c_char, c_void, size_t, uintptr_t, free, malloc, realloc } ;
12
12
use managed:: raw:: { BoxHeaderRepr , BoxRepr } ;
13
13
use unstable:: intrinsics:: TyDesc ;
14
14
use sys:: size_of;
@@ -18,6 +18,7 @@ extern {
18
18
fn abort ( ) ;
19
19
}
20
20
21
+ #[ inline]
21
22
fn get_box_size ( body_size : uint , body_align : uint ) -> uint {
22
23
let header_size = size_of :: < BoxHeaderRepr > ( ) ;
23
24
// FIXME (#2699): This alignment calculation is suspicious. Is it right?
@@ -27,12 +28,14 @@ fn get_box_size(body_size: uint, body_align: uint) -> uint {
27
28
28
29
// Rounds |size| to the nearest |alignment|. Invariant: |alignment| is a power
29
30
// of two.
31
+ #[ inline]
30
32
fn align_to ( size : uint , align : uint ) -> uint {
31
33
assert ! ( align != 0 ) ;
32
34
( size + align - 1 ) & !( align - 1 )
33
35
}
34
36
35
37
/// A wrapper around libc::malloc, aborting on out-of-memory
38
+ #[ inline]
36
39
pub unsafe fn malloc_raw ( size : uint ) -> * c_void {
37
40
let p = malloc ( size as size_t ) ;
38
41
if p. is_null ( ) {
@@ -42,6 +45,17 @@ pub unsafe fn malloc_raw(size: uint) -> *c_void {
42
45
p
43
46
}
44
47
48
+ /// A wrapper around libc::realloc, aborting on out-of-memory
49
+ #[ inline]
50
+ pub unsafe fn realloc_raw ( ptr : * mut c_void , size : uint ) -> * mut c_void {
51
+ let p = realloc ( ptr, size as size_t ) ;
52
+ if p. is_null ( ) {
53
+ // we need a non-allocating way to print an error here
54
+ abort ( ) ;
55
+ }
56
+ p
57
+ }
58
+
45
59
// FIXME #4942: Make these signatures agree with exchange_alloc's signatures
46
60
#[ cfg( stage0, not( test) ) ]
47
61
#[ lang="exchange_malloc" ]
@@ -66,13 +80,8 @@ pub unsafe fn exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char {
66
80
#[ cfg( not( stage0) , not( test) ) ]
67
81
#[ lang="exchange_malloc" ]
68
82
#[ inline]
69
- pub unsafe fn exchange_malloc ( td : * c_char , size : uintptr_t ) -> * c_char {
70
- let td = td as * TyDesc ;
71
- let size = size as uint ;
72
-
73
- assert ! ( td. is_not_null( ) ) ;
74
-
75
- let total_size = get_box_size ( size, ( * td) . align ) ;
83
+ pub unsafe fn exchange_malloc ( align : u32 , size : uintptr_t ) -> * c_char {
84
+ let total_size = get_box_size ( size as uint , align as uint ) ;
76
85
malloc_raw ( total_size as uint ) as * c_char
77
86
}
78
87
0 commit comments