forked from TrueCloudLab/neoneo-go
vm: add reference counter to slots
This commit is contained in:
parent
af2abedd86
commit
2cec088f08
2 changed files with 16 additions and 2 deletions
|
@ -3,18 +3,32 @@ package vm
|
||||||
// Slot is a fixed-size slice of stack items.
|
// Slot is a fixed-size slice of stack items.
|
||||||
type Slot struct {
|
type Slot struct {
|
||||||
storage []StackItem
|
storage []StackItem
|
||||||
|
refs *refCounter
|
||||||
}
|
}
|
||||||
|
|
||||||
// newSlot returns new slot of n items.
|
// newSlot returns new slot of n items.
|
||||||
func newSlot(n int) *Slot {
|
func newSlot(n int, refs *refCounter) *Slot {
|
||||||
return &Slot{
|
return &Slot{
|
||||||
storage: make([]StackItem, n),
|
storage: make([]StackItem, n),
|
||||||
|
refs: refs,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v *VM) newSlot(n int) *Slot {
|
||||||
|
return newSlot(n, v.refs)
|
||||||
|
}
|
||||||
|
|
||||||
// Set sets i-th storage slot.
|
// Set sets i-th storage slot.
|
||||||
func (s *Slot) Set(i int, item StackItem) {
|
func (s *Slot) Set(i int, item StackItem) {
|
||||||
|
if s.storage[i] == item {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
old := s.storage[i]
|
||||||
s.storage[i] = item
|
s.storage[i] = item
|
||||||
|
if old != nil {
|
||||||
|
s.refs.Remove(old)
|
||||||
|
}
|
||||||
|
s.refs.Add(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns item contained in i-th slot.
|
// Get returns item contained in i-th slot.
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSlot_Get(t *testing.T) {
|
func TestSlot_Get(t *testing.T) {
|
||||||
s := newSlot(3)
|
s := newSlot(3, newRefCounter())
|
||||||
require.NotNil(t, s)
|
require.NotNil(t, s)
|
||||||
require.Equal(t, 3, s.Size())
|
require.Equal(t, 3, s.Size())
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue