Skip to content

Instantly share code, notes, and snippets.

@jumpnbrownweasel
jumpnbrownweasel / alloc_arena.zig
Last active September 24, 2022 16:05
Alloc an arena in the arena itself to avoid common UB mistakes.
const std = @import("std");
const expectEqual = std.testing.expectEqual;
/// Called instead of ArenaAllocator.init to ensure that the arena allocator
/// does not become invalid when the arena struct is moved.
///
/// The ArenaAllocator is placed in the arena itself, and therefore the returned
/// pointer to the arena is stable. This means the allocator returned by
/// arena.allocator(), which contains a pointer to the arena struct, is also
/// stable.