Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elfmalloc: Bubble up OOM errors, remove unnecessary internal code #160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion elfmalloc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- Copyright 2017 the authors. See the 'Copyright and license' section of the
<!-- Copyright 2017-2018 the authors. See the 'Copyright and license' section of the
README.md file at the top-level directory of this repository.

Licensed under the Apache License, Version 2.0 (the LICENSE-APACHE file) or
Expand All @@ -25,6 +25,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
`MagazineCache`
- Added `c-api` feature to optimize for the C malloc API

### Changed
- Changed error reporting so that more OOM errors are bubbled up and result
in top-level errors rather than panics

### Fixed
- Fixed a bug preventing non-nightly builds from compiling
- Fixed an integer multiplication overflow bug
Expand Down
4 changes: 2 additions & 2 deletions elfmalloc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017 the authors. See the 'Copyright and license' section of the
# Copyright 2017-2018 the authors. See the 'Copyright and license' section of the
# README.md file at the top-level directory of this repository.
#
# Licensed under the Apache License, Version 2.0 (the LICENSE-APACHE file) or
Expand Down Expand Up @@ -49,7 +49,7 @@ alloc-fmt = { path = "../alloc-fmt" }
alloc-tls = { path = "../alloc-tls" }
bagpipe = { path = "../bagpipe" }
bsalloc = "0.1.0"
lazy_static = "1.0.0"
lazy_static = { version = "1.0.0", features = ["spin_no_std"] }
libc = "0.2"
log = "0.3.8"
malloc-bind = { path = "../malloc-bind" }
Expand Down
16 changes: 9 additions & 7 deletions elfmalloc/src/alloc_impl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 the authors. See the 'Copyright and license' section of the
// Copyright 2017-2018 the authors. See the 'Copyright and license' section of the
// README.md file at the top-level directory of this repository.
//
// Licensed under the Apache License, Version 2.0 (the LICENSE-APACHE file) or
Expand All @@ -25,6 +25,8 @@ use super::general::global;
use std::mem;
#[cfg(feature = "c-api")]
use std::intrinsics::unlikely;
#[cfg(feature = "c-api")]
use std::ptr;

#[cfg(feature = "c-api")]
use self::libc::{size_t, c_void};
Expand All @@ -39,25 +41,25 @@ unsafe impl<'a> Alloc for &'a ElfMallocGlobal {
// two up to 1MiB are aligned to their size. Past that size, only page-alignment is
// guaranteed.
if l.size().is_power_of_two() || l.align() <= mem::size_of::<usize>() {
Ok(global::alloc(l.size()))
global::alloc(l.size())
} else {
Ok(global::alloc(l.size().next_power_of_two()))
}
global::alloc(l.size().next_power_of_two())
}.ok_or(AllocErr::Exhausted { request: l })
}

unsafe fn dealloc(&mut self, p: *mut u8, _l: Layout) {
global::free(p);
}

unsafe fn realloc(&mut self, p: *mut u8, _l1: Layout, l2: Layout) -> Result<*mut u8, AllocErr> {
Ok(global::aligned_realloc(p, l2.size(), l2.align()))
global::aligned_realloc(p, l2.size(), l2.align()).ok_or(AllocErr::Exhausted { request: l2 })
}
}

#[cfg(feature = "c-api")]
unsafe impl Malloc for ElfMallocGlobal {
unsafe fn c_malloc(&self, size: size_t) -> *mut c_void {
let p = global::alloc(size as usize) as *mut c_void;
let p = global::alloc(size as usize).unwrap_or(ptr::null_mut()) as *mut c_void;
alloc_debug_assert_eq!((p as usize) % MIN_ALIGN,
0,
"object does not have the required alignment of {}: {:?}",
Expand All @@ -82,7 +84,7 @@ unsafe impl Malloc for ElfMallocGlobal {
"object does not have the required alignment of {}: {:?}",
MIN_ALIGN,
p);
global::realloc(p as *mut u8, new_size as usize) as *mut c_void
global::realloc(p as *mut u8, new_size as usize).unwrap_or(ptr::null_mut()) as *mut c_void
}
}

Expand Down
7 changes: 4 additions & 3 deletions elfmalloc/src/bin/bench.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 the authors. See the 'Copyright and license' section of the
// Copyright 2017-2018 the authors. See the 'Copyright and license' section of the
// README.md file at the top-level directory of this repository.
//
// Licensed under the Apache License, Version 2.0 (the LICENSE-APACHE file) or
Expand Down Expand Up @@ -101,11 +101,12 @@ unsafe impl<T> Send for ElfClone<T> {}
impl<T: 'static> AllocLike for ElfClone<T> {
type Item = T;
fn create() -> Self {
ElfClone(DynamicAllocator::new(), marker::PhantomData)
ElfClone(DynamicAllocator::new().unwrap(), marker::PhantomData)
}

unsafe fn allocate(&mut self) -> *mut T {
self.0.alloc(mem::size_of::<T>()) as *mut T
// TODO: Do something other than unwrap?
self.0.alloc(mem::size_of::<T>()).unwrap() as *mut T
}

unsafe fn deallocate(&mut self, item: *mut T) {
Expand Down
Loading