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

Use pointer based handles for files too.

parent d415f3e7
...@@ -154,7 +154,7 @@ type FileSystemConnector struct { ...@@ -154,7 +154,7 @@ type FileSystemConnector struct {
//////////////// ////////////////
// Protects the inode hashmap, its contents and the nextFreeInode counter. // Protects the inodeMap and each node's Children map.
lock sync.RWMutex lock sync.RWMutex
// Invariants: see the verify() method. // Invariants: see the verify() method.
...@@ -163,8 +163,11 @@ type FileSystemConnector struct { ...@@ -163,8 +163,11 @@ type FileSystemConnector struct {
// Open files/directories. // Open files/directories.
fileLock sync.RWMutex fileLock sync.RWMutex
openFiles map[uint64]interface{} openFiles map[uint64]*interfaceBridge
nextFreeHandle uint64 }
type interfaceBridge struct {
Iface interface{}
} }
func (me *FileSystemConnector) DebugString() string { func (me *FileSystemConnector) DebugString() string {
...@@ -183,41 +186,41 @@ func (me *FileSystemConnector) DebugString() string { ...@@ -183,41 +186,41 @@ func (me *FileSystemConnector) DebugString() string {
func (me *FileSystemConnector) unregisterFile(node *inode, handle uint64) interface{} { func (me *FileSystemConnector) unregisterFile(node *inode, handle uint64) interface{} {
me.fileLock.Lock() me.fileLock.Lock()
defer me.fileLock.Unlock() defer me.fileLock.Unlock()
f, ok := me.openFiles[handle] b, ok := me.openFiles[handle]
if !ok { if !ok {
panic("invalid handle") panic("invalid handle")
} }
me.openFiles[handle] = nil, false me.openFiles[handle] = nil, false
node.OpenCount-- node.OpenCount--
return f return b.Iface
} }
func (me *FileSystemConnector) registerFile(node *inode, f interface{}) uint64 { func (me *FileSystemConnector) registerFile(node *inode, f interface{}) uint64 {
me.fileLock.Lock() me.fileLock.Lock()
defer me.fileLock.Unlock() defer me.fileLock.Unlock()
h := me.nextFreeHandle b := &interfaceBridge{
me.nextFreeHandle++ Iface: f,
}
h := uint64(uintptr(unsafe.Pointer(b)))
_, ok := me.openFiles[h] _, ok := me.openFiles[h]
if ok { if ok {
panic("handle counter wrapped") panic("handle counter wrapped")
} }
node.OpenCount++ node.OpenCount++
me.openFiles[h] = f me.openFiles[h] = b
return h return h
} }
func (me *FileSystemConnector) getDir(h uint64) RawDir { func (me *FileSystemConnector) getDir(h uint64) RawDir {
me.fileLock.RLock() b := (*interfaceBridge)(unsafe.Pointer(uintptr(h)))
defer me.fileLock.RUnlock() return b.Iface.(RawDir)
return me.openFiles[h].(RawDir)
} }
func (me *FileSystemConnector) getFile(h uint64) File { func (me *FileSystemConnector) getFile(h uint64) File {
me.fileLock.RLock() b := (*interfaceBridge)(unsafe.Pointer(uintptr(h)))
defer me.fileLock.RUnlock() return b.Iface.(File)
return me.openFiles[h].(File)
} }
func (me *FileSystemConnector) verify() { func (me *FileSystemConnector) verify() {
...@@ -373,7 +376,7 @@ func (me *FileSystemConnector) findInode(fullPath string) *inode { ...@@ -373,7 +376,7 @@ func (me *FileSystemConnector) findInode(fullPath string) *inode {
func EmptyFileSystemConnector() (out *FileSystemConnector) { func EmptyFileSystemConnector() (out *FileSystemConnector) {
out = new(FileSystemConnector) out = new(FileSystemConnector)
out.inodeMap = make(map[uint64]*inode) out.inodeMap = make(map[uint64]*inode)
out.openFiles = make(map[uint64]interface{}) out.openFiles = make(map[uint64]*interfaceBridge)
rootData := out.newInode(true) rootData := out.newInode(true)
rootData.Type = ModeToType(S_IFDIR) rootData.Type = ModeToType(S_IFDIR)
......
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