mem
Working with memory directly: allocating and freeing, copying, arenas and pools, and leak checking.
import mem
Most programs never need this package: vectors, maps and strings look after their own memory. The mem package is for low-level work, such as talking to a C library or building a data structure of your own. Everything you take with Allocate has to be given back with Free.
An arena is one large block you take pieces off the front of and then release all at once; it is very fast for work with many small objects that share a lifetime. A pool hands out and takes back blocks of one fixed size.
Types
Arena
struct Arena| Name | Type | Description |
|---|---|---|
buf | void* | The address of the arena's memory. |
cap | u64 = 0 | The capacity in bytes. |
used | u64 = 0 | How much has been taken. |
import mem
func main:
mut arena := mem.NewArena(1024 as u64)
println mem.ArenaCapacity(arena), mem.ArenaUsed(arena)
a := mem.ArenaAlloc(arena, 100 as u64)
b := mem.ArenaAllocZeroed(arena, 200 as u64)
println a != null, b != null, mem.ArenaUsed(arena) >= 300, mem.ArenaRemaining(arena) <= 724
println mem.ArenaAlloc(arena, 4000 as u64) == null
mem.ArenaReset(arena)
println mem.ArenaUsed(arena), arena.cap
mem.ArenaFree(arena)
endPool
struct Pool| Name | Type | Description |
|---|---|---|
block_size | u64 = 0 | The size of each block. |
block_count | u64 = 0 | The number of blocks. |
blocks | void* | The address of the blocks' memory. |
free_list | void* | The list of free blocks. |
import mem
func main:
mut pool := mem.NewPool(32 as u64, 4 as u64)
println mem.PoolBlockSize(pool), mem.PoolCapacity(pool)
b1 := mem.PoolAlloc(pool)
b2 := mem.PoolAlloc(pool)
println b1 != null, b2 != null, b1 != b2
mem.PoolFree(pool, b1)
b3 := mem.PoolAlloc(pool)
println b3 == b1
mem.PoolDestroy(pool)
endFunctions
NewArena
func NewArena(capacity: u64): ArenaThe example for this item is shown with the ones above: mem-Arena.salam
ArenaAlloc
func ArenaAlloc(a &: Arena, size: u64): void*The example for this item is shown with the ones above: mem-Arena.salam
ArenaAllocZeroed
func ArenaAllocZeroed(a &: Arena, size: u64): void*ArenaAlloc, with the memory zeroed.The example for this item is shown with the ones above: mem-Arena.salam
ArenaReset
func ArenaReset(a &: Arena)The example for this item is shown with the ones above: mem-Arena.salam
ArenaFree
func ArenaFree(a &: Arena)The example for this item is shown with the ones above: mem-Arena.salam
ArenaUsed
func ArenaUsed(a &: Arena): u64The example for this item is shown with the ones above: mem-Arena.salam
ArenaCapacity
func ArenaCapacity(a &: Arena): u64The example for this item is shown with the ones above: mem-Arena.salam
ArenaRemaining
func ArenaRemaining(a &: Arena): u64The example for this item is shown with the ones above: mem-Arena.salam
NewPool
func NewPool(block_size: u64, block_count: u64): PoolThe example for this item is shown with the ones above: mem-Pool.salam
PoolAlloc
func PoolAlloc(p &: Pool): void*The example for this item is shown with the ones above: mem-Pool.salam
PoolFree
func PoolFree(p &: Pool, ptr: void*)The example for this item is shown with the ones above: mem-Pool.salam
PoolBlockSize
func PoolBlockSize(p &: Pool): u64The example for this item is shown with the ones above: mem-Pool.salam
PoolCapacity
func PoolCapacity(p &: Pool): u64The example for this item is shown with the ones above: mem-Pool.salam
PoolDestroy
func PoolDestroy(p &: Pool)The example for this item is shown with the ones above: mem-Pool.salam
Allocate
func Allocate(size: u64): void*import mem
func main:
b := mem.Allocate(8 as u64)
mem.Set(b, 0, 8 as u64)
bytes := b as i8*
bytes[0] = 65 as i8
bytes[1] = 66 as i8
b2 := mem.AllocateZeroed(8 as u64)
mem.Copy(b2, b, 2 as u64)
bytes2 := b2 as i8*
println bytes2[0], bytes2[1], bytes2[2]
mem.MemMove(b, (b as i64 + 1) as void*, 1 as u64)
println bytes[0]
b3 := mem.Reallocate(b, 64 as u64)
arr := mem.AllocateArray(4 as u64, 8 as u64)
arr64 := arr as i64*
println arr64[3]
mem.Free(b3)
mem.Free(b2)
mem.Free(arr)
endReallocate
func Reallocate(ptr: void*, size: u64): void*The example for this item is shown with the ones above: mem-Allocate.salam
Free
func Free(ptr: void*)The example for this item is shown with the ones above: mem-Allocate.salam
Set
func Set(ptr: void*, value: int, n: u64)n bytes with the given value.The example for this item is shown with the ones above: mem-Allocate.salam
Copy
func Copy(dst: void*, src: void*, n: u64)n bytes; the two ranges must not overlap.The example for this item is shown with the ones above: mem-Allocate.salam
MemMove
func MemMove(dst: void*, src: void*, n: u64): void*Copy, but correct even when the ranges overlap.The example for this item is shown with the ones above: mem-Allocate.salam
AllocateZeroed
func AllocateZeroed(size: u64): void*The example for this item is shown with the ones above: mem-Allocate.salam
AllocateArray
func AllocateArray(count: u64, size: u64): void*count elements of size bytes each.The example for this item is shown with the ones above: mem-Allocate.salam
AllocCount
func AllocCount(): i64import mem
func main:
println mem.IsMemDebug(), mem.IsAsanActive(), mem.MemDebugActive()
println mem.AllocCount(), mem.FreeCount(), mem.LiveBytes(), mem.PeakBytes()
mem.CheckLeaks()
endFreeCount
func FreeCount(): i64The example for this item is shown with the ones above: mem-Debug.salam
LiveBytes
func LiveBytes(): i64The example for this item is shown with the ones above: mem-Debug.salam
PeakBytes
func PeakBytes(): i64The example for this item is shown with the ones above: mem-Debug.salam
IsMemDebug
func IsMemDebug(): boolThe example for this item is shown with the ones above: mem-Debug.salam
IsAsanActive
func IsAsanActive(): boolThe example for this item is shown with the ones above: mem-Debug.salam
MemDebugActive
func MemDebugActive(): boolThe example for this item is shown with the ones above: mem-Debug.salam
CheckLeaks
func CheckLeaks()The example for this item is shown with the ones above: mem-Debug.salam