mirror of https://github.com/ceph/go-ceph
callbacks: use uintptr instead of int as index
Since the index of the registered callbacks is basically only used as void*/unsafe.Pointer, it makes more sense and code simpler, if the index type is uintptr instead of a simple int. Signed-off-by: Sven Anderson <sven@redhat.com>
This commit is contained in:
parent
9cd28289b5
commit
176a163c3c
|
@ -19,17 +19,17 @@ import (
|
||||||
// to control and validate what "callbacks" get used.
|
// to control and validate what "callbacks" get used.
|
||||||
type Callbacks struct {
|
type Callbacks struct {
|
||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
cmap map[int]interface{}
|
cmap map[uintptr]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new callbacks tracker.
|
// New returns a new callbacks tracker.
|
||||||
func New() *Callbacks {
|
func New() *Callbacks {
|
||||||
return &Callbacks{cmap: make(map[int]interface{})}
|
return &Callbacks{cmap: make(map[uintptr]interface{})}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a callback/object to the tracker and return a new index
|
// Add a callback/object to the tracker and return a new index
|
||||||
// for the object.
|
// for the object.
|
||||||
func (cb *Callbacks) Add(v interface{}) int {
|
func (cb *Callbacks) Add(v interface{}) uintptr {
|
||||||
cb.mutex.Lock()
|
cb.mutex.Lock()
|
||||||
defer cb.mutex.Unlock()
|
defer cb.mutex.Unlock()
|
||||||
// this approach assumes that there are typically very few callbacks
|
// this approach assumes that there are typically very few callbacks
|
||||||
|
@ -38,7 +38,7 @@ func (cb *Callbacks) Add(v interface{}) int {
|
||||||
// until we find a free key like in the cgo wiki page.
|
// until we find a free key like in the cgo wiki page.
|
||||||
// If this code ever becomes a hot path there's surely plenty of room
|
// If this code ever becomes a hot path there's surely plenty of room
|
||||||
// for optimization in the future :-)
|
// for optimization in the future :-)
|
||||||
index := len(cb.cmap) + 1
|
index := uintptr(len(cb.cmap) + 1)
|
||||||
for {
|
for {
|
||||||
if _, found := cb.cmap[index]; !found {
|
if _, found := cb.cmap[index]; !found {
|
||||||
break
|
break
|
||||||
|
@ -50,14 +50,14 @@ func (cb *Callbacks) Add(v interface{}) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove a callback/object given it's index.
|
// Remove a callback/object given it's index.
|
||||||
func (cb *Callbacks) Remove(index int) {
|
func (cb *Callbacks) Remove(index uintptr) {
|
||||||
cb.mutex.Lock()
|
cb.mutex.Lock()
|
||||||
defer cb.mutex.Unlock()
|
defer cb.mutex.Unlock()
|
||||||
delete(cb.cmap, index)
|
delete(cb.cmap, index)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lookup returns a mapped callback/object given an index.
|
// Lookup returns a mapped callback/object given an index.
|
||||||
func (cb *Callbacks) Lookup(index int) interface{} {
|
func (cb *Callbacks) Lookup(index uintptr) interface{} {
|
||||||
cb.mutex.RLock()
|
cb.mutex.RLock()
|
||||||
defer cb.mutex.RUnlock()
|
defer cb.mutex.RUnlock()
|
||||||
return cb.cmap[index]
|
return cb.cmap[index]
|
||||||
|
|
|
@ -126,7 +126,7 @@ func (image *Image) DiffIterate(config DiffIterateConfig) error {
|
||||||
func diffIterateCallback(
|
func diffIterateCallback(
|
||||||
offset C.uint64_t, length C.size_t, exists C.int, index unsafe.Pointer) C.int {
|
offset C.uint64_t, length C.size_t, exists C.int, index unsafe.Pointer) C.int {
|
||||||
|
|
||||||
v := diffIterateCallbacks.Lookup(int(uintptr(index)))
|
v := diffIterateCallbacks.Lookup(uintptr(index))
|
||||||
config := v.(DiffIterateConfig)
|
config := v.(DiffIterateConfig)
|
||||||
return C.int(config.Callback(
|
return C.int(config.Callback(
|
||||||
uint64(offset), uint64(length), int(exists), config.Data))
|
uint64(offset), uint64(length), int(exists), config.Data))
|
||||||
|
|
|
@ -95,7 +95,7 @@ type Watch struct {
|
||||||
image *Image
|
image *Image
|
||||||
wcc watchCallbackCtx
|
wcc watchCallbackCtx
|
||||||
handle C.uint64_t
|
handle C.uint64_t
|
||||||
cbIndex int
|
cbIndex uintptr
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateWatch updates the image object to watch metadata changes to the
|
// UpdateWatch updates the image object to watch metadata changes to the
|
||||||
|
@ -146,7 +146,7 @@ func (w *Watch) Unwatch() error {
|
||||||
|
|
||||||
//export imageWatchCallback
|
//export imageWatchCallback
|
||||||
func imageWatchCallback(index unsafe.Pointer) {
|
func imageWatchCallback(index unsafe.Pointer) {
|
||||||
v := watchCallbacks.Lookup(int(uintptr(index)))
|
v := watchCallbacks.Lookup(uintptr(index))
|
||||||
wcc := v.(watchCallbackCtx)
|
wcc := v.(watchCallbackCtx)
|
||||||
wcc.callback(wcc.data)
|
wcc.callback(wcc.data)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue