-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhkernel.go
218 lines (202 loc) · 5.08 KB
/
hkernel.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) 2021 Silvano DAL ZILIO
//
// MIT License
// +build !buddy
package rudd
import (
"log"
"math"
"runtime"
"sync/atomic"
)
// Retnode is a kernel function of the BDD package. Use it at your own risk.
// Retnode returns a valid node from the value returned by a call to Makenode.
func (b *tables) Retnode(n int) Node {
if n < 0 || n > len(b.nodes) {
if _DEBUG {
log.Panicf("b.retnode(%d) not valid\n", n)
}
return nil
}
if n == 0 {
return bddzero
}
if n == 1 {
return bddone
}
x := n
if b.nodes[n].refcou < _MAXREFCOUNT {
b.nodes[n].refcou++
runtime.SetFinalizer(&x, b.nodefinalizer)
if _DEBUG {
atomic.AddUint64(&(b.setfinalizers), 1)
if _LOGLEVEL > 2 {
log.Printf("inc refcou %d\n", n)
}
}
}
return &x
}
func (b *tables) makenode(level int32, low int, high int, refstack []int) (int, error) {
if _DEBUG {
b.uniqueAccess++
}
// check whether children are equal, in which case we can skip the node
if low == high {
return low, nil
}
// otherwise try to find an existing node using the unique table
if res, ok := b.nodehash(level, low, high); ok {
if _DEBUG {
b.uniqueHit++
}
return res, nil
}
if _DEBUG {
b.uniqueMiss++
}
// If no existing node, we build one. If there is no available spot
// (b.freepos == 0), we try garbage collection and, as a last resort,
// resizing the BDD list.
var err error
if b.freepos == 0 {
// We garbage collect unused nodes to try and find spare space.
b.gbc(refstack)
err = errReset
// We also test if we are under the threshold for resising.
if (b.freenum*100)/len(b.nodes) <= b.minfreenodes {
err = b.noderesize()
if err != errResize {
return -1, errMemory
}
}
// Panic if we still have no free positions after all this
if b.freepos == 0 {
// b.seterror("Unable to resize BDD")
return -1, errMemory
}
}
// We can now build the new node in the first available spot
b.produced++
return b.setnode(level, low, high, 0), err
}
func (b *tables) gbc(refstack []int) {
if _LOGLEVEL > 0 {
log.Println("starting GC")
}
// runtime.GC()
// we append the current stats to the GC history
if _DEBUG {
b.gcstat.history = append(b.gcstat.history, gcpoint{
nodes: len(b.nodes),
freenodes: b.freenum,
setfinalizers: int(b.gcstat.setfinalizers),
calledfinalizers: int(b.gcstat.calledfinalizers),
})
if _LOGLEVEL > 0 {
log.Printf("runtime.GC() reclaimed %d references\n", b.gcstat.calledfinalizers)
}
b.gcstat.setfinalizers = 0
b.gcstat.calledfinalizers = 0
} else {
b.gcstat.history = append(b.gcstat.history, gcpoint{
nodes: len(b.nodes),
freenodes: b.freenum,
})
}
// we mark the nodes in the refstack to avoid collecting them
for _, r := range refstack {
b.markrec(int(r))
}
// we also protect nodes with a positive refcount (and therefore also the
// ones with a MAXREFCOUNT, such has variables)
for k := range b.nodes {
if b.nodes[k].refcou > 0 {
b.markrec(k)
}
}
b.freepos = 0
b.freenum = 0
// we do a pass through the nodes list to void the unmarked nodes. After
// finishing this pass, b.freepos points to the first free position in
// b.nodes, or it is 0 if we found none.
for n := len(b.nodes) - 1; n > 1; n-- {
if b.ismarked(n) && (b.nodes[n].low != -1) {
b.unmarknode(n)
} else {
b.delnode(b.nodes[n])
b.nodes[n].low = -1
b.nodes[n].high = b.freepos
b.freepos = n
b.freenum++
}
}
// we also invalidate the caches
// b.cachereset()
if _LOGLEVEL > 0 {
log.Printf("end GC; freenum: %d\n", b.freenum)
}
}
func (b *tables) noderesize() error {
if _LOGLEVEL > 0 {
log.Printf("start resize: %d\n", len(b.nodes))
}
// if b.error != nil {
// b.seterror("Error before resizing; %s", b.error)
// return b.error
// }
oldsize := len(b.nodes)
nodesize := len(b.nodes)
if (oldsize >= b.maxnodesize) && (b.maxnodesize > 0) {
// b.seterror("Cannot resize BDD, already at max capacity (%d nodes)", b.maxnodesize)
return errMemory
}
if oldsize > (math.MaxInt32 >> 1) {
nodesize = math.MaxInt32 - 1
} else {
nodesize = nodesize << 1
}
if b.maxnodeincrease > 0 && nodesize > (oldsize+b.maxnodeincrease) {
nodesize = oldsize + b.maxnodeincrease
}
if (nodesize > b.maxnodesize) && (b.maxnodesize > 0) {
nodesize = b.maxnodesize
}
if nodesize <= oldsize {
// b.seterror("Unable to grow size of BDD (%d nodes)", nodesize)
return errMemory
}
tmp := b.nodes
b.nodes = make([]huddnode, nodesize)
copy(b.nodes, tmp)
for n := oldsize; n < nodesize; n++ {
b.nodes[n].refcou = 0
b.nodes[n].level = 0
b.nodes[n].low = -1
b.nodes[n].high = n + 1
}
b.nodes[nodesize-1].high = b.freepos
b.freepos = oldsize
b.freenum += (nodesize - oldsize)
// b.cacheresize(len(b.nodes))
if _LOGLEVEL > 0 {
log.Printf("end resize: %d\n", len(b.nodes))
}
return errResize
}
func (b *tables) markrec(n int) {
if n < 2 || b.ismarked(n) || (b.nodes[n].low == -1) {
return
}
b.marknode(n)
b.markrec(b.nodes[n].low)
b.markrec(b.nodes[n].high)
}
func (b *tables) unmarkall() {
for k, v := range b.nodes {
if k < 2 || !b.ismarked(k) || (v.low == -1) {
continue
}
b.unmarknode(k)
}
}