Skip to content

Commit 2bc4d3e

Browse files
committed
get rid of libc_heap::{malloc_raw, realloc_raw}
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]
1 parent 6f253bd commit 2bc4d3e

File tree

6 files changed

+30
-66
lines changed

6 files changed

+30
-66
lines changed

src/liballoc/heap.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ mod imp {
213213
mod imp {
214214
use core::cmp;
215215
use core::ptr;
216+
use core::ptr::RawPtr;
216217
use libc;
217-
use libc_heap;
218218
use super::MIN_ALIGN;
219219

220220
extern {
@@ -226,7 +226,11 @@ mod imp {
226226
#[inline]
227227
pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
228228
if align <= MIN_ALIGN {
229-
libc_heap::malloc_raw(size)
229+
let ptr = libc::malloc(size as libc::size_t);
230+
if ptr.is_null() {
231+
::oom();
232+
}
233+
ptr as *mut u8
230234
} else {
231235
let mut out = 0 as *mut libc::c_void;
232236
let ret = posix_memalign(&mut out,
@@ -242,7 +246,11 @@ mod imp {
242246
#[inline]
243247
pub unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> *mut u8 {
244248
if align <= MIN_ALIGN {
245-
libc_heap::realloc_raw(ptr, size)
249+
let ptr = libc::realloc(ptr as *mut libc::c_void, size as libc::size_t);
250+
if ptr.is_null() {
251+
::oom();
252+
}
253+
ptr as *mut u8
246254
} else {
247255
let new_ptr = allocate(size, align);
248256
ptr::copy_memory(new_ptr, ptr as *const u8, cmp::min(size, old_size));
@@ -274,7 +282,6 @@ mod imp {
274282
mod imp {
275283
use libc::{c_void, size_t};
276284
use libc;
277-
use libc_heap;
278285
use core::ptr::RawPtr;
279286
use super::MIN_ALIGN;
280287

@@ -288,7 +295,11 @@ mod imp {
288295
#[inline]
289296
pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
290297
if align <= MIN_ALIGN {
291-
libc_heap::malloc_raw(size)
298+
let ptr = libc::malloc(size as size_t);
299+
if ptr.is_null() {
300+
::oom();
301+
}
302+
ptr as *mut u8
292303
} else {
293304
let ptr = _aligned_malloc(size as size_t, align as size_t);
294305
if ptr.is_null() {
@@ -301,7 +312,11 @@ mod imp {
301312
#[inline]
302313
pub unsafe fn reallocate(ptr: *mut u8, _old_size: uint, size: uint, align: uint) -> *mut u8 {
303314
if align <= MIN_ALIGN {
304-
libc_heap::realloc_raw(ptr, size)
315+
let ptr = libc::realloc(ptr as *mut c_void, size as size_t);
316+
if ptr.is_null() {
317+
::oom();
318+
}
319+
ptr as *mut u8
305320
} else {
306321
let ptr = _aligned_realloc(ptr as *mut c_void, size as size_t,
307322
align as size_t);

src/liballoc/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,8 @@
5454
//!
5555
//! ## Heap interfaces
5656
//!
57-
//! The [`heap`](heap/index.html) and [`libc_heap`](libc_heap/index.html)
58-
//! modules are the unsafe interfaces to the underlying allocation systems. The
59-
//! `heap` module is considered the default heap, and is not necessarily backed
60-
//! by libc malloc/free. The `libc_heap` module is defined to be wired up to
61-
//! the system malloc/free.
57+
//! The [`heap`](heap/index.html) module defines the low-level interface to the
58+
//! default global allocator. It is not compatible with the libc allocator API.
6259
6360
#![crate_name = "alloc"]
6461
#![experimental]
@@ -90,7 +87,6 @@ pub use boxed as owned;
9087
// Heaps provided for low-level allocation strategies
9188

9289
pub mod heap;
93-
pub mod libc_heap;
9490

9591
// Primitive types using the heaps above
9692

src/liballoc/libc_heap.rs

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/librustrt/c_str.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ fn main() {
7171
7272
*/
7373

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

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

398399
ptr::copy_memory(buf, self.as_ptr(), self_len);
399400
*buf.offset(self_len as int) = 0;

src/libstd/c_vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ mod tests {
165165
use super::CVec;
166166
use libc;
167167
use ptr;
168-
use rt::libc_heap::malloc_raw;
169168

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

174174
CVec::new_with_dtor(mem as *mut u8, n,
175175
proc() { libc::free(mem as *mut libc::c_void); })

src/libstd/rt/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub use self::util::{default_sched_threads, min_stack, running_on_valgrind};
6262

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

0 commit comments

Comments
 (0)