Memory Arena

Hello, dear members of the Julia Community.
I want to manage heap space for external objects (e.g. C objects) by using an Arena
How is it done in Julia? The simple definition of an Arena is “An arena is just a large, contiguous piece of memory that you allocate once and then use to manage memory manually by handing out parts of that memory.”
Please do not suggest any external libraries.
To name one of its benefits. “To avoid fragmenting memory.”

Please do not suggest any external libraries.

This suggests that you want to learn how to implement a simple arena allocator in julia. The short answer is “the obvious way, nothing julia specific here”.

Since you are asking the question anyway, I assume this is not obvious to you. Props to you for learning something new, don’t get annoyed when I write something that is in fact obvious to you.

julia> mutable struct Arena
       ptr::Ptr{Cvoid}
       len::Int
       cap::Int
       end
julia> function makeArena(cap::Int)
       cap == 0 && return Arena(Base.C_NULL, 0, 0)
       Arena(ccall(:malloc, Ptr{Cvoid}, (Csize_t,), cap), 0, cap)
       end
julia> function freeArena(a::Arena)
       ccall(:free, Cvoid, (Ptr{Cvoid},), a.ptr)
       a.ptr = C_NULL
       a.len = 0
       a.cap = 0
       nothing
       end
julia> function allocFromArena(a::Arena, size)
       a.len + size > a.cap && return C_NULL
       res = a.ptr + a.len
       a.len += size
       res
       end

And that’s it. You can make an arena, tear it down, and allocate memory from it. I even introduced some minor safety features to make double-free and use-after-free of the arena more benign.

From that, there are lots of ways you can go. Thread-safety via atomics. Convenience methods for using the arena as a stack. Methods to get aligned allocations. Avoid memory leaks by letting julia GC take over the freeing via finalizers. Whatever floats your boat.

8 Likes