Skip to content

Commit

Permalink
elfmalloc: Use mmap-alloc's large-align feature for aligns over one page
Browse files Browse the repository at this point in the history
- Use mmap-alloc's large-align feature, which enables passing
  alignments which are larger than the page size
- Propagate allocation failures (rather than asserting/unwrapping/etc)
  in more places
  • Loading branch information
joshlf committed Feb 25, 2018
1 parent f42455c commit e2a2612
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 403 deletions.
6 changes: 3 additions & 3 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,11 +49,11 @@ 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" }
mmap-alloc = { path = "../mmap-alloc" }
mmap-alloc = { path = "../mmap-alloc", features = ["large-align"] }
num_cpus = "1.5"
smallvec = "0.4.3"
sysconf = "0.3.1"
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

0 comments on commit e2a2612

Please sign in to comment.