From a39c59801bc7e226d6ed9e18821ff38a7e9f2ea0 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Sat, 14 May 2016 16:25:15 +0200 Subject: [PATCH] Use upstream lazy static as it has a spin_no_std feature now (#158) --- blog/post/2016-04-11-kernel-heap.md | 16 +++++++++------- libs/hole_list_allocator/Cargo.toml | 5 ++++- libs/hole_list_allocator/src/lib.rs | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/blog/post/2016-04-11-kernel-heap.md b/blog/post/2016-04-11-kernel-heap.md index 57f52713..a9bed349 100644 --- a/blog/post/2016-04-11-kernel-heap.md +++ b/blog/post/2016-04-11-kernel-heap.md @@ -739,23 +739,25 @@ static HEAP: Mutex = Mutex::new(Heap::new(HEAP_START, HEAP_SIZE)); ``` The reason is that the `Heap::new` function needs to initialize the first hole (like described [above](#initialization)). This can't be done at compile time, so the function can't be a `const` function. Therefore we can't use it to initialize a static. -There is an easy solution for crates with access to the standard library: [lazy-static]. It automatically initializes the static when it's used the first time. However, it relies on the `std::sync::once` module and is thus unusable in our kernel. I couldn't find any alternatives for `#![no_std]` crates, so I've tried to port it. The results are part of the [once] crate, which we already used above. I've created it quite recently and haven't tested it much, so be careful when using it. Please [report][once issues] any issues you find! +There is an easy solution for crates with access to the standard library: [lazy_static]. It automatically initializes the static when it's used the first time. By default, it relies on the `std::sync::once` module and is thus unusable in our kernel. Fortunately it has a `spin_no_std` feature for `no_std` projects. -[once issues]: https://github.com/phil-opp/rust-once/issues +[lazy_static]: https://github.com/rust-lang-nursery/lazy-static.rs -Let's use the ported `lazy_static!` macro to fix our `hole_list_allocator`: +So let's use the `lazy_static!` macro to fix our `hole_list_allocator`: -[lazy-static]: https://github.com/rust-lang-nursery/lazy-static.rs +```toml +# in libs/hole_list_allocator/Cargo.toml -```shell -> cargo add once +[dependencies.lazy_static] +version = "0.2.1" +features = ["spin_no_std"] ``` ```rust // in libs/hole_list_allocator/src/lib.rs #[macro_use] -extern crate once; +extern crate lazy_static; lazy_static! { static ref HEAP: Mutex = Mutex::new(unsafe { diff --git a/libs/hole_list_allocator/Cargo.toml b/libs/hole_list_allocator/Cargo.toml index a055c0b9..eba97fb5 100644 --- a/libs/hole_list_allocator/Cargo.toml +++ b/libs/hole_list_allocator/Cargo.toml @@ -6,4 +6,7 @@ version = "0.1.0" [dependencies] linked_list_allocator = "0.2.0" spin = "0.3.5" -once = "0.2.0" + +[dependencies.lazy_static] +version = "0.2.1" +features = ["spin_no_std"] diff --git a/libs/hole_list_allocator/src/lib.rs b/libs/hole_list_allocator/src/lib.rs index c6829eb2..332e11d4 100644 --- a/libs/hole_list_allocator/src/lib.rs +++ b/libs/hole_list_allocator/src/lib.rs @@ -10,7 +10,7 @@ use linked_list_allocator::Heap; extern crate spin; extern crate linked_list_allocator; #[macro_use] -extern crate once; +extern crate lazy_static; pub const HEAP_START: usize = 0o_000_001_000_000_0000; pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB