47 lines
869 B
Go
47 lines
869 B
Go
|
package main
|
||
|
|
||
|
import "C"
|
||
|
import (
|
||
|
"errors"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
type ObjectReference int64
|
||
|
|
||
|
var NullObjectReference error = errors.New("Null object reference")
|
||
|
|
||
|
// GoMemoryStore is a int->interface storage structure so that Go pointers are
|
||
|
// never exposed to C code.
|
||
|
type GoMemoryStore struct {
|
||
|
mtx sync.RWMutex
|
||
|
items map[int64]interface{}
|
||
|
next int64
|
||
|
}
|
||
|
|
||
|
func (this *GoMemoryStore) Put(itm interface{}) ObjectReference {
|
||
|
this.mtx.Lock()
|
||
|
defer this.mtx.Unlock()
|
||
|
|
||
|
key := this.next
|
||
|
this.items[key] = itm
|
||
|
this.next++
|
||
|
return ObjectReference(key)
|
||
|
}
|
||
|
|
||
|
func (this *GoMemoryStore) Get(i ObjectReference) (interface{}, bool) {
|
||
|
this.mtx.RLock()
|
||
|
defer this.mtx.RUnlock()
|
||
|
|
||
|
ret, ok := this.items[int64(i)]
|
||
|
return ret, ok
|
||
|
}
|
||
|
|
||
|
func (this *GoMemoryStore) Delete(i ObjectReference) {
|
||
|
this.mtx.Lock()
|
||
|
defer this.mtx.Unlock()
|
||
|
|
||
|
delete(this.items, int64(i))
|
||
|
}
|
||
|
|
||
|
var gms GoMemoryStore
|