Skip to content

Commit 73c263f

Browse files
committed
ptrProxy comments
1 parent 724e269 commit 73c263f

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

ptrproxy.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
"unsafe"
77
)
88

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.
912
func PtrProxy() *ptrProxy {
1013
return &ptrProxy{
1114
lookup: map[uint]unsafe.Pointer{},
@@ -18,6 +21,8 @@ type ptrProxy struct {
1821
lookup map[uint]unsafe.Pointer
1922
}
2023

24+
// Ref registers the given pointer and returns a corresponding id that can be
25+
// used to retrieve it later.
2126
func (p *ptrProxy) Ref(ptr unsafe.Pointer) C.uint {
2227
p.Lock()
2328
id := p.count
@@ -27,13 +32,15 @@ func (p *ptrProxy) Ref(ptr unsafe.Pointer) C.uint {
2732
return C.uint(id)
2833
}
2934

35+
// Deref takes an id and returns the corresponding pointer if it exists.
3036
func (p *ptrProxy) Deref(id C.uint) (unsafe.Pointer, bool) {
3137
p.Lock()
3238
val, ok := p.lookup[uint(id)]
3339
p.Unlock()
3440
return val, ok
3541
}
3642

43+
// Free releases a registered pointer by its id.
3744
func (p *ptrProxy) Free(id C.uint) {
3845
p.Lock()
3946
delete(p.lookup, uint(id))

0 commit comments

Comments
 (0)