fallible: Avoid using system malloc in servo.

This commit is contained in:
Emilio Cobos Álvarez 2017-09-10 16:53:42 +02:00
parent c85633f48e
commit 6661bfa838
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
3 changed files with 32 additions and 9 deletions

View file

@ -12,3 +12,14 @@ path = "lib.rs"
[dependencies]
smallvec = "0.4"
hashglobe = { path = "../hashglobe" }
# This crate effectively does nothing except if the `known_system_malloc`
# feature is specified.
#
# In that case, we actually call the system malloc functions to reserve space,
# otherwise we just let rust do its thing (aborting on OOM).
#
# This is effectively a stop-gap measure until we can do this properly in
# stable rust.
[features]
known_system_malloc = []

View file

@ -8,10 +8,9 @@ extern crate smallvec;
use hashglobe::FailedAllocationError;
use smallvec::Array;
use smallvec::SmallVec;
use std::mem;
use std::ptr::copy_nonoverlapping;
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;
@ -30,9 +29,12 @@ pub trait FallibleVec<T> {
impl<T> FallibleVec<T> for Vec<T> {
#[inline]
fn try_push(&mut self, val: T) -> Result<(), FailedAllocationError> {
if self.capacity() == self.len() {
try_double_vec(self)?;
debug_assert!(self.capacity() > self.len());
#[cfg(feature = "known_system_malloc")]
{
if self.capacity() == self.len() {
try_double_vec(self)?;
debug_assert!(self.capacity() > self.len());
}
}
self.push(val);
Ok(())
@ -41,9 +43,12 @@ impl<T> FallibleVec<T> for Vec<T> {
// Double the capacity of |vec|, or fail to do so due to lack of memory.
// Returns Ok(()) on success, Err(..) on failure.
#[cfg(feature = "known_system_malloc")]
#[inline(never)]
#[cold]
fn try_double_vec<T>(vec: &mut Vec<T>) -> Result<(), FailedAllocationError> {
use std::mem;
let old_ptr = vec.as_mut_ptr();
let old_len = vec.len();
@ -89,9 +94,12 @@ fn try_double_vec<T>(vec: &mut Vec<T>) -> Result<(), FailedAllocationError> {
impl<T: Array> FallibleVec<T::Item> for SmallVec<T> {
#[inline]
fn try_push(&mut self, val: T::Item) -> Result<(), FailedAllocationError> {
if self.capacity() == self.len() {
try_double_small_vec(self)?;
debug_assert!(self.capacity() > self.len());
#[cfg(feature = "known_system_malloc")]
{
if self.capacity() == self.len() {
try_double_small_vec(self)?;
debug_assert!(self.capacity() > self.len());
}
}
self.push(val);
Ok(())
@ -100,6 +108,7 @@ impl<T: Array> FallibleVec<T::Item> for SmallVec<T> {
// Double the capacity of |svec|, or fail to do so due to lack of memory.
// Returns Ok(()) on success, Err(..) on failure.
#[cfg(feature = "known_system_malloc")]
#[inline(never)]
#[cold]
fn try_double_small_vec<T>(svec: &mut SmallVec<T>)
@ -107,6 +116,9 @@ fn try_double_small_vec<T>(svec: &mut SmallVec<T>)
where
T: Array,
{
use std::mem;
use std::ptr::copy_nonoverlapping;
let old_ptr = svec.as_mut_ptr();
let old_len = svec.len();

View file

@ -16,7 +16,7 @@ path = "lib.rs"
doctest = false
[features]
gecko = ["nsstring_vendor", "num_cpus", "style_traits/gecko"]
gecko = ["nsstring_vendor", "num_cpus", "style_traits/gecko", "fallible/known_system_malloc"]
use_bindgen = ["bindgen", "regex", "toml"]
servo = ["serde", "heapsize", "heapsize_derive",
"style_traits/servo", "servo_atoms", "servo_config", "html5ever",