From 51cfb4bf54a572e64f55a7ecfa7d47d0828f9505 Mon Sep 17 00:00:00 2001 From: bw Date: Mon, 28 Nov 2022 08:08:27 -0500 Subject: [PATCH] cgofuse: add file table realloc/shrinking method --- internal/filesystem/cgofuse/table.go | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/internal/filesystem/cgofuse/table.go b/internal/filesystem/cgofuse/table.go index 5dafcae4..54b6fcb0 100644 --- a/internal/filesystem/cgofuse/table.go +++ b/internal/filesystem/cgofuse/table.go @@ -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()