]> git.lizzy.rs Git - rust.git/commitdiff
global_heap: inline malloc_raw and add realloc_raw
authorDaniel Micay <danielmicay@gmail.com>
Mon, 1 Jul 2013 02:12:26 +0000 (22:12 -0400)
committerDaniel Micay <danielmicay@gmail.com>
Mon, 1 Jul 2013 02:22:52 +0000 (22:22 -0400)
src/libstd/libc.rs
src/libstd/rt/global_heap.rs

index 41b78afded1a8c3d483fc1256944e8eef2e1b694..f4ea29b5c05e45232c11569e3b622862eee458ac 100644 (file)
@@ -1945,7 +1945,7 @@ unsafe fn strtoul(s: *c_char, endp: **c_char, base: c_int)
                 #[fast_ffi]
                 unsafe fn malloc(size: size_t) -> *c_void;
                 #[fast_ffi]
-                unsafe fn realloc(p: *c_void, size: size_t) -> *c_void;
+                unsafe fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
                 #[fast_ffi]
                 unsafe fn free(p: *c_void);
                 unsafe fn abort() -> !;
index f669dc753d60df76a95ee22761de449b49678b97..a7fbed2dd70b773a743c67fbcd511a1ee396ea54 100644 (file)
@@ -8,7 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-use libc::{c_char, c_void, size_t, uintptr_t, free, malloc};
+use libc::{c_char, c_void, size_t, uintptr_t, free, malloc, realloc};
 use managed::raw::{BoxHeaderRepr, BoxRepr};
 use unstable::intrinsics::TyDesc;
 use sys::size_of;
@@ -33,6 +33,7 @@ fn align_to(size: uint, align: uint) -> uint {
 }
 
 /// A wrapper around libc::malloc, aborting on out-of-memory
+#[inline]
 pub unsafe fn malloc_raw(size: uint) -> *c_void {
     let p = malloc(size as size_t);
     if p.is_null() {
@@ -42,6 +43,17 @@ pub unsafe fn malloc_raw(size: uint) -> *c_void {
     p
 }
 
+/// A wrapper around libc::realloc, aborting on out-of-memory
+#[inline]
+pub unsafe fn realloc_raw(ptr: *mut c_void, size: uint) -> *mut c_void {
+    let p = realloc(ptr, size as size_t);
+    if p.is_null() {
+        // we need a non-allocating way to print an error here
+        abort();
+    }
+    p
+}
+
 // FIXME #4942: Make these signatures agree with exchange_alloc's signatures
 #[cfg(stage0, not(test))]
 #[lang="exchange_malloc"]