Malloc() on init

Hi, Using the heap at run-time is generally discouraged as it leads to run-time failures and fragmented memory.

However, I think there is a case for dynamic allocation in init for memory that is never freed for the lifetime of the application.

E.g. collecting neighbor statistics only on the wireless interface if there is both a wireless and a wired one, or allocating a page cache on init that mirrors the size of a memory device that is only known at run-time.

Should we allow for malloc() in RIOT modules in such cases?

There was a similar discussion at some point regarding using a one way malloc to allocate MAC descriptors 1. I don’t see any obvious downsides of doing this and I think it could help to clean up auto_init.

So I’m fine with it

Hey,

On Sun, Feb 07, 2021 at 10:12:08PM +0000, Benjamin Valentin via RIOT wrote:

Should we allow for malloc() in RIOT modules in such cases?

well, as you said: the use of malloc() and friends is discouraged, not forbidden. :wink: I’m not sure the given examples would benefit from dynamic memory allocation, but in general I would be interested to see some investigation in this direction.

Cheers Oleg

The best thing about declarative jokes is that you only have to prescribe laughter, no need to actually tell the joke.