Skip to content

[next] Prevent slot int variable from being GCed. #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions handles.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ type HandleList struct {
sync.RWMutex
// stores the Go pointers
handles []interface{}
// indicates which indices are in use
set map[int]bool
// 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
}

func NewHandleList() *HandleList {
return &HandleList{
handles: make([]interface{}, 5),
set: make(map[int]bool),
set: make(map[int]*int),
}
}

// findUnusedSlot finds the smallest-index empty space in our
// 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]
_, isUsed := v.set[i]
if !isUsed {
return i
}
Expand All @@ -47,7 +48,7 @@ func (v *HandleList) Track(pointer interface{}) unsafe.Pointer {

slot := v.findUnusedSlot()
v.handles[slot] = pointer
v.set[slot] = true
v.set[slot] = &slot // Keep a pointer to slot in Go world, so it's not freed by GC.

v.Unlock()

Expand Down