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
#![warn(missing_docs)]
#![cfg_attr(feature = "strict", deny(warnings))]
#![no_std]
#![crate_type = "rlib"]
#![feature(global_allocator)]
#![feature(alloc)]
#![feature(allocator_api)]

//! Custom allocator crate for wasm

extern crate alloc;
extern crate pwasm_libc;

use alloc::heap::{Alloc, Layout, AllocErr};

/// Wasm allocator
pub struct WasmAllocator;

unsafe impl<'a> Alloc for &'a WasmAllocator {
    unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
        Ok(pwasm_libc::malloc(layout.size()))
    }

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

#[global_allocator]
static ALLOCATOR: WasmAllocator = WasmAllocator;