From 300024b82bff03e43d8ad53a89f0159b763426cc Mon Sep 17 00:00:00 2001
From: Bruno Kirschner <bruno.kirschner@online.de>
Date: Sat, 31 Aug 2019 01:06:35 +0200
Subject: [PATCH] [API] Provide a card10-alloc based global allocator as
 default feature.

---
 Cargo.lock                |  7 ++++++-
 Cargo.toml                |  1 +
 card10-l0dable/Cargo.toml | 15 ++++++++++++++-
 card10-l0dable/src/lib.rs | 29 +++++++++++++++++++++++++++++
 4 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 362737c..68a4841 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 121d839..356f669 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 96880ce..ea2c7f5 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 bf0bf40..5d081ef 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!");
+    }
+}
+
-- 
GitLab