-
Notifications
You must be signed in to change notification settings - Fork 2
/
keep_alive.go
107 lines (88 loc) · 2.21 KB
/
keep_alive.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
package unsafekit
import (
"fmt"
"reflect"
"runtime"
"unsafe"
"github.com/insolar/vanilla/longbits"
)
func KeepAliveWhile(p unsafe.Pointer, fn func(unsafe.Pointer) uintptr) uintptr {
res := fn(p)
runtime.KeepAlive(p)
return res
}
const keepAlivePageLen = 4096/PtrSize - 4
type keepAlivePage [keepAlivePageLen]unsafe.Pointer
type KeepAliveList struct {
list []unsafe.Pointer
lastPageCount uint16
listOfPages bool
}
func (p *KeepAliveList) Reset() {
*p = KeepAliveList{}
}
func (p *KeepAliveList) Keep(v interface{}) {
vv := reflect.ValueOf(v)
switch vv.Kind() {
case reflect.Chan, reflect.Map, reflect.Ptr, reflect.UnsafePointer, reflect.Slice:
p.keep(vv.Pointer())
case reflect.String:
p.keepStrData(vv.String())
default:
panic(fmt.Sprint("unsupported value", vv))
}
runtime.KeepAlive(v)
}
func (p *KeepAliveList) KeepDataOf(v longbits.ByteString) {
p.keepStrData(string(v))
runtime.KeepAlive(v)
}
func (p *KeepAliveList) KeepDataOfBytes(v []byte) {
pSlice := (*reflect.SliceHeader)(unsafe.Pointer(&v))
p.keep(pSlice.Data)
runtime.KeepAlive(v)
}
func (p *KeepAliveList) keepStrData(s string) {
pString := (*reflect.StringHeader)(unsafe.Pointer(&s))
p.keep(pString.Data)
}
func (p *KeepAliveList) keep(v uintptr) {
switch {
case p.listOfPages:
if p.lastPageCount == keepAlivePageLen {
break
}
switch {
case len(p.list) < 2:
panic("illegal state")
case p.lastPageCount > keepAlivePageLen:
panic("illegal state")
}
lastPage := (*keepAlivePage)(p.list[len(p.list)-1])
lastPage[p.lastPageCount] = unsafe.Pointer(v)
p.lastPageCount++
return
case p.lastPageCount != 0:
panic("illegal state")
case len(p.list) == keepAlivePageLen:
lastPage := new(keepAlivePage)
copy(lastPage[:], p.list)
p.list = make([]unsafe.Pointer, 1, 2)
p.list[0] = unsafe.Pointer(lastPage)
case len(p.list) > keepAlivePageLen:
panic("illegal state")
default:
p.list = append(p.list, unsafe.Pointer(v))
return
}
switch {
case p.lastPageCount != keepAlivePageLen:
panic("illegal state")
case len(p.list) < 1:
panic("illegal state")
}
lastPage := new(keepAlivePage)
p.list = append(p.list, (unsafe.Pointer)(lastPage))
lastPage[0] = unsafe.Pointer(p)
p.lastPageCount = 1
}