|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +cat > Cargo.toml <<'EOF' |
| 4 | +[package] |
| 5 | +name = "abc" |
| 6 | +version = "0.0.1" |
| 7 | +edition = "2018" |
| 8 | +
|
| 9 | +[profile.dev] |
| 10 | +panic = "abort" |
| 11 | +
|
| 12 | +[profile.release] |
| 13 | +panic = "abort" |
| 14 | +EOF |
| 15 | + |
| 16 | +mkdir -p src |
| 17 | + |
| 18 | +cat > src/lib.rs <<'EOF' |
| 19 | +// ice_test |
| 20 | +#![no_std] |
| 21 | +
|
| 22 | +#![feature(const_fn)] |
| 23 | +
|
| 24 | +use core::alloc::{GlobalAlloc, Layout}; |
| 25 | +
|
| 26 | +pub static DEFAULT_ALLOCATOR: Allocator = Allocator::new(&DEFAULT_HEAP); |
| 27 | +static DEFAULT_HEAP: GeneralAllocator = GeneralAllocator::new(); |
| 28 | +
|
| 29 | +#[derive(Copy, Clone)] |
| 30 | +pub struct Allocator { |
| 31 | + allocator: &'static (dyn GlobalAlloc + 'static), |
| 32 | +} |
| 33 | +
|
| 34 | +unsafe impl Sync for Allocator {} |
| 35 | +
|
| 36 | +impl Allocator { |
| 37 | + const fn new(allocator: &'static dyn GlobalAlloc) -> Self { |
| 38 | + Self { allocator } |
| 39 | + } |
| 40 | +} |
| 41 | +
|
| 42 | +unsafe impl GlobalAlloc for Allocator { |
| 43 | + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { |
| 44 | + self.allocator.alloc(layout) |
| 45 | + } |
| 46 | + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { |
| 47 | + self.allocator.dealloc(ptr, layout) |
| 48 | + } |
| 49 | +} |
| 50 | +
|
| 51 | +pub struct GeneralAllocator; |
| 52 | +
|
| 53 | +unsafe impl Sync for GeneralAllocator {} |
| 54 | +
|
| 55 | +impl GeneralAllocator { |
| 56 | + pub const fn new() -> Self { |
| 57 | + Self {} |
| 58 | + } |
| 59 | +} |
| 60 | +
|
| 61 | +unsafe impl GlobalAlloc for GeneralAllocator { |
| 62 | + unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { |
| 63 | + todo!() |
| 64 | + } |
| 65 | +
|
| 66 | + unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) { |
| 67 | + todo!() |
| 68 | + } |
| 69 | +} |
| 70 | +EOF |
| 71 | + |
| 72 | +cat > src/test.rs <<'EOF' |
| 73 | +#![feature(alloc_error_handler)] |
| 74 | +#![no_std] |
| 75 | +#![no_main] |
| 76 | +
|
| 77 | +use ice_test::{Allocator, DEFAULT_ALLOCATOR}; |
| 78 | +
|
| 79 | +
|
| 80 | +extern crate alloc; |
| 81 | +
|
| 82 | +#[global_allocator] |
| 83 | +pub static GLOBAL_ALLOCATOR: Allocator = DEFAULT_ALLOCATOR; |
| 84 | +
|
| 85 | +#[no_mangle] |
| 86 | +fn main() { |
| 87 | +
|
| 88 | +} |
| 89 | +
|
| 90 | +#[panic_handler] |
| 91 | +fn panic(_info: &core::panic::PanicInfo) -> ! { |
| 92 | + loop {} |
| 93 | +} |
| 94 | +
|
| 95 | +#[alloc_error_handler] |
| 96 | +fn alloc_error_handler(layout: core::alloc::Layout) -> ! { |
| 97 | + panic!( |
| 98 | + "Error allocating {} bytes of memory with alignment {}", |
| 99 | + layout.size(), |
| 100 | + layout.align() |
| 101 | + ); |
| 102 | +} |
| 103 | +EOF |
| 104 | + |
| 105 | +cargo test |
0 commit comments