1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#![feature(thread_local)]

extern crate libc;
mod bump_pointer_local;
mod malloc_api;

use std::alloc::{Layout, GlobalAlloc};
use bump_pointer_local::LOCAL_ALLOCATOR;



pub struct BumpPointer;

unsafe impl GlobalAlloc for BumpPointer {
    #[inline(always)]
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        let (bytes, align) = (layout.size(), layout.align());
        let ptr = LOCAL_ALLOCATOR.alloc(bytes, align);
        mem_zero(ptr, bytes);
        ptr
    }

    #[inline(always)]
    unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
}


#[inline(always)] 
unsafe fn mem_zero(ptr: *mut u8, bytes: usize) {
    let mut cursor = ptr;
    let limit = ptr.add(bytes);
    while cursor < limit {
        cursor.write(0);
        cursor = cursor.add(1);
    }
}