Debian Codegraph @

internal/wasm/memory/mem_other.go

raw symbols SCIP
1 //go:build !unix && !windows
2
3 package memory
4
5 import "sync"
6
7 // Memory implements LinearMemory for platforms without mmap support.
8 // It uses a pre-allocated buffer to ensure memory never moves,
9 // which is critical for shared wasm memory.
10 type Memory struct {
11 backing []byte // Full preallocated capacity
12 Buf []byte // Current logical view
13 Max int64
14 mu sync.Mutex
15 }
16
17 func (m *Memory) Slice() *[]byte {
18 return &m.Buf
19 }
20
21 func (m *Memory) Grow(delta, _ int64) int64 {
22 m.mu.Lock()
23 defer m.mu.Unlock()
24
25 sz := len(m.Buf)
26 old := int64(sz >> 16)
27 if delta == 0 {
28 return old
29 }
30
31 new := old + delta
32 newBytes := int(new) << 16
33 if new > m.Max || newBytes < 0 {
34 return -1
35 }
36
37 // If this is the first call, allocate the full backing buffer
38 if m.backing == nil {
39 maxBytes := int(m.Max) << 16
40 if maxBytes < 0 {
41 return -1
42 }
43 m.backing = make([]byte, maxBytes)
44 m.Buf = m.backing[:0]
45 }
46
47 // Grow by slicing the pre-allocated backing buffer (memory doesn't move)
48 if newBytes > len(m.backing) {
49 return -1
50 }
51 m.Buf = m.backing[:newBytes]
52 return old
53 }
54
55 func (m *Memory) Close() error {
56 m.mu.Lock()
57 defer m.mu.Unlock()
58 m.backing = nil
59 m.Buf = nil
60 return nil
61 }