diff --git a/components/fallible/lib.rs b/components/fallible/lib.rs index 4a48190a1eb..c220699a786 100644 --- a/components/fallible/lib.rs +++ b/components/fallible/lib.rs @@ -6,16 +6,12 @@ extern crate hashglobe; extern crate smallvec; use hashglobe::FailedAllocationError; +#[cfg(feature = "known_system_malloc")] +use hashglobe::alloc; use smallvec::Array; use smallvec::SmallVec; use std::vec::Vec; -#[cfg(feature = "known_system_malloc")] -extern "C" { - fn realloc(ptr: *mut u8, bytes: usize) -> *mut u8; - fn malloc(bytes: usize) -> *mut u8; -} - pub trait FallibleVec { /// Append |val| to the end of |vec|. Returns Ok(()) on success, /// Err(reason) if it fails, with |reason| describing the failure. @@ -67,9 +63,9 @@ fn try_double_vec(vec: &mut Vec) -> Result<(), FailedAllocationError> { let new_ptr = unsafe { if old_cap == 0 { - malloc(new_size_bytes) + alloc::alloc(new_size_bytes, 0) } else { - realloc(old_ptr as *mut u8, new_size_bytes) + alloc::realloc(old_ptr as *mut u8, new_size_bytes) } }; @@ -146,12 +142,12 @@ where // There's an old block to free, and, presumably, old contents to // copy. realloc takes care of both aspects. unsafe { - new_ptr = realloc(old_ptr as *mut u8, new_size_bytes); + new_ptr = alloc::realloc(old_ptr as *mut u8, new_size_bytes); } } else { // There's no old block to free. There may be old contents to copy. unsafe { - new_ptr = malloc(new_size_bytes); + new_ptr = alloc::alloc(new_size_bytes, 0); if !new_ptr.is_null() && old_size_bytes > 0 { copy_nonoverlapping(old_ptr as *const u8, new_ptr as *mut u8, old_size_bytes); diff --git a/components/hashglobe/src/alloc.rs b/components/hashglobe/src/alloc.rs index c5e213b2931..f7d50501467 100644 --- a/components/hashglobe/src/alloc.rs +++ b/components/hashglobe/src/alloc.rs @@ -22,7 +22,7 @@ const MIN_ALIGN: usize = 8; target_arch = "sparc64")))] const MIN_ALIGN: usize = 16; -pub use self::platform::{alloc, dealloc}; +pub use self::platform::{alloc, dealloc, realloc}; #[cfg(any(unix, target_os = "redox"))] mod platform { @@ -47,6 +47,11 @@ mod platform { libc::free(ptr as *mut libc::c_void) } + #[inline] + pub unsafe fn realloc(ptr: *mut u8, new_size: usize) -> *mut u8 { + libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8 + } + #[cfg(any(target_os = "android", target_os = "redox"))] #[inline] unsafe fn aligned_malloc(size: usize, align: usize) -> *mut u8 { @@ -98,6 +103,7 @@ mod platform { extern "system" { fn GetProcessHeap() -> HANDLE; fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID; + fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID, dwBytes: SIZE_T) -> LPVOID; fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL; fn GetLastError() -> DWORD; } @@ -149,4 +155,12 @@ mod platform { GetLastError()); } } + + #[inline] + pub unsafe fn realloc(ptr: *mut u8, new_size: usize) -> *mut u8 { + HeapReAlloc(GetProcessHeap(), + 0, + ptr as LPVOID, + new_size) as *mut u8 + } } diff --git a/components/hashglobe/src/lib.rs b/components/hashglobe/src/lib.rs index cdb98ad2003..f9d5109a51d 100644 --- a/components/hashglobe/src/lib.rs +++ b/components/hashglobe/src/lib.rs @@ -10,7 +10,7 @@ extern crate heapsize; -mod alloc; +pub mod alloc; pub mod hash_map; pub mod hash_set; pub mod protected;