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:
parent
aa67e13498
commit
ae2a5ecbf6
7 changed files with 42 additions and 37 deletions
|
|
@ -19,6 +19,7 @@ use std::rt::task::BlockedTask;
|
|||
use std::str;
|
||||
use std::unstable::finally::Finally;
|
||||
use std::vec;
|
||||
use std::rt::global_heap::malloc_raw;
|
||||
|
||||
use homing::{HomingIO, HomeHandle};
|
||||
use stream::StreamWatcher;
|
||||
|
|
@ -122,8 +123,7 @@ fn socket_name(sk: SocketNameKind, handle: *c_void) -> Result<SocketAddr, IoErro
|
|||
// Allocate a sockaddr_storage
|
||||
// since we don't know if it's ipv4 or ipv6
|
||||
let size = uvll::rust_sockaddr_size();
|
||||
let name = libc::malloc(size as size_t);
|
||||
assert!(!name.is_null());
|
||||
let name = malloc_raw(size as uint) as *c_void;
|
||||
let mut namelen = size;
|
||||
|
||||
let ret = match getsockname(handle, name, &mut namelen) {
|
||||
|
|
|
|||
|
|
@ -31,8 +31,9 @@
|
|||
|
||||
use std::libc::{size_t, c_int, c_uint, c_void, c_char, c_double};
|
||||
use std::libc::ssize_t;
|
||||
use std::libc::{malloc, free};
|
||||
use std::libc::free;
|
||||
use std::libc;
|
||||
use std::rt::global_heap::malloc_raw;
|
||||
|
||||
#[cfg(test)]
|
||||
use std::libc::uintptr_t;
|
||||
|
|
@ -374,9 +375,7 @@ pub enum uv_membership {
|
|||
pub unsafe fn malloc_handle(handle: uv_handle_type) -> *c_void {
|
||||
assert!(handle != UV_UNKNOWN_HANDLE && handle != UV_HANDLE_TYPE_MAX);
|
||||
let size = uv_handle_size(handle);
|
||||
let p = malloc(size);
|
||||
assert!(p.is_not_null());
|
||||
return p;
|
||||
malloc_raw(size as uint) as *c_void
|
||||
}
|
||||
|
||||
pub unsafe fn free_handle(v: *c_void) {
|
||||
|
|
@ -386,9 +385,7 @@ pub unsafe fn free_handle(v: *c_void) {
|
|||
pub unsafe fn malloc_req(req: uv_req_type) -> *c_void {
|
||||
assert!(req != UV_UNKNOWN_REQ && req != UV_REQ_TYPE_MAX);
|
||||
let size = uv_req_size(req);
|
||||
let p = malloc(size);
|
||||
assert!(p.is_not_null());
|
||||
return p;
|
||||
malloc_raw(size as uint) as *c_void
|
||||
}
|
||||
|
||||
pub unsafe fn free_req(v: *c_void) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue