88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
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 } ;
1212use managed:: raw:: { BoxHeaderRepr , BoxRepr } ;
1313use unstable:: intrinsics:: TyDesc ;
1414use sys:: size_of;
@@ -18,6 +18,7 @@ extern {
1818 fn abort ( ) ;
1919}
2020
21+ #[ inline]
2122fn get_box_size ( body_size : uint , body_align : uint ) -> uint {
2223 let header_size = size_of :: < BoxHeaderRepr > ( ) ;
2324 // 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 {
2728
2829// Rounds |size| to the nearest |alignment|. Invariant: |alignment| is a power
2930// of two.
31+ #[ inline]
3032fn align_to ( size : uint , align : uint ) -> uint {
3133 assert ! ( align != 0 ) ;
3234 ( size + align - 1 ) & !( align - 1 )
3335}
3436
3537/// A wrapper around libc::malloc, aborting on out-of-memory
38+ #[ inline]
3639pub unsafe fn malloc_raw ( size : uint ) -> * c_void {
3740 let p = malloc ( size as size_t ) ;
3841 if p. is_null ( ) {
@@ -42,6 +45,17 @@ pub unsafe fn malloc_raw(size: uint) -> *c_void {
4245 p
4346}
4447
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+
4559// FIXME #4942: Make these signatures agree with exchange_alloc's signatures
4660#[ cfg( stage0, not( test) ) ]
4761#[ lang="exchange_malloc" ]
@@ -66,13 +80,8 @@ pub unsafe fn exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char {
6680#[ cfg( not( stage0) , not( test) ) ]
6781#[ lang="exchange_malloc" ]
6882#[ 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 ) ;
7685 malloc_raw ( total_size as uint ) as * c_char
7786}
7887
0 commit comments