-
Notifications
You must be signed in to change notification settings - Fork 0
/
autofree.go
229 lines (207 loc) · 4.89 KB
/
autofree.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
219
220
221
222
223
224
225
226
227
228
229
package nogc
import (
"unsafe"
)
// AutoFree is a singly linked list (Stack) of nodes which contain pointers
// that will all free when Free is called. uintptr is used to ensure the
// compiler doesn't confuse it for a Go GC managed pointer.
type AutoFree uintptr
//goland:noinspection GoVetUnsafePointer
func (af AutoFree) Count() uintptr {
return ((*autoHead)(unsafe.Pointer(af))).count
}
//goland:noinspection GoVetUnsafePointer
func (af AutoFree) Size() uintptr {
return ((*autoHead)(unsafe.Pointer(af))).bytes
}
//goland:noinspection GoVetUnsafePointer
func (af AutoFree) Max() uintptr {
return ((*autoHead)(unsafe.Pointer(af))).max
}
type autoHead struct {
head Pointer // pointer to the head node
max uintptr // max number of entries per node
count uintptr
bytes uintptr
}
type autoNode struct {
len uintptr
next Pointer
first struct{}
}
//goland:noinspection GoVetUnsafePointer
func NewAuto(nodeSize uintptr) AutoFree {
if nodeSize == 0 {
nodeSize = 32
}
p, s := AllocCap(unsafe.Sizeof(autoHead{}) + unsafe.Sizeof(autoNode{}) + (nodeSize * unsafe.Sizeof(Pointer(0))))
h := (*autoHead)(p.Unsafe())
h.head = Pointer(uintptr(p) + unsafe.Sizeof(autoHead{}))
h.max = nodeSize
h.count = 0
h.bytes = s
n := (*autoNode)(unsafe.Pointer(h.head))
n.len = 0
n.next = 0
return AutoFree(p)
}
func (af *AutoFree) Scope(fn func(AutoFree)) {
if fn != nil {
fn(*af)
}
af.Free()
}
//goland:noinspection GoVetUnsafePointer
func (af *AutoFree) HasNext() bool {
return *(*uintptr)(unsafe.Pointer(*af)) != 0
}
//goland:noinspection GoVetUnsafePointer
func (af *AutoFree) Next() AutoFree {
if af == nil {
return 0
}
p := uintptr(*af)
return AutoFree(*(*uintptr)(unsafe.Pointer(p)))
}
//goland:noinspection GoVetUnsafePointer
func (af AutoFree) Alloc(size uintptr) Pointer {
if af == 0 {
return Pointer(0)
}
p := Alloc(size)
af.add(p)
return p
}
func (af AutoFree) AllocCap(size uintptr) FatPointer {
p, c := AllocCap(size)
if p == 0 {
return FatPointer{}
}
af.add(p)
return FatPointer{
Pointer: p,
len: c,
}
}
//goland:noinspection GoVetUnsafePointer
func (af AutoFree) Bytes(size uintptr) Bytes {
if af == 0 {
return Bytes{}
}
b := AllocBytes(size)
af.add(b.allocationPointer())
return b
}
//goland:noinspection GoVetUnsafePointer
func (af *AutoFree) add(ptr Pointer) {
if af == nil {
return
}
h := (*autoHead)(unsafe.Pointer(*af))
n := (*autoNode)(unsafe.Pointer(h.head))
if n == nil {
return
}
if n.len == h.max {
nextPtr, sz := AllocCap(unsafe.Sizeof(autoNode{}) + (h.max * unsafe.Sizeof(Pointer(0))))
h.bytes += sz + Sizeof(ptr)
next := (*autoNode)(nextPtr.Unsafe())
// Add length to 1
next.len = 1
// Link to current n
next.next = Pointer(unsafe.Pointer(n))
// Update reference to new n
h.head = nextPtr
// Add first item
*(*uintptr)(unsafe.Pointer(&next.first)) = uintptr(ptr)
} else {
h.bytes += Sizeof(ptr)
// Add item
*(*Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(&n.first)) + (n.len * unsafe.Sizeof(uintptr(0))))) = ptr
// Increment length
n.len++
}
h.count++
}
// Close releases / frees every allocation
func (af *AutoFree) Close() error {
if af == nil {
return nil
}
af.Free()
return nil
}
// Free releases every allocation
//goland:noinspection GoVetUnsafePointer
func (af *AutoFree) Free() {
if af == nil {
return
}
head := (*autoHead)(unsafe.Pointer(*af))
n := (*autoNode)(unsafe.Pointer(head.head))
if n == nil {
return
}
for n != nil {
var (
start = uintptr(unsafe.Pointer(&n.first))
end = start + (n.len * unsafe.Sizeof(Pointer(0)))
item Pointer
)
for i := start; i < end; i += unsafe.Sizeof(Pointer(0)) {
item = *(*Pointer)(unsafe.Pointer(i))
if item == 0 {
break
}
Free(item)
}
if n.next == 0 {
// Free header node
Free(Pointer(uintptr(unsafe.Pointer(n)) - unsafe.Sizeof(autoHead{})))
break
}
Free(Pointer(unsafe.Pointer(n)))
n = (*autoNode)(unsafe.Pointer(n.next))
}
*af = 0
}
//goland:noinspection GoVetUnsafePointer
func (af *AutoFree) Print() {
head := (*autoHead)(unsafe.Pointer(*af))
n := (*autoNode)(unsafe.Pointer(head.head))
if n == nil {
return
}
println("AutoFree =>", " count:", head.count, " bytes:", head.bytes, " addr:", uint(Pointer(unsafe.Pointer(head))))
count := -1
for n != nil {
count++
var (
start = uintptr(unsafe.Pointer(&n.first))
end = start + (n.len * unsafe.Sizeof(Pointer(0)))
item Pointer
index uintptr
)
println("\t[", count, "] ->", n.len, "items")
for i := start; i < end; i += unsafe.Sizeof(Pointer(0)) {
item = *(*Pointer)(unsafe.Pointer(i))
if item == 0 {
break
}
index = (i - start) / unsafe.Sizeof(Pointer(0))
space := ""
if index < 10 {
space = " "
} else if index < 100 {
space = " "
} else if index < 1000 {
space = " "
}
println("\t\t", space, uint(index), "->", uint(item))
}
if n.next == 0 {
break
}
n = (*autoNode)(unsafe.Pointer(n.next))
}
}