diff options
Diffstat (limited to 'pkg/lang/vm/mem/mem.go')
| -rw-r--r-- | pkg/lang/vm/mem/mem.go | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/pkg/lang/vm/mem/mem.go b/pkg/lang/vm/mem/mem.go new file mode 100644 index 0000000..4335347 --- /dev/null +++ b/pkg/lang/vm/mem/mem.go @@ -0,0 +1,68 @@ +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)) + } +} |
