handle zero-size allocations correctly

The `malloc` family of functions may return a null pointer for a
zero-size allocation, which should not be interpreted as an
out-of-memory error.

If the implementation does not return a null pointer, then handling
this will result in memory savings for zero-size types.

This also switches some code to `malloc_raw` in order to maintain a
centralized point for handling out-of-memory in `rt::global_heap`.

Closes #11634
This commit is contained in:
Daniel Micay 2014-01-17 20:45:48 -05:00
parent aa67e13498
commit ae2a5ecbf6
7 changed files with 42 additions and 37 deletions

View file

@ -167,7 +167,7 @@ mod imp {
use libc::c_void;
use libc;
use ptr;
use ptr::RawPtr;
use rt::global_heap::malloc_raw;
type pthread_mutex_t = libc::c_void;
type pthread_mutexattr_t = libc::c_void;
@ -175,16 +175,14 @@ mod imp {
type pthread_condattr_t = libc::c_void;
pub unsafe fn init_lock() -> uint {
let block = libc::malloc(rust_pthread_mutex_t_size() as libc::size_t);
assert!(!block.is_null());
let block = malloc_raw(rust_pthread_mutex_t_size() as uint) as *c_void;
let n = pthread_mutex_init(block, ptr::null());
assert_eq!(n, 0);
return block as uint;
}
pub unsafe fn init_cond() -> uint {
let block = libc::malloc(rust_pthread_cond_t_size() as libc::size_t);
assert!(!block.is_null());
let block = malloc_raw(rust_pthread_cond_t_size() as uint) as *c_void;
let n = pthread_cond_init(block, ptr::null());
assert_eq!(n, 0);
return block as uint;
@ -249,14 +247,13 @@ mod imp {
use libc;
use libc::{HANDLE, BOOL, LPSECURITY_ATTRIBUTES, c_void, DWORD, LPCSTR};
use ptr;
use ptr::RawPtr;
use rt::global_heap::malloc_raw;
type LPCRITICAL_SECTION = *c_void;
static SPIN_COUNT: DWORD = 4000;
pub unsafe fn init_lock() -> uint {
let block = libc::malloc(rust_crit_section_size() as libc::size_t);
assert!(!block.is_null());
let block = malloc_raw(rust_crit_section_size() as uint) as *c_void;
InitializeCriticalSectionAndSpinCount(block, SPIN_COUNT);
return block as uint;
}