-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear.go
51 lines (39 loc) · 1.02 KB
/
linear.go
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
package main
import "github.com/fatih/color"
// linear is the simplest allocator that does not support free.
type linear struct {
memorySize byte
prevAddr byte
nextAddr byte
totalUsed int
}
func (l *linear) name() string { return "linear" }
func (l *linear) init(size byte) { l.memorySize = size }
func (l *linear) malloc(size byte) {
if l.memorySize-l.nextAddr < size {
panic("out of memory")
}
l.totalUsed++
l.prevAddr = l.nextAddr
l.nextAddr += size
// return l.prevAddr
}
func (l *linear) free(addr byte) { panic("not supported") }
func (l *linear) printSnapshot() {
var cells []string
for i := byte(0); i < l.nextAddr; i++ {
if i >= l.prevAddr {
cells = append(cells, formatCell("xx", color.FgYellow, color.BlinkSlow))
} else {
cells = append(cells, formatCell("xx", color.FgYellow))
}
}
for i := l.nextAddr; i < l.memorySize; i++ {
cells = append(cells, formatCell("--"))
}
totalFree := 1
if l.nextAddr == l.memorySize {
totalFree = 0
}
printSnapshot(cells, totalFree, l.totalUsed)
}