Skip to content

Commit 80ab877

Browse files
committed
global_heap: inline malloc_raw and add realloc_raw
1 parent 040ac2a commit 80ab877

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/libstd/libc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1945,7 +1945,7 @@ pub mod funcs {
19451945
#[fast_ffi]
19461946
unsafe fn malloc(size: size_t) -> *c_void;
19471947
#[fast_ffi]
1948-
unsafe fn realloc(p: *c_void, size: size_t) -> *c_void;
1948+
unsafe fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
19491949
#[fast_ffi]
19501950
unsafe fn free(p: *c_void);
19511951
unsafe fn abort() -> !;

src/libstd/rt/global_heap.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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};
1212
use managed::raw::{BoxHeaderRepr, BoxRepr};
1313
use unstable::intrinsics::TyDesc;
1414
use sys::size_of;
@@ -33,6 +33,7 @@ fn align_to(size: uint, align: uint) -> uint {
3333
}
3434

3535
/// A wrapper around libc::malloc, aborting on out-of-memory
36+
#[inline]
3637
pub unsafe fn malloc_raw(size: uint) -> *c_void {
3738
let p = malloc(size as size_t);
3839
if p.is_null() {
@@ -42,6 +43,17 @@ pub unsafe fn malloc_raw(size: uint) -> *c_void {
4243
p
4344
}
4445

46+
/// A wrapper around libc::realloc, aborting on out-of-memory
47+
#[inline]
48+
pub unsafe fn realloc_raw(ptr: *mut c_void, size: uint) -> *mut c_void {
49+
let p = realloc(ptr, size as size_t);
50+
if p.is_null() {
51+
// we need a non-allocating way to print an error here
52+
abort();
53+
}
54+
p
55+
}
56+
4557
// FIXME #4942: Make these signatures agree with exchange_alloc's signatures
4658
#[cfg(stage0, not(test))]
4759
#[lang="exchange_malloc"]

0 commit comments

Comments
 (0)