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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#![cfg(target_arch = "wasm32")]
#![no_std]
#![feature(core_intrinsics, lang_items, alloc_error_handler)]
extern crate alloc;
pub mod allocator;
mod buf_writer;
mod capability;
mod guards;
pub mod intrinsics;
mod logging;
mod model;
mod resources;
pub mod serial;
mod stats_allocator;
pub mod tensor_output;
use core::{alloc::Layout, fmt::Write, panic::PanicInfo};
use dlmalloc::GlobalDlmalloc;
use crate::allocator::Allocator;
pub use crate::{
buf_writer::BufWriter,
capability::Capability,
guards::{PipelineGuard, SetupGuard},
logging::Logger,
model::Model,
resources::{Resource, ResourceError},
serial::Serial,
tensor_output::TensorOutput,
};
#[global_allocator]
pub static ALLOCATOR: Allocator<GlobalDlmalloc> =
Allocator::new(GlobalDlmalloc);
#[panic_handler]
fn on_panic(info: &PanicInfo) -> ! {
static mut PANICKING: bool = false;
unsafe {
if !PANICKING {
PANICKING = true;
log::error!("{}", info);
}
static mut DEBUG_BUFFER: [u8; 1024] = [0; 1024];
let mut w = BufWriter::new(&mut DEBUG_BUFFER);
if write!(w, "{}", info).is_ok() {
let written = w.written();
intrinsics::_debug(written.as_ptr(), written.len() as u32);
}
core::arch::wasm32::unreachable()
}
}
#[alloc_error_handler]
fn on_alloc_error(layout: Layout) -> ! {
panic!(
"memory allocation of {} bytes failed ({:?})",
layout.size(),
ALLOCATOR.stats()
);
}