forked from hillu/go-yara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_legacy.go
42 lines (33 loc) · 701 Bytes
/
handle_legacy.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
//+build !go1.17
// This variant contains a backport of go 1.18's "runtime/cgo".Handle.
package yara
import (
"sync"
"sync/atomic"
)
type cgoHandle uintptr
func cgoNewHandle(v interface{}) cgoHandle {
h := atomic.AddUintptr(&handleIdx, 1)
if h == 0 {
panic("cgoNewHandle: ran out of handle space")
}
handles.Store(h, v)
return cgoHandle(h)
}
func (h cgoHandle) Value() interface{} {
v, ok := handles.Load(uintptr(h))
if !ok {
panic("cgoHandle: misuse of an invalid Handle")
}
return v
}
func (h cgoHandle) Delete() {
_, ok := handles.LoadAndDelete(uintptr(h))
if !ok {
panic("cgoHandle: misuse of an invalid Handle")
}
}
var (
handles = sync.Map{}
handleIdx uintptr
)