about summary refs log tree commit diff
path: root/pkg/lang/vm/mem/mem.go
blob: 99412eae795b0a2c0df52c1eef5098c281139f54 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package mem

type Mem interface {
	Allocate(kind CellKind) (Ptr, error)
	AllocateAt(ptr Ptr, kind CellKind) error

	Set(ptr Ptr, v CellData) error
	Get(ptr Ptr) (CellData, error)
	Is(ptr Ptr, kind CellKind) bool
	Kind(ptr Ptr) CellKind

	Retain(ptr Ptr) error
	Release(ptr Ptr) error
	RefCount(ptr Ptr) int
}

type memImpl struct {
	cells []cell
	free  []Ptr
}

func New() Mem {
	cells := make([]cell, 1)

	// Reserve NullPtr
	cells[NullPtr].kind = CellKindForbidden

	return &memImpl{
		cells: cells,
		free:  make([]Ptr, 0),
	}
}

func (m *memImpl) Allocate(kind CellKind) (Ptr, error) {
	if kind == CellKindForbidden || kind == CellKindEmpty {
		return NullPtr, ErrInvalidCellKind{kind}
	}

	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 {
			// This should never happen.
			panic("cell marked as free was not empty")
		}

		m.cells[idx] = cell{kind: kind, refs: 1}
		return Ptr(idx), nil
	} else {
		if len(m.cells) > 10000 {
			return NullPtr, ErrMemOverflow
		}

		idx := len(m.cells)
		m.cells = append(m.cells, cell{kind: kind, refs: 1})
		return Ptr(idx), nil
	}
}

func (m *memImpl) AllocateAt(ptr Ptr, kind CellKind) error {
	if kind == CellKindForbidden || kind == CellKindEmpty {
		return ErrInvalidCellKind{kind}
	}

	if ptr < Ptr(len(m.cells)) && m.cells[ptr].kind != CellKindEmpty {
		return ErrInvalidMemAccess{ptr}
	}

	if ptr > 10000 {
		return ErrMemOverflow
	}

	if ptr >= Ptr(len(m.cells)) {
		m.cells = append(m.cells, make([]cell, ptr-Ptr(len(m.cells))+1)...)
	}

	m.cells[ptr] = cell{kind: kind, refs: 1}

	// Remove cell from free list, if it was there.
	for i, f := range m.free {
		if f == ptr {
			m.free = append(m.free[:i], m.free[i+1:]...)
			break
		}
	}

	return nil
}

func (m *memImpl) Set(ptr Ptr, v CellData) error {
	if err := m.validPtr(ptr); err != nil {
		return err
	}

	if m.cells[ptr].kind != v.MatchingCellKind() {
		return ErrDifferingCellKind{Ptr: ptr, Expected: m.cells[ptr].kind, Got: v.MatchingCellKind()}
	}

	m.cells[ptr].data = v
	return nil
}

func (m *memImpl) Get(ptr Ptr) (CellData, error) {
	if err := m.validPtr(ptr); err != nil {
		return nil, err
	}

	return m.cells[ptr].data, nil
}

func (m *memImpl) Is(ptr Ptr, kind CellKind) bool {
	if ptr >= Ptr(len(m.cells)) {
		return kind == CellKindEmpty
	}

	return m.cells[ptr].kind == kind
}

func (m *memImpl) Kind(ptr Ptr) CellKind {
	if ptr >= Ptr(len(m.cells)) {
		return CellKindEmpty
	}

	return m.cells[ptr].kind
}

func (m *memImpl) Retain(ptr Ptr) error {
	if err := m.validPtr(ptr); err != nil {
		return err
	}

	m.cells[ptr].refs++
	return nil
}

func (m *memImpl) Release(ptr Ptr) error {
	if err := m.validPtr(ptr); err != nil {
		return err
	}

	m.cells[ptr].refs--
	if m.cells[ptr].refs == 0 {
		c := m.cells[ptr].data

		if c != nil {
			c.DropCell(m)
		}

		m.cells[ptr] = cell{}
		m.free = append(m.free, ptr)
	}

	return nil
}

func (m *memImpl) RefCount(ptr Ptr) int {
	if err := m.validPtr(ptr); err != nil {
		return 0
	}

	return m.cells[ptr].refs
}

func (m *memImpl) validPtr(ptr Ptr) error {
	if ptr >= Ptr(len(m.cells)) {
		return ErrInvalidMemAccess{ptr}
	}

	kind := m.cells[ptr].kind
	if kind == CellKindForbidden || kind == CellKindEmpty {
		return ErrInvalidMemAccess{ptr}
	}

	return nil
}