Skip to content

Commit

Permalink
get rid of libc_heap::{malloc_raw, realloc_raw}
Browse files Browse the repository at this point in the history
The C standard library functions should be used directly. The quirky
NULL / zero-size allocation workaround is no longer necessary and was
adding an extra branch to the allocator code path in a build without
jemalloc. This is a small step towards liballoc being compatible with
handling OOM errors instead of aborting (#18292).

[breaking-change]
  • Loading branch information
thestinger committed Oct 25, 2014
1 parent 6f253bd commit 2bc4d3e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 66 deletions.
27 changes: 21 additions & 6 deletions src/liballoc/heap.rs
Expand Up @@ -213,8 +213,8 @@ mod imp {
mod imp {
use core::cmp;
use core::ptr;
use core::ptr::RawPtr;
use libc;
use libc_heap;
use super::MIN_ALIGN;

extern {
Expand All @@ -226,7 +226,11 @@ mod imp {
#[inline]
pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
if align <= MIN_ALIGN {
libc_heap::malloc_raw(size)
let ptr = libc::malloc(size as libc::size_t);
if ptr.is_null() {
::oom();
}
ptr as *mut u8
} else {
let mut out = 0 as *mut libc::c_void;
let ret = posix_memalign(&mut out,
Expand All @@ -242,7 +246,11 @@ mod imp {
#[inline]
pub unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> *mut u8 {
if align <= MIN_ALIGN {
libc_heap::realloc_raw(ptr, size)
let ptr = libc::realloc(ptr as *mut libc::c_void, size as libc::size_t);
if ptr.is_null() {
::oom();
}
ptr as *mut u8
} else {
let new_ptr = allocate(size, align);
ptr::copy_memory(new_ptr, ptr as *const u8, cmp::min(size, old_size));
Expand Down Expand Up @@ -274,7 +282,6 @@ mod imp {
mod imp {
use libc::{c_void, size_t};
use libc;
use libc_heap;
use core::ptr::RawPtr;
use super::MIN_ALIGN;

Expand All @@ -288,7 +295,11 @@ mod imp {
#[inline]
pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
if align <= MIN_ALIGN {
libc_heap::malloc_raw(size)
let ptr = libc::malloc(size as size_t);
if ptr.is_null() {
::oom();
}
ptr as *mut u8
} else {
let ptr = _aligned_malloc(size as size_t, align as size_t);
if ptr.is_null() {
Expand All @@ -301,7 +312,11 @@ mod imp {
#[inline]
pub unsafe fn reallocate(ptr: *mut u8, _old_size: uint, size: uint, align: uint) -> *mut u8 {
if align <= MIN_ALIGN {
libc_heap::realloc_raw(ptr, size)
let ptr = libc::realloc(ptr as *mut c_void, size as size_t);
if ptr.is_null() {
::oom();
}
ptr as *mut u8
} else {
let ptr = _aligned_realloc(ptr as *mut c_void, size as size_t,
align as size_t);
Expand Down
8 changes: 2 additions & 6 deletions src/liballoc/lib.rs
Expand Up @@ -54,11 +54,8 @@
//!
//! ## Heap interfaces
//!
//! The [`heap`](heap/index.html) and [`libc_heap`](libc_heap/index.html)
//! modules are the unsafe interfaces to the underlying allocation systems. The
//! `heap` module is considered the default heap, and is not necessarily backed
//! by libc malloc/free. The `libc_heap` module is defined to be wired up to
//! the system malloc/free.
//! The [`heap`](heap/index.html) module defines the low-level interface to the
//! default global allocator. It is not compatible with the libc allocator API.

#![crate_name = "alloc"]
#![experimental]
Expand Down Expand Up @@ -90,7 +87,6 @@ pub use boxed as owned;
// Heaps provided for low-level allocation strategies

pub mod heap;
pub mod libc_heap;

// Primitive types using the heaps above

Expand Down
48 changes: 0 additions & 48 deletions src/liballoc/libc_heap.rs

This file was deleted.

7 changes: 4 additions & 3 deletions src/librustrt/c_str.rs
Expand Up @@ -71,7 +71,6 @@ fn main() {
*/

use alloc::libc_heap::malloc_raw;
use collections::string::String;
use collections::hash;
use core::fmt;
Expand Down Expand Up @@ -101,7 +100,8 @@ impl Clone for CString {
/// with C's allocator API, rather than the usual shallow clone.
fn clone(&self) -> CString {
let len = self.len() + 1;
let buf = unsafe { malloc_raw(len) } as *mut libc::c_char;
let buf = unsafe { libc::malloc(len as libc::size_t) } as *mut libc::c_char;
if buf.is_null() { fail!("out of memory") }
unsafe { ptr::copy_nonoverlapping_memory(buf, self.buf, len); }
CString { buf: buf as *const libc::c_char, owns_buffer_: true }
}
Expand Down Expand Up @@ -393,7 +393,8 @@ impl<'a> ToCStr for &'a [u8] {

unsafe fn to_c_str_unchecked(&self) -> CString {
let self_len = self.len();
let buf = malloc_raw(self_len + 1);
let buf = libc::malloc(self_len as libc::size_t + 1) as *mut u8;
if buf.is_null() { fail!("out of memory") }

ptr::copy_memory(buf, self.as_ptr(), self_len);
*buf.offset(self_len as int) = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/c_vec.rs
Expand Up @@ -165,11 +165,11 @@ mod tests {
use super::CVec;
use libc;
use ptr;
use rt::libc_heap::malloc_raw;

fn malloc(n: uint) -> CVec<u8> {
unsafe {
let mem = malloc_raw(n);
let mem = libc::malloc(n as libc::size_t);
if mem.is_null() { fail!("out of memory") }

CVec::new_with_dtor(mem as *mut u8, n,
proc() { libc::free(mem as *mut libc::c_void); })
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/rt/mod.rs
Expand Up @@ -62,7 +62,7 @@ pub use self::util::{default_sched_threads, min_stack, running_on_valgrind};

// Reexport functionality from librustrt and other crates underneath the
// standard library which work together to create the entire runtime.
pub use alloc::{heap, libc_heap};
pub use alloc::heap;
pub use rustrt::{task, local, mutex, exclusive, stack, args, rtio, thread};
pub use rustrt::{Stdio, Stdout, Stderr, begin_unwind, begin_unwind_fmt};
pub use rustrt::{bookkeeping, at_exit, unwind, DEFAULT_ERROR_CODE, Runtime};
Expand Down

0 comments on commit 2bc4d3e

Please sign in to comment.