Skip to content

Commit

Permalink
Use addr_of_mut! instead of as_ptr() to avoid shared reference to mut…
Browse files Browse the repository at this point in the history
…able static
  • Loading branch information
jannic committed Sep 21, 2024
1 parent 0e9baba commit 93b323b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Starting with Rust 1.68, this crate can be used as a global allocator on stable

extern crate alloc;

use core::ptr::addr_of_mut;
use cortex_m_rt::entry;
use embedded_alloc::LlffHeap as Heap;

Expand All @@ -35,7 +36,7 @@ fn main() -> ! {
use core::mem::MaybeUninit;
const HEAP_SIZE: usize = 1024;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }
}

// now the allocator is ready types like Box, Vec can be used.
Expand Down
3 changes: 2 additions & 1 deletion examples/allocator_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern crate alloc;
use alloc::vec::Vec;
use core::mem::MaybeUninit;
use core::panic::PanicInfo;
use core::ptr::addr_of_mut;
use cortex_m_rt::entry;
use embedded_alloc::LlffHeap as Heap;

Expand All @@ -20,7 +21,7 @@ fn main() -> ! {
const HEAP_SIZE: usize = 16;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
let heap: Heap = Heap::empty();
unsafe { heap.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
unsafe { heap.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }

let mut xs = Vec::new_in(heap);
xs.push(1);
Expand Down
3 changes: 2 additions & 1 deletion examples/global_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate alloc;

use alloc::vec::Vec;
use core::panic::PanicInfo;
use core::ptr::addr_of_mut;
use cortex_m_rt::entry;
// Linked-List First Fit Heap allocator (feature = "llff")
use embedded_alloc::LlffHeap as Heap;
Expand All @@ -21,7 +22,7 @@ fn main() -> ! {
use core::mem::MaybeUninit;
const HEAP_SIZE: usize = 1024;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }
}

let mut xs = Vec::new();
Expand Down
3 changes: 2 additions & 1 deletion examples/llff_integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern crate panic_semihosting;

use alloc::vec::Vec;
use core::mem::{size_of, MaybeUninit};
use core::ptr::addr_of_mut;
use cortex_m_rt::entry;
use cortex_m_semihosting::{debug, hprintln};
use embedded_alloc::LlffHeap as Heap;
Expand Down Expand Up @@ -66,7 +67,7 @@ fn main() -> ! {
{
const HEAP_SIZE: usize = 1024;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }
}

#[allow(clippy::type_complexity)]
Expand Down
3 changes: 2 additions & 1 deletion examples/tlsf_integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern crate panic_semihosting;

use alloc::collections::LinkedList;
use core::mem::MaybeUninit;
use core::ptr::addr_of_mut;
use cortex_m_rt::entry;
use cortex_m_semihosting::{debug, hprintln};
use embedded_alloc::TlsfHeap as Heap;
Expand Down Expand Up @@ -83,7 +84,7 @@ fn test_allocator_api() {
fn main() -> ! {
{
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }
}

#[allow(clippy::type_complexity)]
Expand Down

0 comments on commit 93b323b

Please sign in to comment.