Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It is now possible to define allocators in submodules #715

Merged
merged 1 commit into from
Jan 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions blog/content/second-edition/posts/10-heap-allocation/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,13 @@ We now have a simple allocator, but we still have to tell the Rust compiler that
The `#[global_allocator]` attribute tells the Rust compiler which allocator instance it should use as the global heap allocator. The attribute is only applicable to a `static` that implements the `GlobalAlloc` trait. Let's register an instance of our `Dummy` allocator as the global allocator:

```rust
// in src/lib.rs
// in src/allocator.rs

#[global_allocator]
static ALLOCATOR: allocator::Dummy = allocator::Dummy;
```

Since the `Dummy` allocator is a [zero sized type], we don't need to specify any fields in the initialization expression. Note that the `#[global_allocator]` module [cannot be used in submodules][pr51335], so we need to put it into the `lib.rs`.

[pr51335]: https://github.com/rust-lang/rust/pull/51335
Since the `Dummy` allocator is a [zero sized type], we don't need to specify any fields in the initialization expression.

When we now try to compile it, the first error should be gone. Let's fix the remaining second error:

Expand Down Expand Up @@ -520,7 +518,7 @@ linked_list_allocator = "0.6.4"
Then we can replace our dummy allocator with the allocator provided by the crate:

```rust
// in src/lib.rs
// in src/allocator.rs

use linked_list_allocator::LockedHeap;

Expand All @@ -547,7 +545,7 @@ pub fn init_heap(

// new
unsafe {
super::ALLOCATOR.lock().init(HEAP_START, HEAP_SIZE);
ALLOCATOR.lock().init(HEAP_START, HEAP_SIZE);
}

Ok(())
Expand Down