Writing an OS in Rust: Allocating Frames
os.phil-opp.com
os.phil-opp.com
I've never been able to find a finished copy, but this one definitely gives you a good start, if only for 32 bit archs.
In any case, I'm pumped to try out this Rust one- looks very well done, and it's finally an excuse to learn some Rust!
Somewhat related topic I found: https://www.reddit.com/r/rust/comments/2mthq2/how_would_a_ru...
> Binaries generated by the compiler will use `alloc_jemalloc` by default (where available). [...] Dynamic and static libraries, however, will use `alloc_system` by default.
[1]: https://doc.rust-lang.org/nightly/book/custom-allocators.htm...
But whichever malloc implementation it uses, it still relies on the kernel. For example, jemalloc uses the operating system to map memory, by calling library functions like VirtualAlloc (win32) or mmap (POSIX).
https://github.com/jemalloc/jemalloc/blob/3a92319ddc5610b755...
Because Rust libraries being used in Rust projects will be statically linked currently, they will be built using jemalloc. Deploying a pure Rust project is as simple as pushing out a binary (std links to libc, so very few Rust projects are technically pure Rust, but its as good as because the system probably has libc).
Also, as a rule, you don't "free" in Rust at all, and code which defines how the memory is deallocated will be defined in the same library it was allocated in. I don't know the details of what it means when two libraries using two different allocators are interacting, but I don't think its as total a conflict as if you were to try to free a pointer with the wrong allocator's free function.
If you're building a dynamic or static library, we use the system allocator because Rust is "subordinate". If you're building a binary, we use jemalloc because Rust is in control and jemalloc is basically the best allocator in town. These behaviors can be manually overridden, and you can also provide your own custom global allocator (see linked RFC for details).
A Rust project using Rust libraries will generally link them in as rlibs, which is basically the Rust equivalent of an object file. rlibs just inherit whatever allocator from what they're linked into.
It is never a good idea to try to release memory in a module that didn't allocate it in first place, specially if one doesn't know how it was allocated.
[1]: https://doc.rust-lang.org/nightly/core/index.html
[2]: https://doc.rust-lang.org/nightly/alloc/index.html
[3]: https://doc.rust-lang.org/nightly/collections/index.html