Skip to content
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
4 changes: 2 additions & 2 deletions alloc-fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extern crate libc;
extern crate spin;

use core::fmt::{Arguments, Result as FmtResult, Write};
use core::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT};
use core::sync::atomic::AtomicBool;
use core::sync::atomic::Ordering::SeqCst;

// Import items that macros need to reference. They will reference them as $crate::foo instead of
Expand Down Expand Up @@ -152,7 +152,7 @@ macro_rules! alloc_eprintln {
// (essentially short-circuiting what would eventually happen if print_backtrace_and_abort
// successfully finished executing).
#[doc(hidden)]
pub static IS_PANICKING: AtomicBool = ATOMIC_BOOL_INIT;
pub static IS_PANICKING: AtomicBool = AtomicBool::new(false);

#[macro_export]
macro_rules! alloc_panic {
Expand Down
1 change: 0 additions & 1 deletion bsalloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#![no_std]
#![feature(allocator_api)]
#![feature(alloc)]

extern crate alloc;
#[macro_use]
Expand Down
36 changes: 28 additions & 8 deletions elfmalloc/src/alloc_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ extern crate alloc;
extern crate malloc_bind;
#[cfg(feature = "c-api")]
extern crate libc;
use self::alloc::alloc::{Alloc, AllocErr, Layout};
use self::alloc::alloc::{Alloc, AllocErr, Layout, GlobalAlloc};
#[cfg(feature = "c-api")]
use self::malloc_bind::{LayoutFinder, Malloc, MIN_ALIGN};
use super::general::global;
use std::ptr;
use std::ptr::NonNull;
use std::mem;
#[cfg(feature = "c-api")]
use std::intrinsics::unlikely;
Expand All @@ -34,23 +36,41 @@ use self::libc::{size_t, c_void};
pub struct ElfMallocGlobal;

unsafe impl<'a> Alloc for &'a ElfMallocGlobal {
unsafe fn alloc(&mut self, l: Layout) -> Result<*mut u8, AllocErr> {
unsafe fn alloc(&mut self, l: Layout) -> Result<NonNull<u8>, AllocErr> {
// All objects are only guaranteed to be word-aligned except for powers of two. Powers of
// 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()))
Ok(NonNull::new_unchecked(global::alloc(l.size())))
} else {
Ok(global::alloc(l.size().next_power_of_two()))
Ok(NonNull::new_unchecked(global::alloc(l.size().next_power_of_two())))
}
}

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

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()))
unsafe fn realloc(&mut self, p: NonNull<u8>, l1: Layout, size: usize) -> Result<NonNull<u8>, AllocErr> {
Ok(NonNull::new_unchecked(global::aligned_realloc(p.as_ptr(), size, l1.align())))
}
}

unsafe impl GlobalAlloc for ElfMallocGlobal {
unsafe fn alloc(&self, l: Layout) -> *mut u8 {
Alloc::alloc(&mut &*self, l)
.map(|p| p.as_ptr())
.unwrap_or(ptr::null_mut())
}

unsafe fn dealloc(&self, p: *mut u8, l: Layout) {
Alloc::dealloc(&mut &*self, NonNull::new_unchecked(p), l)
}

unsafe fn realloc(&self, p: *mut u8, l1: Layout, size: usize) -> *mut u8 {
Alloc::realloc(&mut &*self, NonNull::new_unchecked(p), l1, size)
.map(|p| p.as_ptr())
.unwrap_or(ptr::null_mut())
}
}

Expand Down
24 changes: 10 additions & 14 deletions elfmalloc/src/bin/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
// the MIT license (the LICENSE-MIT file) at your option. This file may not be
// copied, modified, or distributed except according to those terms.

#![feature(alloc)]
#![feature(allocator_api)]
extern crate alloc;
extern crate elfmalloc;
extern crate num_cpus;
use std::marker;
use alloc::heap;
use std::alloc::{Alloc, Layout};
use std::mem;
use std::thread;
use std::time;
use std::ptr::write_volatile;
use std::ptr::{write_volatile, NonNull};

// use elfmalloc::slag::{AllocBuilder, LocalAllocator, MagazineAllocator};
// use elfmalloc::general::global;
Expand Down Expand Up @@ -69,21 +68,19 @@ impl<T> Clone for ElfGlobal<T> {
}
unsafe impl<T> Send for ElfGlobal<T> {}

use alloc::allocator::{Alloc, Layout};

impl<T: 'static> AllocLike for ElfGlobal<T> {
type Item = T;
fn create() -> Self {
ElfGlobal(marker::PhantomData)
}

unsafe fn allocate(&mut self) -> *mut T {
(&ElfMallocGlobal{}).alloc(Layout::new::<T>()).unwrap() as *mut T
(&ElfMallocGlobal{}).alloc(Layout::new::<T>()).unwrap().as_ptr() as *mut T
// global::alloc(mem::size_of::<T>()) as *mut T
}

unsafe fn deallocate(&mut self, item: *mut T) {
(&ElfMallocGlobal{}).dealloc(item as *mut u8, Layout::new::<T>())
(&ElfMallocGlobal{}).dealloc(NonNull::new_unchecked(item as *mut u8), Layout::new::<T>())
// global::free(item as *mut u8)
}

Expand Down Expand Up @@ -151,16 +148,15 @@ impl<T> AllocLike for DefaultMalloc<T> {
DefaultMalloc(marker::PhantomData)
}
unsafe fn allocate(&mut self) -> *mut T {
use heap::{Alloc, Layout};
heap::Heap
.alloc(Layout::from_size_align(mem::size_of::<T>(), 8).unwrap())
.unwrap() as *mut T
use alloc::alloc::Global;
Global.alloc(Layout::from_size_align(mem::size_of::<T>(), 8).unwrap())
.unwrap().as_ptr() as *mut T
}

unsafe fn deallocate(&mut self, item: *mut T) {
use heap::{Alloc, Layout};
heap::Heap.dealloc(
item as *mut u8,
use alloc::alloc::Global;
Global.dealloc(
NonNull::new_unchecked(item as *mut u8),
Layout::from_size_align(mem::size_of::<T>(), 8).unwrap(),
);
}
Expand Down
6 changes: 2 additions & 4 deletions elfmalloc/src/bin/bench_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// the MIT license (the LICENSE-MIT file) at your option. This file may not be
// copied, modified, or distributed except according to those terms.

#![feature(alloc)]
#![feature(log_syntax)]
#![feature(allocator_api)]
#![feature(test)]
Expand All @@ -17,7 +16,6 @@ extern crate test;
use test::stats::Stats;
use elfmalloc::rust_alloc::SharedAlloc;
use elfmalloc::vec_alloc::AVec;
use alloc::heap::Heap;
use smallvec::VecLike;

use std::thread;
Expand Down Expand Up @@ -163,14 +161,14 @@ macro_rules! bench_group {
_t,
1,
$iters,
$fn::<AVec<_, Heap>>($param, _t));
$fn::<AVec<_, ::alloc::alloc::Global>>($param, _t));
create_bench!(v2n,
format!("{}_avec_heap", stringify!($name)),
$param ::: $pty = $pval,
_t,
num_cpus::get(),
$iters,
$fn::<AVec<_, Heap>>($param, _t));
$fn::<AVec<_, ::alloc::alloc::Global>>($param, _t));
create_bench!(v3,
format!("{}_avec_elf", stringify!($name)),
$param ::: $pty = $pval,
Expand Down
5 changes: 3 additions & 2 deletions elfmalloc/src/frontends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ mod magazine {
//! because we implement the `Depot` using a `BagPipe`; as a result, the `Depot` should not be
//! a source of contention except in the case where magazines are quite small.

use std::ptr::NonNull;
use super::*;
use super::super::bagpipe::bag::WeakBag;
use super::super::bagpipe::{BagPipe, BagCleanup};
Expand Down Expand Up @@ -444,7 +445,7 @@ mod magazine {
let n_pages = (bytes >> page_size.trailing_zeros()) + cmp::min(1, rem);
let region_size = n_pages * page_size;
alloc_debug_assert!(bytes <= region_size);
let mem = mmap::map(region_size) as *mut Magazine;
let mem = mmap::map(region_size).as_ptr() as *mut Magazine;
ptr::write(
mem,
Magazine {
Expand All @@ -469,7 +470,7 @@ mod magazine {
unsafe fn destroy(slf: *mut Magazine) {
alloc_debug_assert_eq!(((*slf).base as *mut Magazine).offset(-1), slf);
alloc_debug_assert_eq!(slf as usize % mmap::page_size(), 0);
mmap::unmap(slf as *mut u8, (*slf).mapped)
mmap::unmap(NonNull::new_unchecked(slf as *mut u8), (*slf).mapped)
}

fn push(&mut self, item: *mut u8) -> bool {
Expand Down
18 changes: 9 additions & 9 deletions elfmalloc/src/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ impl<M: MemorySource, D: DirtyFn, AM: AllocMap<ObjectAlloc<PageAlloc<M, D>>, Key
n_classes: usize,
) -> Self {
use self::mmap::map;
let mut meta_pointer = map(mem::size_of::<Metadata>() * n_classes) as *mut Metadata;
let mut meta_pointer = map(mem::size_of::<Metadata>() * n_classes).as_ptr() as *mut Metadata;
let small_page_size = pa_small.backing_memory().page_size();
let am = AM::init(start_from, n_classes, |size: usize| {
let (u_size, pa, ty) = if size < ELFMALLOC_SMALL_CUTOFF {
Expand Down Expand Up @@ -920,6 +920,7 @@ mod large_alloc {
use std::cell::RefCell;
use std::cmp;
use std::ptr;
use std::ptr::NonNull;
use super::super::sources::{MemorySource, MmapSource};
use super::{ELFMALLOC_PAGE_SIZE, ELFMALLOC_SMALL_CUTOFF, round_to_page};
use super::super::alloc_type::AllocType;
Expand Down Expand Up @@ -950,25 +951,24 @@ mod large_alloc {
let src = MmapSource::new(ELFMALLOC_SMALL_CUTOFF);
let n_pages = region_size / ELFMALLOC_SMALL_CUTOFF + cmp::min(1, region_size % ELFMALLOC_SMALL_CUTOFF);
let mem = src.carve(n_pages).expect("[lage_alloc::alloc] mmap failed");
let res = mem.offset(ELFMALLOC_PAGE_SIZE as isize);
let res = mem.as_ptr().offset(ELFMALLOC_PAGE_SIZE as isize);
let addr = get_commitment_mut(res);
ptr::write(
addr,
AllocInfo {
ty: AllocType::Large,
base: mem,
base: mem.as_ptr(),
region_size: region_size,
},
);

// begin extra debugging information
alloc_debug_assert!(!mem.is_null());
alloc_debug_assert_eq!(mem as usize % ELFMALLOC_SMALL_CUTOFF, 0);
alloc_debug_assert_eq!(mem.as_ptr() as usize % ELFMALLOC_SMALL_CUTOFF, 0);
let upage: usize = 4096;
alloc_debug_assert_eq!(mem as usize % upage, 0);
alloc_debug_assert_eq!(mem.as_ptr() as usize % upage, 0);
alloc_debug_assert_eq!(res as usize % upage, 0);
alloc_debug_assert_eq!(get_commitment(res), (size + ELFMALLOC_PAGE_SIZE, mem));
#[cfg(test)] SEEN_PTRS.try_with(|hs| hs.borrow_mut().insert(mem, region_size));
alloc_debug_assert_eq!(get_commitment(res), (size + ELFMALLOC_PAGE_SIZE, mem.as_ptr()));
#[cfg(test)] SEEN_PTRS.try_with(|hs| hs.borrow_mut().insert(mem.as_ptr(), region_size));
// end extra debugging information
res
}
Expand Down Expand Up @@ -1006,7 +1006,7 @@ mod large_alloc {
});
}
// end extra debugging information
unmap(base_ptr, size);
unmap(NonNull::new_unchecked(base_ptr), size);
}

pub unsafe fn get_size(item: *mut u8) -> usize {
Expand Down
5 changes: 1 addition & 4 deletions elfmalloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
// the MIT license (the LICENSE-MIT file) at your option. This file may not be
// copied, modified, or distributed except according to those terms.

#![feature(alloc)]
#![feature(allocator_api)]
#![cfg_attr(test, feature(test))]
#![feature(thread_local_state)]
#![feature(thread_local)]
#![feature(const_fn)]
#![feature(const_size_of)]
#![feature(cfg_target_thread_local)]
#![feature(core_intrinsics)]
#![feature(const_ptr_null_mut)]
#![feature(raw_vec_internals)]
extern crate alloc;
extern crate bagpipe;
extern crate mmap_alloc;
Expand Down
Loading