diff options
| author | Carlos Martín Nieto <[email protected]> | 2016-02-18 13:10:08 +0100 |
|---|---|---|
| committer | Carlos Martín Nieto <[email protected]> | 2016-02-18 13:10:08 +0100 |
| commit | f05417aaba1a6a01d9533c38b4901bfc58002f49 (patch) | |
| tree | 941f760c4a30200a0bfd0ce54cfd00e425aef8be /handles.go | |
| parent | 55594814c9009f9d645aea39a9f917cf82666228 (diff) | |
| parent | dc8b154f4f1b346fb6b8dee99fcfa6e4ca2d2d24 (diff) | |
Merge pull request #282 from ianlancetaylor/master
handles, merge, odb: changes for Go 1.6 pointer passing rules
Diffstat (limited to 'handles.go')
| -rw-r--r-- | handles.go | 20 |
1 files changed, 9 insertions, 11 deletions
@@ -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)) } |
