Skip to content

Commit

Permalink
bump allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
laizy committed Mar 26, 2019
1 parent 5e6295e commit 75d769e
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"ontio-std",
"ontio-codegen",
"ontio-bump-alloc",
"examples",
"examples/token",
"examples/token-zero-copy",
Expand Down
5 changes: 5 additions & 0 deletions examples/oep8token/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ version = "0.1.0"
authors = ["lucas7788 <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib"]
path = "src/lib.rs"

[dependencies]
ontio-std = {path="../../ontio-std"}

[features]
default = ["ontio-std/bump-alloc"]
mock = ["ontio-std/mock"]
2 changes: 1 addition & 1 deletion examples/token/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ crate-type = ["cdylib"]
path = "src/lib.rs"

[dependencies]
ontio-std = {path="../../ontio-std"}
ontio-std = {path="../../ontio-std", features=["bump-alloc"]}

4 changes: 2 additions & 2 deletions examples/token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ fn balance_of(owner: &Addr) -> U256 {
fn transfer(from: &Addr, to: &Addr, amount: U256) -> bool {
assert!(runtime::check_witness(from));

let mut frmbal = balance_of(from);
let mut tobal = balance_of(to);
let frmbal = balance_of(from);
let tobal = balance_of(to);
if amount == 0.into() || frmbal < amount {
return false;
}
Expand Down
8 changes: 8 additions & 0 deletions ontio-bump-alloc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "ontio-bump-alloc"
version = "0.1.0"
authors = ["laizy <[email protected]>"]
edition = "2018"

[dependencies]
cfg-if = { version = "0.1", default-features = false }
84 changes: 84 additions & 0 deletions ontio-bump-alloc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#![no_std]
use cfg_if::cfg_if;
use core::alloc::{GlobalAlloc, Layout};
use core::cell::RefCell;
use core::ptr;

const PAGE_SIZE: usize = 65536;

struct Inner {
pos: *mut u8,
size: usize,
}

pub struct BumpAlloc {
inner: RefCell<Inner>,
}

//contract execution is single thread
unsafe impl Sync for BumpAlloc {}

impl BumpAlloc {
pub const fn new() -> BumpAlloc {
BumpAlloc { inner: RefCell::new(Inner { pos: ptr::null_mut(), size: 0 }) }
}
}

fn align_to(size: usize, align: usize) -> usize {
(size + align - 1) & !(align - 1)
}

unsafe impl GlobalAlloc for BumpAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let mut inner = self.inner.borrow_mut();

let need_bytes = align_to(layout.size(), layout.align());
let pos = align_to(inner.pos as usize, layout.align());
if pos + need_bytes > inner.size {
let need_page = (pos + need_bytes - inner.size + PAGE_SIZE - 1) / PAGE_SIZE;
match alloc_pages(need_page) {
Some(p) => {
if inner.pos == ptr::null_mut() {
inner.pos = p;
}
inner.size = (p as usize) + need_page * PAGE_SIZE;
}
None => return ptr::null_mut(),
}
}

let pos = align_to(inner.pos as usize, layout.align());
inner.pos = (pos + need_bytes) as *mut u8;

pos as *mut u8
}

unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
}

cfg_if! {
if #[cfg(target_arch = "wasm32")] {
mod alloc_impl {
use core::arch::wasm32;
use super::PAGE_SIZE;

pub(crate) unsafe fn alloc_pages(num_page: usize) -> Option<*mut u8> {
let ptr = wasm32::memory_grow(0, num_page);
if ptr != usize::max_value() {
let ptr = (ptr * PAGE_SIZE) as *mut u8;
Some(ptr)
} else {
None
}
}
}
use alloc_impl::alloc_pages;
} else {
mod imp_noop {
pub(crate) unsafe fn alloc_pages(_num_page: usize) -> Option<*mut u8> {
None
}
}
use imp_noop::alloc_pages;
}
}
2 changes: 2 additions & 0 deletions ontio-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2018"

[dependencies]
wee_alloc = "0.4"
ontio-bump-alloc = {path = "../ontio-bump-alloc", optional = true}
bigint = { version = "4.4", default-features = false }
fixed-hash = { version = "0.3", default-features = false }
cfg-if = { version = "0.1", default-features = false }
Expand All @@ -18,4 +19,5 @@ rand = "0.6"

[features]
std = []
bump-alloc = ["ontio-bump-alloc"]
mock = ["std", "fixed-hash/default"]
11 changes: 10 additions & 1 deletion ontio-std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@
//#![feature(trace_macros)]

cfg_if::cfg_if! {
if #[cfg(not(feature = "std"))] {
if #[cfg(all(not(feature = "std"), feature = "bump-alloc"))] {
use ontio_bump_alloc::BumpAlloc;
#[global_allocator]
static ALLOC: BumpAlloc = BumpAlloc::new();
} else if #[cfg(not(feature = "std"))] {
extern crate wee_alloc;
// Use `wee_alloc` as the global allocator.
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
}
}

cfg_if::cfg_if! {
if #[cfg(not(feature = "std"))] {
/// Overrides the default panic_fmt
#[no_mangle]
#[panic_handler]
Expand Down

0 comments on commit 75d769e

Please sign in to comment.