From 0e9babaf1f413c9be18b1c27c307319e715116af Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Sat, 21 Sep 2024 08:56:25 +0000 Subject: [PATCH 1/2] Declare required features for examples --- Cargo.toml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index f84d36a..bcf197a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,3 +43,19 @@ cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } cortex-m-rt = "0.7" cortex-m-semihosting = "0.5" panic-semihosting = { version = "0.6", features = ["exit"] } + +[[example]] +name = "allocator_api" +required-features = ["allocator_api", "llff"] + +[[example]] +name = "llff_integration_test" +required-features = ["allocator_api", "llff"] + +[[example]] +name = "tlsf_integration_test" +required-features = ["allocator_api", "tlsf"] + +[[example]] +name = "global_alloc" +required-features = ["llff"] From 93b323b4723e6afc480ecb1e15e599735a059e3c Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Sat, 21 Sep 2024 09:10:10 +0000 Subject: [PATCH 2/2] Use addr_of_mut! instead of as_ptr() to avoid shared reference to mutable static --- README.md | 3 ++- examples/allocator_api.rs | 3 ++- examples/global_alloc.rs | 3 ++- examples/llff_integration_test.rs | 3 ++- examples/tlsf_integration_test.rs | 3 ++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bbe9eb9..5eb6cde 100644 --- a/README.md +++ b/README.md @@ -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; @@ -35,7 +36,7 @@ fn main() -> ! { use core::mem::MaybeUninit; const HEAP_SIZE: usize = 1024; static mut HEAP_MEM: [MaybeUninit; 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. diff --git a/examples/allocator_api.rs b/examples/allocator_api.rs index 39d54cf..ab08818 100644 --- a/examples/allocator_api.rs +++ b/examples/allocator_api.rs @@ -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; @@ -20,7 +21,7 @@ fn main() -> ! { const HEAP_SIZE: usize = 16; static mut HEAP_MEM: [MaybeUninit; 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); diff --git a/examples/global_alloc.rs b/examples/global_alloc.rs index d41debd..21c95f5 100644 --- a/examples/global_alloc.rs +++ b/examples/global_alloc.rs @@ -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; @@ -21,7 +22,7 @@ fn main() -> ! { use core::mem::MaybeUninit; const HEAP_SIZE: usize = 1024; static mut HEAP_MEM: [MaybeUninit; 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(); diff --git a/examples/llff_integration_test.rs b/examples/llff_integration_test.rs index af006be..c0823e5 100644 --- a/examples/llff_integration_test.rs +++ b/examples/llff_integration_test.rs @@ -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; @@ -66,7 +67,7 @@ fn main() -> ! { { const HEAP_SIZE: usize = 1024; static mut HEAP_MEM: [MaybeUninit; 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)] diff --git a/examples/tlsf_integration_test.rs b/examples/tlsf_integration_test.rs index 574497b..13e95b0 100644 --- a/examples/tlsf_integration_test.rs +++ b/examples/tlsf_integration_test.rs @@ -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; @@ -83,7 +84,7 @@ fn test_allocator_api() { fn main() -> ! { { static mut HEAP_MEM: [MaybeUninit; 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)]