diff --git a/Cargo.lock b/Cargo.lock index 362737c0c4718f6c3ca110cae94ca0c2e1c87392..68a4841bfb16379eb2b525647d0a52179a3dd81b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -77,10 +77,15 @@ name = "byteorder" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "card10-alloc" +version = "0.1.0" + [[package]] name = "card10-l0dable" version = "0.1.1" dependencies = [ + "card10-alloc 0.1.0", "card10-sys 0.1.0", ] @@ -181,8 +186,8 @@ dependencies = [ name = "l0dable-example" version = "0.0.0" dependencies = [ + "card10-alloc 0.1.0", "card10-l0dable 0.1.1", - "cc 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 121d83969b85dfca59a9a1a8bc616467b144fcff..356f6697b14011b5f5e9074bd1a3a76d35f6ccd9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = [ + "card10-alloc", "card10-sys", "card10-l0dable", "example", diff --git a/card10-l0dable/Cargo.toml b/card10-l0dable/Cargo.toml index 96880ced0aa0403563f51380d63f891871e40e49..ea2c7f5265cdc8cdb452bf2383d1937e05e18364 100644 --- a/card10-l0dable/Cargo.toml +++ b/card10-l0dable/Cargo.toml @@ -17,5 +17,18 @@ categories = ["no-std"] description = "make l0dables for the Card10 (CCCamp 2019) badge" [dependencies] -card10-sys = { path = "../card10-sys", version = "^0.1" } +[dependencies.card10-sys] +path = "../card10-sys" +version = "^0.1" + +[dependencies.card10-alloc] +path = "../card10-alloc" +version = "^0.1" +optional = true + +[features] +default = [ "alloc" ] + +# Provides an allocator to the dependent crate. +alloc = [ "card10-alloc" ] diff --git a/card10-l0dable/src/lib.rs b/card10-l0dable/src/lib.rs index bf0bf40148f54a7db98b9395d38fd313b3dbba79..5d081ef4071e5e7d63205ea65907336ff887b3e0 100644 --- a/card10-l0dable/src/lib.rs +++ b/card10-l0dable/src/lib.rs @@ -1,5 +1,7 @@ #![no_std] +#![feature(alloc_error_handler)] + mod os; pub use os::*; mod display; @@ -39,3 +41,30 @@ macro_rules! main { } }; } + +// ----------------------------------------------------------------------------- +// Allocation handling +// ----------------------------------------------------------------------------- + +/// Global Allocator Handling. +/// +/// Contains only the minimal necessary definitions: +/// +/// - static global allocator +/// - a proper allocation error handler +#[cfg(feature = "alloc")] +mod alloc { + + pub extern crate alloc; + use alloc::alloc::Layout; + use card10_alloc::Card10Allocator; + + #[global_allocator] + pub static ALLOCATOR: Card10Allocator = Card10Allocator; + + #[alloc_error_handler] + fn error_handler(_layout: Layout) -> ! { + panic!("OOM!"); + } +} +