flipperzero-rs / flipperzero

Rust on the Flipper Zero
MIT License
499 stars 32 forks source link

`rt`: Add framework for creating heap statics #64

Open str4d opened 1 year ago

str4d commented 1 year ago

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.

dcoles commented 1 year ago

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?

str4d commented 1 year ago

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

dcoles commented 1 year ago

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.

str4d commented 1 year ago

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.

dcoles commented 1 year ago

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.