summaryrefslogtreecommitdiff
path: root/handles.go
diff options
context:
space:
mode:
authorIan Lance Taylor <[email protected]>2016-02-16 21:34:43 -0800
committerIan Lance Taylor <[email protected]>2016-02-16 21:34:43 -0800
commita1f25eafec55509d49dffb4c84f7c5b729e6a85e (patch)
tree650ef0ade4eafdea041e9c468a253249ee11957f /handles.go
parentb70973e5c71b12f7ac42467d6b409ab4c7467dbd (diff)
handles, merge: simplify code, don't copy file contents
Diffstat (limited to 'handles.go')
-rw-r--r--handles.go14
1 files changed, 6 insertions, 8 deletions
diff --git a/handles.go b/handles.go
index a855717..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,7 +46,7 @@ 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()
@@ -73,7 +71,7 @@ func (v *HandleList) Get(handle unsafe.Pointer) interface{} {
v.RLock()
- if _, ok := v.set[slot]; !ok {
+ if !v.set[slot] {
panic(fmt.Sprintf("invalid pointer handle: %p", handle))
}