-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
73 lines (62 loc) · 2.04 KB
/
main.go
File metadata and controls
73 lines (62 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// The `so/mem` package is responsible for manual memory management.
// It supports heap allocation for single values and slices,
// and custom allocators for more control.
package main
import "solod.dev/so/mem"
type Point struct {
x, y int
}
func main() {
// mem.Alloc allocates a single value on the heap.
// Always pair it with mem.Free to avoid leaks.
// The first argument is an optional allocator;
// passing nil uses the default allocator (mem.System).
p := mem.Alloc[Point](mem.System)
defer mem.Free(mem.System, p)
p.x = 10
p.y = 20
println("point p =", p.x, p.y)
// mem.AllocSlice allocates a slice on the heap
// with a given length and capacity.
nums := mem.AllocSlice[int](mem.System, 5, 5)
defer mem.FreeSlice(mem.System, nums)
for i := range nums {
nums[i] = (i + 1) * 10
}
println("slice =", nums[0], nums[1], nums[2], nums[3], nums[4])
// mem.TryAlloc returns an error if allocation fails
// instead of panicking.
q, err := mem.TryAlloc[Point](mem.System)
if err != nil {
panic(err)
}
defer mem.Free(mem.System, q)
q.x = 30
q.y = 40
println("point q =", q.x, q.y)
// mem.Arena uses a pre-allocated buffer for fast, linear allocation.
// Individual Free calls are no-ops - call Reset to reclaim
// all memory at once. If the backing buffer is heap-allocated,
// free it with mem.FreeSlice when done.
//
// Arenas are ideal for short-lived allocations with simple lifetimes,
// such as during parsing or temporary buffers.
buf := make([]byte, 1024)
arena := mem.NewArena(buf)
var a mem.Allocator = &arena
// Allocate 10 points (16B x 10 = 160B) from the arena.
points := make([]*Point, 10)
for i := range points {
pt := mem.Alloc[Point](a)
pt.x = i + 1
pt.y = (i + 1) * 2
points[i] = pt
}
println("allocated", len(points), "points in arena")
println("points[0] =", points[0].x, points[0].y)
println("points[9] =", points[9].x, points[9].y)
// Reset reclaims all arena memory for reuse.
arena.Reset()
// If the buffer was heap-allocated, we'd need to free it here:
// mem.FreeSlice(mem.System, buf)
}