Commit e6fa94ed authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

Store interface{} to original object in HandleMap.

parent f7e64903
......@@ -59,7 +59,7 @@ func (me *FileSystemConnector) verify() {
func (me *FileSystemConnector) newInode(isDir bool) *Inode {
data := new(Inode)
data.nodeId = me.inodeMap.Register(&data.handled)
data.nodeId = me.inodeMap.Register(&data.handled, data)
data.connector = me
if isDir {
data.children = make(map[string]*Inode, initDirSize)
......@@ -260,7 +260,7 @@ func (me *FileSystemConnector) Mount(parent *Inode, name string, nodeFs NodeFile
node.mountFs(nodeFs, opts)
parent.addChild(name, node)
if parent.mounts == nil {
parent.mounts = make(map[string]*fileSystemMount)
}
......@@ -275,7 +275,7 @@ func (me *FileSystemConnector) Mount(parent *Inode, name string, nodeFs NodeFile
return OK
}
// Unmount() tries to unmount the given inode.
// Unmount() tries to unmount the given inode.
//
// Returns the following error codes:
//
......
......@@ -13,7 +13,7 @@ var _ = log.Println
type openedFile struct {
Handled
WithFlags
WithFlags
dir rawDir
}
......@@ -119,6 +119,6 @@ func (me *fileSystemMount) registerFileHandle(node *Inode, dir rawDir, f File, f
}
node.openFiles = append(node.openFiles, b)
handle := me.openFiles.Register(&b.Handled)
handle := me.openFiles.Register(&b.Handled, b)
return handle, b
}
......@@ -20,14 +20,14 @@ import (
//
// This structure is thread-safe.
type HandleMap interface {
Register(obj *Handled) uint64
Register(obj *Handled, asInt interface{}) uint64
Count() int
Forget(uint64) *Handled
}
// TODO - store interface{} pointer to wrapped object.
type Handled struct {
check uint32
check uint32
object interface{}
}
// 32 bits version of HandleMap
......@@ -36,7 +36,7 @@ type int32HandleMap struct {
handles map[uint32]*Handled
}
func (me *int32HandleMap) Register(obj *Handled) uint64 {
func (me *int32HandleMap) Register(obj *Handled, asInt interface{}) uint64 {
me.mutex.Lock()
defer me.mutex.Unlock()
handle := uint32(uintptr(unsafe.Pointer(obj)))
......@@ -112,7 +112,7 @@ func (me *int64HandleMap) Count() int {
return len(me.handles)
}
func (me *int64HandleMap) Register(obj *Handled) (handle uint64) {
func (me *int64HandleMap) Register(obj *Handled, asInterface interface{}) (handle uint64) {
defer me.verify()
me.mutex.Lock()
......@@ -142,6 +142,7 @@ func (me *int64HandleMap) Register(obj *Handled) (handle uint64) {
obj.check = check
}
obj.object = asInterface
me.handles[handle] = obj
return handle
}
......@@ -169,8 +170,8 @@ func DecodeHandle(handle uint64) (val *Handled) {
val = (*Handled)(unsafe.Pointer(uintptr(handle & ((1 << 32) - 1))))
}
if val.check != check {
msg := fmt.Sprintf("handle check mismatch; handle has 0x%x, object has 0x%x",
check, val.check)
msg := fmt.Sprintf("handle check mismatch; handle has 0x%x, object has 0x%x: %v",
check, val.check, val.object)
panic(msg)
}
return val
......
......@@ -26,10 +26,11 @@ func TestHandleMapDoubleRegister(t *testing.T) {
log.Println("TestDoubleRegister")
defer markSeen("already has a handle")
hm := NewHandleMap(true)
hm.Register(&Handled{})
obj := &Handled{}
hm.Register(obj, obj)
v := &Handled{}
hm.Register(v)
hm.Register(v)
hm.Register(v, v)
hm.Register(v, v)
t.Error("Double register did not panic")
}
......@@ -44,7 +45,7 @@ func TestHandleMapUnaligned(t *testing.T) {
v := (*Handled)(unsafe.Pointer(&b[1]))
defer markSeen("unaligned")
hm.Register(v)
hm.Register(v, v)
t.Error("Unaligned register did not panic")
}
......@@ -59,7 +60,7 @@ func TestHandleMapPointerLayout(t *testing.T) {
p := uintptr(bogus)
v := (*Handled)(unsafe.Pointer(p))
defer markSeen("48")
hm.Register(v)
hm.Register(v, v)
t.Error("bogus register did not panic")
}
......@@ -70,7 +71,7 @@ func TestHandleMapBasic(t *testing.T) {
}
v := new(Handled)
hm := NewHandleMap(true)
h := hm.Register(v)
h := hm.Register(v, v)
log.Printf("Got handle 0x%x", h)
if DecodeHandle(h) != v {
t.Fatal("address mismatch")
......@@ -92,7 +93,7 @@ func TestHandleMapMultiple(t *testing.T) {
hm := NewHandleMap(true)
for i := 0; i < 10; i++ {
v := &Handled{}
h := hm.Register(v)
h := hm.Register(v, v)
if DecodeHandle(h) != v {
t.Fatal("address mismatch")
}
......@@ -111,7 +112,7 @@ func TestHandleMapCheckFail(t *testing.T) {
v := new(Handled)
hm := NewHandleMap(true)
h := hm.Register(v)
h := hm.Register(v, v)
DecodeHandle(h | (uint64(1) << 63))
t.Error("Borked decode did not panic")
}
......@@ -123,7 +124,7 @@ func TestHandleMapNoCheck(t *testing.T) {
}
v := new(Handled)
hm := NewHandleMap(false)
h := hm.Register(v)
h := hm.Register(v, v)
if h > uint64(0xffffffff) {
t.Errorf("handles should in 32 bit if verification switched off: %x", h)
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment