package mem type Mem struct { cells []cell free []int } func New() Mem { return Mem{ cells: make([]cell, 0), free: make([]int, 0), } } func (m *Mem) Allocate(kind CellKind) Ptr { if len(m.free) > 0 { idx := m.free[len(m.free)-1] m.free = m.free[:len(m.free)-1] if m.cells[idx].kind != CellKindEmpty { panic("invalid free cell") } m.cells[idx].kind = kind return Ptr(idx) } else { idx := len(m.cells) m.cells = append(m.cells, cell{kind: kind}) return Ptr(idx) } } func (m *Mem) Set(ptr Ptr, v any) { if ptr >= Ptr(len(m.cells)) { panic("out of bounds") } m.cells[ptr].data = v } func (m *Mem) Get(ptr Ptr) any { if ptr >= Ptr(len(m.cells)) { panic("out of bounds") } return m.cells[ptr].data } func (m *Mem) Retain(ptr Ptr) { if ptr >= Ptr(len(m.cells)) { panic("out of bounds") } m.cells[ptr].refs++ } func (m *Mem) Release(ptr Ptr) { if ptr >= Ptr(len(m.cells)) { panic("out of bounds") } m.cells[ptr].refs-- if m.cells[ptr].refs == 0 { m.cells[ptr].kind = CellKindEmpty m.free = append(m.free, int(ptr)) } }