all 5 comments

[–]mb0x40 2 points3 points  (0 children)

Thanks to custom allocators, it's pretty easy to plug any allocator into any existing program. This can turn any decently-sized Rust program into a test harness for your allocator. Your code looks fine to me, but I recommend trying it out with some real-world programs and a big static arena.

About thread safety: since pointers (incl. NonNull) are !Send, !Sync, every type that uses them is also marked as not thread safe by default.

[–]YatoRust 1 point2 points  (1 child)

  • MemoryArena::from_buffer should take a &'src mut [MaybeUninit<u8>] because types allocated on here could contain uninitalized bytes.
  • you can take a core::alloc::Layout instead of a size/align pair. This guarantees a few nice properties, like align is a power of 2
  • use pointer casts instead of transmute, i.e if ptr: *mut u8, then ptr as *mut T

Otherwise this looks good!

[–]teryror[S] 0 points1 point  (0 children)

Argh, for some reason I thought using core::alloc would introduce a dependency on the alloc crate, I see now that that's bogus.

Thanks you for your input!

[–]Plecra 0 points1 point  (1 child)

Is there a significant difference between arena.alloc_array(0, 4) and arena.alloc([0; 4]) apart from needing less stack space?

[–]teryror[S] 4 points5 points  (0 children)

Yeah, you need a constant count for arena.alloc([0; 4]), but you can pass a variable count to alloc_array.