Use upstream lazy static as it has a spin_no_std feature now (#158)

This commit is contained in:
Philipp Oppermann
2016-05-14 16:25:15 +02:00
parent 7170c658a7
commit a39c59801b
3 changed files with 14 additions and 9 deletions

View File

@@ -739,23 +739,25 @@ static HEAP: Mutex<Heap> = 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. 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 [dependencies.lazy_static]
> cargo add once version = "0.2.1"
features = ["spin_no_std"]
``` ```
```rust ```rust
// in libs/hole_list_allocator/src/lib.rs // in libs/hole_list_allocator/src/lib.rs
#[macro_use] #[macro_use]
extern crate once; extern crate lazy_static;
lazy_static! { lazy_static! {
static ref HEAP: Mutex<Heap> = Mutex::new(unsafe { static ref HEAP: Mutex<Heap> = Mutex::new(unsafe {

View File

@@ -6,4 +6,7 @@ version = "0.1.0"
[dependencies] [dependencies]
linked_list_allocator = "0.2.0" linked_list_allocator = "0.2.0"
spin = "0.3.5" spin = "0.3.5"
once = "0.2.0"
[dependencies.lazy_static]
version = "0.2.1"
features = ["spin_no_std"]

View File

@@ -10,7 +10,7 @@ use linked_list_allocator::Heap;
extern crate spin; extern crate spin;
extern crate linked_list_allocator; extern crate linked_list_allocator;
#[macro_use] #[macro_use]
extern crate once; extern crate lazy_static;
pub const HEAP_START: usize = 0o_000_001_000_000_0000; pub const HEAP_START: usize = 0o_000_001_000_000_0000;
pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB