It's my first day using Zig. And I found that I need to choose allocators everywhere, so I made this little note for myself:
https://ziglang.org/documentation/master/std/#std.mem.Allocator
Allocator List
ArenaAllocator:
Wraps an existing allocator, alloc()
will allocate without freeing, then free all together at the end. (Example Usage: parsers, tcp streams...)
FixedBufferAllocator:
Does aligned alloc with a specific length, store on a stackful byte array and alloc will move end_index
(Usage: fast, any stackful)
GeneralPurposeAllocator:
Mutexed and bucketed heap allocation, will double its size by resizing when alloc is oversized. (Usage: When lazy)
StackFallbackAllocator:
=FixedBufferAllocator, but can fallback to another(usually heapful) allocator if failed (super cool imo.) (Usage: When lazy but not that lazy)
PageAllocator:
Uses posix mmap
or windows VirtualAlloc
to write to a memory page managed by the OS (Usage: Make sure the OS will get the memory back immediately)
WasmAllocator/WasmPageAllocator:
TLDR: wasm only, growing at different manners, in 8bit vs 128bit data blocks
CAllocator:
=libc malloc API wrapper (Usage: wasm :))
SbrkAllocator:
Legacy Sbrk: naive version(mutex lock) (Usage: when you are using SYS_brk)
Accessories
LogToWriterAllocator: Takes another allocator and a writer for loggin
LoggingAllocator: same, but the writer writes to stdout
MemoryPool: Can wrap any allocator with ArenaAllocator
, forcing the size to be x*len(object)
, so that alignedAlloc()
will work efficiently. (Example usage, parsers, char operations)