-
Notifications
You must be signed in to change notification settings - Fork 330
/
handle.go
146 lines (131 loc) · 3.4 KB
/
handle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2016 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package nodefs
import (
"log"
"sync"
)
// HandleMap translates objects in Go space to 64-bit handles that can
// be given out to -say- the linux kernel as NodeIds.
//
// The 32 bits version of this is a threadsafe wrapper around a map.
//
// To use it, include "handled" as first member of the structure
// you wish to export.
//
// This structure is thread-safe.
type handleMap interface {
// Register stores "obj" and returns a unique (NodeId, generation) tuple.
Register(obj *handled) (handle, generation uint64)
Count() int
// Decode retrieves a stored object from its 64-bit handle.
Decode(uint64) *handled
// Forget decrements the reference counter for "handle" by "count" and drops
// the object if the refcount reaches zero.
// Returns a boolean whether the object was dropped and the object itself.
Forget(handle uint64, count int) (bool, *handled)
// Handle gets the object's NodeId.
Handle(obj *handled) uint64
// Has checks if NodeId is stored.
Has(uint64) bool
}
type handled struct {
handle uint64
count int
}
func (h *handled) verify() {
if h.count < 0 {
log.Panicf("negative lookup count %d", h.count)
}
if (h.count == 0) != (h.handle == 0) {
log.Panicf("registration mismatch: lookup %d id %d", h.count, h.handle)
}
}
const _ALREADY_MSG = "Object already has a handle"
////////////////////////////////////////////////////////////////
// portable version using 32 bit integers.
type portableHandleMap struct {
sync.RWMutex
// The generation counter is incremented each time a NodeId is reused,
// hence the (NodeId, Generation) tuple is always unique.
generation uint64
// Number of currently used handles
used int
// Array of Go objects indexed by NodeId
handles []*handled
// Free slots in the "handles" array
freeIds []uint64
}
func newPortableHandleMap() *portableHandleMap {
return &portableHandleMap{
// Avoid handing out ID 0 and 1.
handles: []*handled{nil, nil},
}
}
func (m *portableHandleMap) Register(obj *handled) (handle, generation uint64) {
m.Lock()
if obj.count == 0 {
if len(m.freeIds) == 0 {
handle = uint64(len(m.handles))
m.handles = append(m.handles, obj)
} else {
handle = m.freeIds[len(m.freeIds)-1]
m.freeIds = m.freeIds[:len(m.freeIds)-1]
m.generation++
m.handles[handle] = obj
}
m.used++
obj.handle = handle
} else {
handle = obj.handle
}
obj.count++
generation = m.generation
m.Unlock()
return
}
func (m *portableHandleMap) Handle(obj *handled) (h uint64) {
m.RLock()
if obj.count == 0 {
h = 0
} else {
h = obj.handle
}
m.RUnlock()
return h
}
func (m *portableHandleMap) Count() int {
m.RLock()
c := m.used
m.RUnlock()
return c
}
func (m *portableHandleMap) Decode(h uint64) *handled {
m.RLock()
v := m.handles[h]
m.RUnlock()
return v
}
func (m *portableHandleMap) Forget(h uint64, count int) (forgotten bool, obj *handled) {
m.Lock()
obj = m.handles[h]
obj.count -= count
if obj.count < 0 {
log.Panicf("underflow: handle %d, count %d, object %d", h, count, obj.count)
} else if obj.count == 0 {
m.handles[h] = nil
m.freeIds = append(m.freeIds, h)
m.used--
forgotten = true
obj.handle = 0
}
m.Unlock()
return forgotten, obj
}
func (m *portableHandleMap) Has(h uint64) bool {
m.RLock()
ok := m.handles[h] != nil
m.RUnlock()
return ok
}