forked from tchajed/goose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
allocator.go
47 lines (40 loc) · 850 Bytes
/
allocator.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
package semantics
// patterns that show up in in-memory allocators
type unit struct{}
func findKey(m map[uint64]unit) (uint64, bool) {
var found uint64 = 0
var ok bool = false
for k := range m {
if !ok {
found = k
ok = true
}
// TODO: goose doesn't support break in map iteration
}
return found, ok
}
func allocate(m map[uint64]unit) (uint64, bool) {
k, ok := findKey(m)
delete(m, k)
return k, ok
}
func freeRange(sz uint64) map[uint64]unit {
m := make(map[uint64]unit)
for i := uint64(0); i < sz; i++ {
m[i] = unit{}
}
return m
}
func testAllocateDistinct() bool {
free := freeRange(4)
a1, _ := allocate(free)
a2, _ := allocate(free)
return a1 != a2
}
func testAllocateFull() bool {
free := freeRange(2)
_, ok1 := allocate(free)
_, ok2 := allocate(free)
_, ok3 := allocate(free)
return ok1 && ok2 && !ok3
}