Bringing C-like programming to the JVM with ease
Note
Currently under private development. On release, expect breaking changes.
Define a Struct interface:
interface Point : Struct {
var x: Int
var y: Int
}Then use an arena to alloc:
val points = StructArena.confined<Point>()
val a = points.alloc()
val b = points.alloc()
a.x = 5
b.y = 15
println("a: $a, b: $b") // Prints "a: Point(x=5, y=0), b: Point(x=0, y=15)"
Similar to Java FFM API's Arena, there are a few StructArenas available, accessed via its companion object.
- Arena Type: The function defined in
StructArenacompanion object - Thread-confined: Whether or not the data is bounded to the initial allocator thread of the arena. Usage from outside the initial thread may result in unexpected behavior including JVM crashes.
- Dynamic Count: Whether or not a dynamic (unbounded) count is supported. If not, exceeding the initial capacity
will result in an
OutOfMemoryError. - Free Support: Whether or not freeing is supported.
Whole arenameans only an entire arena can be freed at once. - Time Complexity: The Big O notation range of alloc/free performance.
- Alloc/Free Overhead: The general amount of overhead compared to raw thread stack.
- Get/Set Overhead: The general amount of overhead compared to raw thread stack.
| Arena Type | Thread-confined | Dynamic Count | Free Support | Time Complexity | Alloc/Free Overhead | Get/Set Overhead |
|---|---|---|---|---|---|---|
fixed(capacity: Long) |
Yes | No | Whole arena | O(1) |
Lowest possible (incr) | Lowest possible |
confined(capacity: Long) |
Yes | No | Whole arena | O(1)..O(n / 64) |
Very-low (thread-local) | Lowest possible |
confined() |
Yes | Yes | Individual | O(1)..O(n / 64) |
Low (get/put address) | Lowest possible |
permanent() |
No | Yes | None | O(1) |
Low (malloc/free sys call) | Low (volatile) |
shared(capacity: Long) |
No | No | Whole arena | O(1)..O(n / 64) |
Medium-low (atomic incr) | Low (atomic) |
shared() |
No | Yes | Whole arena | O(1) |
Medium (CAS) | Low (atomic) |
heap() |
No | Yes | Auto by GC | O(1) |
High (JVM object heap alloc) | Medium (CAS) |
Allocations via permanent() can never be freed - they will remain until the JVM process closes.
Calls to free() will result in an UnsupportedOperationException.
Allocations via heap() are automatically freed by the garbage collector, thus free() is a no-op.
Distributed under the terms of the MIT License.