Documentation ¶
Overview ¶
Package mapper makes it easy to exchange Go values for a pointer that can be passed to C APIs, and then retrieve the original Go value in a Go callback.
Index ¶
- type Key
- type Mapper
- func (mapper *Mapper) Clear()
- func (mapper *Mapper) Delete(key Key)
- func (mapper *Mapper) Get(key Key) (goValue interface{})
- func (mapper *Mapper) GetPtr(ptr unsafe.Pointer) (goValue interface{})
- func (mapper *Mapper) MapPair(key Key, goValue interface{})
- func (mapper *Mapper) MapPtrPair(ptr unsafe.Pointer, goValue interface{}) Key
- func (mapper *Mapper) MapValue(goValue interface{}) Key
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Key ¶
type Key struct {
// contains filtered or unexported fields
}
Key is an opaque token used to map onto Go values.
func KeyFromPtr ¶
KeyFromPtr converts a cgo pointer to a Key.
The key can be any unique pointer, but it is recommended that it be a value obtained from outside the view of the Go GC, in case the GC moves that memory around. Typically key is a malloc-based pointer obtained from cgo.
We require the key to be at least 2-bytes aligned: that is, the lower bit must be zero, which is a reasonable assumption for pointers returned via malloc.
type Mapper ¶
type Mapper struct {
// contains filtered or unexported fields
}
Mapper maps between Key and Go values.
var G Mapper
G is the global mapper... for users who don't care about lock contention. For those that do, it is recommended to use a separate Mapper instance.
func (*Mapper) Delete ¶
Delete an existing mapping via the key.
func (*Mapper) Get ¶
Get retrieves the Go value from key.
func (*Mapper) GetPtr ¶
GetPtr calls Get after first converting ptr to a Key.
func (*Mapper) MapPair ¶
MapPair creates a mapping between the provided Key and Go value.
func (*Mapper) MapPtrPair ¶
MapPtrPair is like MapPair, but maps from a cgo pointer, and returns the associated Key. This method is a convenience wrapper around KeyFromPtr and MapPair.
func (*Mapper) MapValue ¶
MapValue maps a new Key to the given Go value, and returns the Key.
The key here is a sizeof(pointer)/2 atomic, that is simply incremented by two on each call. On a 64-bit platform, this key-space is so large that is will unlikely ever run out during the lifetime of a program... but if it does, we panic. To avoid running out of space on a 32-bit platform (where 2,147,483,648 mappings are possible), use MapPtrPair instead.