3 ms·
How do you make things allocation free? and until how much degree you call something allocation free?
by wejick 4y ago
How do you make things allocation free?
and until how much degree you call something allocation free?
- eldenring 4y ago> you can't (or don't want to) allocate memory dynamically. this crate is intended to allow all metrics storage to be declared in statics, for use in embedded systems and other no-std use-cases. Basically, the library just transforms over a statically assigned buffer. In a lot of languages, this means you lose out on some language features, but I believe features like pattern matching are alloc free in rust.
- cormacrelf 4y agoMore accurately for a lot of languages you can’t make them allocation free. Rust put all of the parts of the standard library that allocate in one crate, called alloc, and that crate is optional (but included in std, so opt-out). Platforms that don’t have an allocator don’t have the alloc crate. So you are forced to select dependencies like this one that will compile without it. Language features like pattern matching are not affected.This is about standard library features. You don’t get Vec or String without the alloc crate.
- quake 4y agoTakes some knowledge of the std lib of whatever language you're using (easy with C and Rust, harder with C++) to know what calls will try to allocate memory. But another method is to use a static buffer of bytes designated in the link table as your "heap" and have tasks only allocate when they start, and ensure they do not free that memory. Algorithmically, allocation is the easy part, reusing freed blocks is more difficult. So if your embedded allocator doesn't free and faults at OOM, if you allocate only at the start of a program, you can still be more confident in memory safety
- mgaunard 4y agoThe problem with allocating is not the algorithm complexity, it's the fact it may need to allocate new pages from the operating system, which is a somewhat expensive operation. If you use a global allocator, when this happens is entirely non-deterministic. If you use a local iterator, at least you control when that happens, and have guarantees on the asymptotical behaviour.
- avinassh 4y ago^I had the same question, thank you for asking! To extend, OP can you link a few code lines in the project which shows this allocation free magic?