Skip to content

Commit ed36a35

Browse files
committed
elfmalloc: Bubble up OOM errors, remove unnecessary internal code
- Make many internal methods return Option<T> rather than T and return None on OOM - Remove CoarseAllocator::backing_memory and replace it with page_size (which was all the former method was ever being used for)
1 parent 64a6ce5 commit ed36a35

File tree

11 files changed

+286
-299
lines changed

11 files changed

+286
-299
lines changed

elfmalloc/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- Copyright 2017 the authors. See the 'Copyright and license' section of the
1+
<!-- Copyright 2017-2018 the authors. See the 'Copyright and license' section of the
22
README.md file at the top-level directory of this repository.
33
44
Licensed under the Apache License, Version 2.0 (the LICENSE-APACHE file) or
@@ -25,6 +25,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
2525
`MagazineCache`
2626
- Added `c-api` feature to optimize for the C malloc API
2727

28+
### Changed
29+
- Changed error reporting so that more OOM errors are bubbled up and result
30+
in top-level errors rather than panics
31+
2832
### Fixed
2933
- Fixed a bug preventing non-nightly builds from compiling
3034
- Fixed an integer multiplication overflow bug

elfmalloc/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2017 the authors. See the 'Copyright and license' section of the
1+
# Copyright 2017-2018 the authors. See the 'Copyright and license' section of the
22
# README.md file at the top-level directory of this repository.
33
#
44
# Licensed under the Apache License, Version 2.0 (the LICENSE-APACHE file) or
@@ -49,7 +49,7 @@ alloc-fmt = { path = "../alloc-fmt" }
4949
alloc-tls = { path = "../alloc-tls" }
5050
bagpipe = { path = "../bagpipe" }
5151
bsalloc = "0.1.0"
52-
lazy_static = "1.0.0"
52+
lazy_static = { version = "1.0.0", features = ["spin_no_std"] }
5353
libc = "0.2"
5454
log = "0.3.8"
5555
malloc-bind = { path = "../malloc-bind" }

elfmalloc/src/alloc_impl.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017 the authors. See the 'Copyright and license' section of the
1+
// Copyright 2017-2018 the authors. See the 'Copyright and license' section of the
22
// README.md file at the top-level directory of this repository.
33
//
44
// Licensed under the Apache License, Version 2.0 (the LICENSE-APACHE file) or
@@ -25,6 +25,8 @@ use super::general::global;
2525
use std::mem;
2626
#[cfg(feature = "c-api")]
2727
use std::intrinsics::unlikely;
28+
#[cfg(feature = "c-api")]
29+
use std::ptr;
2830

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

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

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

5759
#[cfg(feature = "c-api")]
5860
unsafe impl Malloc for ElfMallocGlobal {
5961
unsafe fn c_malloc(&self, size: size_t) -> *mut c_void {
60-
let p = global::alloc(size as usize) as *mut c_void;
62+
let p = global::alloc(size as usize).unwrap_or(ptr::null_mut()) as *mut c_void;
6163
alloc_debug_assert_eq!((p as usize) % MIN_ALIGN,
6264
0,
6365
"object does not have the required alignment of {}: {:?}",
@@ -82,7 +84,7 @@ unsafe impl Malloc for ElfMallocGlobal {
8284
"object does not have the required alignment of {}: {:?}",
8385
MIN_ALIGN,
8486
p);
85-
global::realloc(p as *mut u8, new_size as usize) as *mut c_void
87+
global::realloc(p as *mut u8, new_size as usize).unwrap_or(ptr::null_mut()) as *mut c_void
8688
}
8789
}
8890

elfmalloc/src/bin/bench.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017 the authors. See the 'Copyright and license' section of the
1+
// Copyright 2017-2018 the authors. See the 'Copyright and license' section of the
22
// README.md file at the top-level directory of this repository.
33
//
44
// Licensed under the Apache License, Version 2.0 (the LICENSE-APACHE file) or
@@ -101,11 +101,12 @@ unsafe impl<T> Send for ElfClone<T> {}
101101
impl<T: 'static> AllocLike for ElfClone<T> {
102102
type Item = T;
103103
fn create() -> Self {
104-
ElfClone(DynamicAllocator::new(), marker::PhantomData)
104+
ElfClone(DynamicAllocator::new().unwrap(), marker::PhantomData)
105105
}
106106

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

111112
unsafe fn deallocate(&mut self, item: *mut T) {

0 commit comments

Comments
 (0)