pool.go (745B)
1 package lib9p 2 3 import ( 4 "fmt" 5 6 ) 7 8 type FidPool struct { 9 m map[uint32]*Fid 10 } 11 12 func allocFidPool() *FidPool { 13 f := new(FidPool) 14 f.m = make(map[uint32]*Fid) 15 return f 16 } 17 18 func (pool *FidPool) lookup(fid uint32) (*Fid, bool) { 19 f, ok := pool.m[fid] 20 return f, ok 21 } 22 23 func (pool *FidPool) alloc(fid uint32) (*Fid, error) { 24 if _, ok := pool.m[fid]; ok { 25 return nil, fmt.Errorf("fid already in use.") 26 } 27 f := newFid(fid) 28 pool.m[fid] = f 29 return f, nil 30 } 31 32 func (pool *FidPool) delete(fid uint32) *Fid { 33 f, ok := pool.lookup(fid) 34 if !ok { 35 return nil 36 } 37 delete(pool.m, fid) 38 return f 39 } 40 41 func (pool *FidPool) String() string { 42 s := "{" 43 for fnum, fstruct := range pool.m { 44 s += fmt.Sprintf(" [%d]%v", fnum, fstruct) 45 } 46 s += "}" 47 return s 48 }