Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions internal/hybrid_cache/hybrid_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type HybridCache struct {
memoryOffset uint64
backingStore BackingStore
backingOffset uint64
cleanup runtime.Cleanup
}

// HybridCache本身是一个大的Block,支持分块成多个小的Block
Expand Down Expand Up @@ -99,11 +100,15 @@ func (hc *HybridCache) initFileCache() error {
if err != nil {
return err
}
hc.cleanup = runtime.AddCleanup(hc, func(file BackingStore) {
_ = file.Close()
}, file)
hc.backingStore = file
return nil
}

func (hc *HybridCache) Close() error {
hc.cleanup.Stop()
var err error
if hc.memoryStore != nil {
err = hc.memoryStore.Free()
Expand Down Expand Up @@ -228,12 +233,6 @@ func NewHybridCache(blockSize, maxMemorySize uint64) (hc *HybridCache, err error
return nil, errors.Join(err, err2)
}
}
runtime.SetFinalizer(hc, func(hc *HybridCache) {
if hc.backingStore != nil {
_ = hc.backingStore.Close()
hc.backingStore = nil
}
})
return hc, nil
}

Expand Down
14 changes: 10 additions & 4 deletions internal/mem/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,16 @@ func NewGuardedMemory(cap, max uint64) (m LinearMemory, err error) {
if s, ok := m.(interface{ SetGrowCheck(GrowCheck) }); ok {
s.SetGrowCheck(MemoryGrowCheck)
}
gm := &guardedMemory{m}
runtime.SetFinalizer(gm, func(gm *guardedMemory) {
gm.Free()
})
gm := &guardedMemory{LinearMemory: m}
gm.cleanup = runtime.AddCleanup(gm, func(m LinearMemory) {
m.Free()
}, m)
return gm, nil
}

type guardedMemory struct {
LinearMemory
cleanup runtime.Cleanup
}

func (s *guardedMemory) Reallocate(size uint64) (all []byte, err error) {
Expand All @@ -77,3 +78,8 @@ func (s *guardedMemory) Reallocate(size uint64) (all []byte, err error) {
}()
return s.LinearMemory.Reallocate(size)
}

func (s *guardedMemory) Free() error {
s.cleanup.Stop()
return s.LinearMemory.Free()
}
Loading