An arena allocator is a type of memory allocator that operates by allocating chunks of memory from a contiguous block of memory (the "arena"), rather than from the operating system's memory pool. This can lead to improved performance for certain use cases. Here's one way to implement an arena allocator in C#:
main.cs514 chars23 lines
In this implementation, we use a byte[]
to represent the arena, and keep track of the current offset within the array. The Allocate
method takes a size parameter, and returns a pointer to a newly-allocated region of memory within the arena. If there is not enough space left in the arena to satisfy the request, an OutOfMemoryException
is thrown.
The Reset
method simply resets the offset to zero, effectively freeing all memory allocated by the allocator. Note that this implementation does not support deallocating individual regions of memory within the arena.
gistlibby LogSnag