vm: add reference counter to slots

This commit is contained in:
Evgenii Stratonikov 2020-05-08 15:34:32 +03:00
parent af2abedd86
commit 2cec088f08
2 changed files with 16 additions and 2 deletions

View file

@ -3,18 +3,32 @@ package vm
// Slot is a fixed-size slice of stack items.
type Slot struct {
storage []StackItem
refs *refCounter
}
// newSlot returns new slot of n items.
func newSlot(n int) *Slot {
func newSlot(n int, refs *refCounter) *Slot {
return &Slot{
storage: make([]StackItem, n),
refs: refs,
}
}
func (v *VM) newSlot(n int) *Slot {
return newSlot(n, v.refs)
}
// Set sets i-th storage slot.
func (s *Slot) Set(i int, item StackItem) {
if s.storage[i] == item {
return
}
old := s.storage[i]
s.storage[i] = item
if old != nil {
s.refs.Remove(old)
}
s.refs.Add(item)
}
// Get returns item contained in i-th slot.

View file

@ -8,7 +8,7 @@ import (
)
func TestSlot_Get(t *testing.T) {
s := newSlot(3)
s := newSlot(3, newRefCounter())
require.NotNil(t, s)
require.Equal(t, 3, s.Size())