rebase: update vendored go-ceph to v0.6

Closes: #1547
Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos
2020-10-13 17:22:31 +02:00
committed by mergify[bot]
parent 268c0f1965
commit 29c78f97c0
12 changed files with 286 additions and 40 deletions

View File

@ -19,17 +19,17 @@ import (
// to control and validate what "callbacks" get used.
type Callbacks struct {
mutex sync.RWMutex
cmap map[int]interface{}
cmap map[uintptr]interface{}
}
// New returns a new callbacks tracker.
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
// for the object.
func (cb *Callbacks) Add(v interface{}) int {
func (cb *Callbacks) Add(v interface{}) uintptr {
cb.mutex.Lock()
defer cb.mutex.Unlock()
// 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.
// If this code ever becomes a hot path there's surely plenty of room
// for optimization in the future :-)
index := len(cb.cmap) + 1
index := uintptr(len(cb.cmap) + 1)
for {
if _, found := cb.cmap[index]; !found {
break
@ -50,14 +50,14 @@ func (cb *Callbacks) Add(v interface{}) int {
}
// Remove a callback/object given it's index.
func (cb *Callbacks) Remove(index int) {
func (cb *Callbacks) Remove(index uintptr) {
cb.mutex.Lock()
defer cb.mutex.Unlock()
delete(cb.cmap, 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()
defer cb.mutex.RUnlock()
return cb.cmap[index]

14
vendor/github.com/ceph/go-ceph/internal/cutil/cutil.go generated vendored Normal file
View File

@ -0,0 +1,14 @@
package cutil
import "unsafe"
// VoidPtr casts a uintptr value to an unsafe.Pointer value in order to use it
// directly as a void* argument in a C function call.
// CAUTION: NEVER store the result in a variable, or the Go GC could panic.
func VoidPtr(i uintptr) unsafe.Pointer {
var nullPtr unsafe.Pointer
// It's not possible to cast uintptr directly to unsafe.Pointer. Therefore we
// cast a null pointer to uintptr and apply pointer arithmetic on it, which
// allows us to cast it back to unsafe.Pointer.
return unsafe.Pointer(uintptr(nullPtr) + i)
}

78
vendor/github.com/ceph/go-ceph/internal/cutil/iovec.go generated vendored Normal file
View File

@ -0,0 +1,78 @@
package cutil
/*
#include <stdlib.h>
#include <sys/uio.h>
*/
import "C"
import (
"unsafe"
)
var iovecSize uintptr
// StructIovecPtr is an unsafe pointer wrapping C's `*struct iovec`.
type StructIovecPtr unsafe.Pointer
// Iovec helps manage struct iovec arrays needed by some C functions.
type Iovec struct {
// cvec represents an array of struct iovec C memory
cvec unsafe.Pointer
// length of the array (in elements)
length int
}
// NewIovec creates an Iovec, and underlying C memory, of the specified size.
func NewIovec(l int) *Iovec {
r := &Iovec{
cvec: C.malloc(C.size_t(l) * C.size_t(iovecSize)),
length: l,
}
return r
}
// ByteSlicesToIovec takes a slice of byte slices and returns a new iovec that
// maps the slice data to struct iovec entries.
func ByteSlicesToIovec(data [][]byte) *Iovec {
iov := NewIovec(len(data))
for i := range data {
iov.Set(i, data[i])
}
return iov
}
// Pointer returns a StructIovecPtr that represents the C memory of the
// underlying array.
func (v *Iovec) Pointer() StructIovecPtr {
return StructIovecPtr(unsafe.Pointer(v.cvec))
}
// Len returns the number of entries in the Iovec.
func (v *Iovec) Len() int {
return v.length
}
// Free the C memory in the Iovec.
func (v *Iovec) Free() {
if v.cvec != nil {
C.free(v.cvec)
v.cvec = nil
v.length = 0
}
}
// Set will map the memory of the given byte slice to the iovec at the
// specified position.
func (v *Iovec) Set(i int, buf []byte) {
offset := uintptr(i) * iovecSize
iov := (*C.struct_iovec)(unsafe.Pointer(
uintptr(unsafe.Pointer(v.cvec)) + offset))
iov.iov_base = unsafe.Pointer(&buf[0])
iov.iov_len = C.size_t(len(buf))
}
func init() {
var iovec C.struct_iovec
iovecSize = unsafe.Sizeof(iovec)
}