File tree 1 file changed +7
-0
lines changed 1 file changed +7
-0
lines changed Original file line number Diff line number Diff line change 6
6
"unsafe"
7
7
)
8
8
9
+ // PtrProxy creates a safe pointer registry. It hangs on to an unsafe.Pointer and
10
+ // returns a totally-safe C.uint ID that can be used to look up the original
11
+ // pointer by using it.
9
12
func PtrProxy () * ptrProxy {
10
13
return & ptrProxy {
11
14
lookup : map [uint ]unsafe.Pointer {},
@@ -18,6 +21,8 @@ type ptrProxy struct {
18
21
lookup map [uint ]unsafe.Pointer
19
22
}
20
23
24
+ // Ref registers the given pointer and returns a corresponding id that can be
25
+ // used to retrieve it later.
21
26
func (p * ptrProxy ) Ref (ptr unsafe.Pointer ) C.uint {
22
27
p .Lock ()
23
28
id := p .count
@@ -27,13 +32,15 @@ func (p *ptrProxy) Ref(ptr unsafe.Pointer) C.uint {
27
32
return C .uint (id )
28
33
}
29
34
35
+ // Deref takes an id and returns the corresponding pointer if it exists.
30
36
func (p * ptrProxy ) Deref (id C.uint ) (unsafe.Pointer , bool ) {
31
37
p .Lock ()
32
38
val , ok := p .lookup [uint (id )]
33
39
p .Unlock ()
34
40
return val , ok
35
41
}
36
42
43
+ // Free releases a registered pointer by its id.
37
44
func (p * ptrProxy ) Free (id C.uint ) {
38
45
p .Lock ()
39
46
delete (p .lookup , uint (id ))
You can’t perform that action at this time.
0 commit comments