-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.go
More file actions
102 lines (87 loc) · 1.71 KB
/
common.go
File metadata and controls
102 lines (87 loc) · 1.71 KB
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
package kernel
import "C"
import (
"runtime"
"runtime/cgo"
"unsafe"
)
type destroyableFuncs interface {
destroy(ptr unsafe.Pointer)
}
type copyableFuncs interface {
destroyableFuncs
copy(ptr unsafe.Pointer) unsafe.Pointer
}
type uniqueHandle struct {
ptr unsafe.Pointer
funcs destroyableFuncs
}
func newUniqueHandle(ptr unsafe.Pointer, funcs destroyableFuncs) *uniqueHandle {
if ptr == nil {
panic("ptr must be provided to create handle")
}
if funcs == nil {
panic("funcs must be provided to create handle")
}
h := &uniqueHandle{
ptr: ptr,
funcs: funcs,
}
runtime.SetFinalizer(h, (*uniqueHandle).destroy)
return h
}
func (h *uniqueHandle) destroy() {
if h.ptr != nil {
h.funcs.destroy(h.ptr)
h.ptr = nil
}
}
func (h *uniqueHandle) Destroy() {
runtime.SetFinalizer(h, nil)
h.destroy()
}
type handle struct {
ptr unsafe.Pointer
funcs copyableFuncs
}
func newHandle(ptr unsafe.Pointer, funcs copyableFuncs, fromOwned bool) *handle {
if ptr == nil {
panic("ptr must be provided to create handle")
}
if funcs == nil {
panic("funcs must be provided to create handle")
}
if !fromOwned {
ptr = funcs.copy(ptr)
if ptr == nil {
panic(ErrKernelInstantiate)
}
}
h := &handle{
ptr: ptr,
funcs: funcs,
}
runtime.SetFinalizer(h, (*handle).destroy)
return h
}
func (h *handle) destroy() {
if h.ptr != nil {
h.funcs.destroy(h.ptr)
h.ptr = nil
}
}
func (h *handle) Destroy() {
runtime.SetFinalizer(h, nil)
h.destroy()
}
//export go_delete_handle
func go_delete_handle(handle unsafe.Pointer) {
cgo.Handle(handle).Delete()
}
func ReverseBytes(data []byte) []byte {
result := make([]byte, len(data))
for i, b := range data {
result[len(data)-1-i] = b
}
return result
}