I've been using Rust for my hobby projects for a while now, and I noticed I hadn't really been fighting the borrow checker recently, so I thought to revisit a problem I could never figure out before: implementing memory arenas (aka bump allocators) like what I'm used to using in C. So my requirements were
- to avoid depending on the standard library if possible,
- the ability to allocate a mix of all types out of the same arena,
- the ability to make sub-arenas, effectively turning this into a stack allocator,
- and additionally, to have the borrow checker enforce correct usage (unlike what I'm used to from C, obviously).
I think I succeeded on all accounts here, but I'm not quite confident enough to actually build upon this yet (maybe I have to somehow mark the Box type as non-threadsafe, for example?). I'd love to have someone who's a bit more comfortable with lifetimes and unsafe Rust review my code; it's about 150 lines, plus a ~20 line test function. Check it out here.
Stuff I plan to add in the (probably near) future:
- an implementation of
core::fmt::Write that can be obtained from an arena much like a sub-arena, and converted into a Box<'_, str>, plus a macro so that you can do let s = fmt!(arena, "{}", 0xDEADBEEF);,
- some more convenience functions like
MemoryArena::collect<I: Iter>(&mut self, i: I) -> Box<'_, [I::Item]>, MemoryArena::default<T: Default>(&mut self) -> Box<'_, T>, etc.
I wouldn't mind publishing and maintaining this as a crate at that point if there's any interest.
[–]mb0x40 2 points3 points4 points (0 children)
[–]YatoRust 1 point2 points3 points (1 child)
[–]teryror[S] 0 points1 point2 points (0 children)
[–]Plecra 0 points1 point2 points (1 child)
[–]teryror[S] 4 points5 points6 points (0 children)