Skip to content

Commit

Permalink
cgofuse: add file table realloc/shrinking method
Browse files Browse the repository at this point in the history
  • Loading branch information
11bw authored and djdv committed Dec 2, 2022
1 parent f71036e commit 51cfb4b
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions internal/filesystem/cgofuse/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,38 @@ func (files handleSlice) extend() (handleSlice, error) {
return newTable, nil
}

func (files handleSlice) shrink(lowerBound int) handleSlice {
var (
emptySlots int
filesLen = len(files)
filesCap = cap(files)
)
for i := filesLen - 1; i != -1; i-- {
if files[i] != nil {
break
}
emptySlots++
}
var (
newLen = filesLen - emptySlots
bound = boundCheck(lowerBound, newLen)
)
if newLen == bound || filesCap == bound {
return nil
}
newTable := make(handleSlice, newLen, bound)
copy(newTable, files)
return newTable
}

func boundCheck(lowerBound, oldCap int) int {
newCap := lowerBound
for newCap < oldCap {
newCap *= 2
}
return newCap
}

func (ft *fileTable) add(f fs.File) (fileDescriptor, error) {
ft.Lock()
defer ft.Unlock()
Expand Down

0 comments on commit 51cfb4b

Please sign in to comment.