From 23c559956a6e55c5523d7fe8e772d57c444e3cfd Mon Sep 17 00:00:00 2001 From: Jesse Braham Date: Wed, 13 Jul 2022 08:52:26 -0700 Subject: [PATCH] Add a simple allocation error handler gated behind a feature --- Cargo.toml | 3 +++ src/lib.rs | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index ee5e637..7e82fa0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,3 +24,6 @@ xtensa-lx = { version = "0.7.0", features = ["esp32s2"] } [target.xtensa-esp32s3-none-elf.dependencies] linked_list_allocator = "0.9.1" xtensa-lx = { version = "0.7.0", features = ["esp32s3"] } + +[features] +oom-handler = [] diff --git a/src/lib.rs b/src/lib.rs index 84d3a42..3dc1ec2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ //! `nightly` release channel. #![no_std] +#![cfg_attr(feature = "oom-handler", feature(alloc_error_handler))] use core::{ alloc::{GlobalAlloc, Layout}, @@ -25,6 +26,12 @@ use riscv::interrupt; #[cfg(target_arch = "xtensa")] use xtensa_lx::interrupt; +#[cfg(feature = "oom-handler")] +#[alloc_error_handler] +fn oom(_: core::alloc::Layout) -> ! { + panic!("Allocation failed, out of memory"); +} + pub struct EspHeap { heap: Mutex>, }