flipperzero-rs / flipperzero

Rust on the Flipper Zero

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`rt`: Add framework for creating heap statics

str4d opened this issue · comments

Normal Rust binaries will initialize heap statics on start, and then essentially leak the memory. This works fine because the memory is cleaned up during process exit. We can't assume the same with a FAP, so if we want to support "statics", we should provide a way in the runtime for initializing them before the user's app code runs, and freeing them after it exits.

In this case you're talking about statics that are stored on the heap (from lazy_static, once_cell), rather than static data stored in the .rodata right?

Yep. People could still use one of those crates, but I believe it would get reported as a memory leak by the FZ tooling.

It might be possible to provide a factory function for OnceCell that registers the static for cleanup. Calling the take method takes ownership of the inner value allowing them to be dropped.

We can't use once_cell because it requires std for Mutex. I'm instead planning on using generic_once_cell, which works for any mutex that implements lock_api::RawMutex. I've implemented the latter locally, and opened #74 with an Instant impl that I need.

We can't use once_cell because it requires std for Mutex.

I think it's possible to use with #[no-std] as long as you provide a critical-section implementation. I might take a look at that today, since a number of embedded crates are based around it.