diff options
Diffstat (limited to 'vendor/github.com/libgit2/git2go/handles.go')
-rw-r--r-- | vendor/github.com/libgit2/git2go/handles.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/vendor/github.com/libgit2/git2go/handles.go b/vendor/github.com/libgit2/git2go/handles.go new file mode 100644 index 000000000..d27d3c353 --- /dev/null +++ b/vendor/github.com/libgit2/git2go/handles.go @@ -0,0 +1,57 @@ +package git + +/* +#include <stdlib.h> +*/ +import "C" +import ( + "fmt" + "sync" + "unsafe" +) + +type HandleList struct { + sync.RWMutex + // stores the Go pointers + handles map[unsafe.Pointer]interface{} +} + +func NewHandleList() *HandleList { + return &HandleList{ + handles: make(map[unsafe.Pointer]interface{}), + } +} + +// Track adds the given pointer to the list of pointers to track and +// returns a pointer value which can be passed to C as an opaque +// pointer. +func (v *HandleList) Track(pointer interface{}) unsafe.Pointer { + handle := C.malloc(1) + + v.Lock() + v.handles[handle] = pointer + v.Unlock() + + return handle +} + +// Untrack stops tracking the pointer given by the handle +func (v *HandleList) Untrack(handle unsafe.Pointer) { + v.Lock() + delete(v.handles, handle) + C.free(handle) + v.Unlock() +} + +// Get retrieves the pointer from the given handle +func (v *HandleList) Get(handle unsafe.Pointer) interface{} { + v.RLock() + defer v.RUnlock() + + ptr, ok := v.handles[handle] + if !ok { + panic(fmt.Sprintf("invalid pointer handle: %p", handle)) + } + + return ptr +} |