From 6661bfa8381c46e160bd5931d064e1cf320a118b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sun, 10 Sep 2017 16:53:42 +0200 Subject: [PATCH] fallible: Avoid using system malloc in servo. --- components/fallible/Cargo.toml | 11 +++++++++++ components/fallible/lib.rs | 28 ++++++++++++++++++++-------- components/style/Cargo.toml | 2 +- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/components/fallible/Cargo.toml b/components/fallible/Cargo.toml index a6080523901..eae094d5776 100644 --- a/components/fallible/Cargo.toml +++ b/components/fallible/Cargo.toml @@ -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 = [] diff --git a/components/fallible/lib.rs b/components/fallible/lib.rs index f43d3189e2b..816e20573f9 100644 --- a/components/fallible/lib.rs +++ b/components/fallible/lib.rs @@ -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 { impl FallibleVec for Vec { #[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 FallibleVec for Vec { // 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(vec: &mut Vec) -> 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(vec: &mut Vec) -> Result<(), FailedAllocationError> { impl FallibleVec for SmallVec { #[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 FallibleVec for SmallVec { // 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(svec: &mut SmallVec) @@ -107,6 +116,9 @@ fn try_double_small_vec(svec: &mut SmallVec) where T: Array, { + use std::mem; + use std::ptr::copy_nonoverlapping; + let old_ptr = svec.as_mut_ptr(); let old_len = svec.len(); diff --git a/components/style/Cargo.toml b/components/style/Cargo.toml index 7c46a2c69bd..d2c1b13892c 100644 --- a/components/style/Cargo.toml +++ b/components/style/Cargo.toml @@ -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",