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