summaryrefslogtreecommitdiff
path: root/handles.go
diff options
context:
space:
mode:
Diffstat (limited to 'handles.go')
-rw-r--r--handles.go20
1 files changed, 9 insertions, 11 deletions
diff --git a/handles.go b/handles.go
index a062231..f5d30f0 100644
--- a/handles.go
+++ b/handles.go
@@ -10,15 +10,14 @@ type HandleList struct {
sync.RWMutex
// stores the Go pointers
handles []interface{}
- // Indicates which indices are in use, and keeps a pointer to slot int variable (the handle)
- // in the Go world, so that the Go garbage collector does not free it.
- set map[int]*int
+ // Indicates which indices are in use.
+ set map[int]bool
}
func NewHandleList() *HandleList {
return &HandleList{
handles: make([]interface{}, 5),
- set: make(map[int]*int),
+ set: make(map[int]bool),
}
}
@@ -26,8 +25,7 @@ func NewHandleList() *HandleList {
// list. You must only run this function while holding a write lock.
func (v *HandleList) findUnusedSlot() int {
for i := 1; i < len(v.handles); i++ {
- _, isUsed := v.set[i]
- if !isUsed {
+ if !v.set[i] {
return i
}
}
@@ -48,16 +46,16 @@ func (v *HandleList) Track(pointer interface{}) unsafe.Pointer {
slot := v.findUnusedSlot()
v.handles[slot] = pointer
- v.set[slot] = &slot // Keep a pointer to slot in Go world, so it's not freed by GC.
+ v.set[slot] = true
v.Unlock()
- return unsafe.Pointer(&slot)
+ return unsafe.Pointer(uintptr(slot))
}
// Untrack stops tracking the pointer given by the handle
func (v *HandleList) Untrack(handle unsafe.Pointer) {
- slot := *(*int)(handle)
+ slot := int(uintptr(handle))
v.Lock()
@@ -69,11 +67,11 @@ func (v *HandleList) Untrack(handle unsafe.Pointer) {
// Get retrieves the pointer from the given handle
func (v *HandleList) Get(handle unsafe.Pointer) interface{} {
- slot := *(*int)(handle)
+ slot := int(uintptr(handle))
v.RLock()
- if _, ok := v.set[slot]; !ok {
+ if !v.set[slot] {
panic(fmt.Sprintf("invalid pointer handle: %p", handle))
}