MCPcopy Create free account
hub / github.com/OpenListTeam/OpenList / MemoryGrowCheck

Function MemoryGrowCheck

internal/mem/utils.go:16–45  ·  view source on GitHub ↗
(growSize uint64)

Source from the content-addressed store, hash-verified

14var ErrNotEnoughMemory = errors.New("not enough memory")
15
16func MemoryGrowCheck(growSize uint64) error {
17 if conf.MinFreeMemory == 0 {
18 return ErrNotEnoughMemory
19 }
20 r, err, _ := singleflight.AnyGroup.Do("MemoryGrowCheck", func() (any, error) {
21 m, err := mem.VirtualMemory()
22 if err != nil {
23 return nil, err
24 }
25 if m.Available < conf.MinFreeMemory {
26 return nil, ErrNotEnoughMemory
27 }
28 var res atomic.Uint64
29 res.Store(m.Available)
30 return &res, nil
31 })
32 if err != nil {
33 return err
34 }
35 res := r.(*atomic.Uint64)
36 for {
37 available := res.Load()
38 if available < growSize || available-growSize < conf.MinFreeMemory {
39 return ErrNotEnoughMemory
40 }
41 if res.CompareAndSwap(available, available-growSize) {
42 return nil
43 }
44 }
45}
46
47func NewGuardedMemory(cap, max uint64) (m LinearMemory, err error) {
48 if err := MemoryGrowCheck(cap); err != nil {

Callers 1

NewGuardedMemoryFunction · 0.85

Calls 3

DoMethod · 0.65
StoreMethod · 0.45
LoadMethod · 0.45

Tested by

no test coverage detected