bump_allocator/
lib.rs

1#![feature(thread_local)]
2
3extern crate libc;
4mod bump_pointer_local;
5mod malloc_api;
6
7use std::alloc::{Layout, GlobalAlloc};
8use bump_pointer_local::LOCAL_ALLOCATOR;
9
10
11
12pub struct BumpPointer;
13
14unsafe impl GlobalAlloc for BumpPointer {
15    #[inline(always)]
16    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
17        let (bytes, align) = (layout.size(), layout.align());
18        let ptr = LOCAL_ALLOCATOR.alloc(bytes, align);
19        mem_zero(ptr, bytes);
20        ptr
21    }
22
23    #[inline(always)]
24    unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
25}
26
27
28#[inline(always)] 
29unsafe fn mem_zero(ptr: *mut u8, bytes: usize) {
30    let mut cursor = ptr;
31    let limit = ptr.add(bytes);
32    while cursor < limit {
33        cursor.write(0);
34        cursor = cursor.add(1);
35    }
36}