commit 0ae61060daf180ba25305eea7b3cc1edf5075c29
parent d30dbb4f7fa09e657772ecd84423b33968584f7c
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sat, 4 Nov 2023 08:31:56 +0900
modify semFS to use map instead of slice
Diffstat:
2 files changed, 7 insertions(+), 5 deletions(-)
diff --git a/cmd/semfs/fs.go b/cmd/semfs/fs.go
@@ -14,7 +14,8 @@ import (
)
type semFS struct {
- semfiles []*semFile
+ semfiles map[int]*semFile
+ lastpath int
}
func (fsys *semFS) OpenFile(name string, omode lib9p.OpenMode, perm fs.FileMode) (lib9p.File, error) {
@@ -62,11 +63,12 @@ func (root *semFS) Create(name string, uid string, mode lib9p.OpenMode, perm lib
fs: root,
name: name,
sem: 0,
- path: uint64(len(root.semfiles) + 1),
+ path: uint64(root.lastpath+1),
cancel: cancel,
}
newfile.start(ctx)
- root.semfiles = append(root.semfiles, newfile)
+ root.semfiles[root.lastpath+1] = newfile
+ root.lastpath++
return newfile, nil
}
@@ -175,6 +177,6 @@ func (f *semFile) Remove() error {
f.cancel()
close(f.rchan)
close(f.wchan)
- f.fs.semfiles[f.path-1] = nil
+ delete(f.fs.semfiles, int(f.path))
return nil
}
diff --git a/cmd/semfs/main.go b/cmd/semfs/main.go
@@ -23,7 +23,7 @@ func main() {
if err != nil {
log.Fatalf("listen tcp: %v", err)
}
- fsys := new(semFS)
+ fsys := &semFS{semfiles: make(map[int]*semFile)}
for {
conn, err := listener.Accept()
if err != nil {