github.com/tetratelabs/wazero@v1.7.1/experimental/memory.go (about) 1 package experimental 2 3 import ( 4 "context" 5 6 "github.com/tetratelabs/wazero/internal/expctxkeys" 7 ) 8 9 // MemoryAllocator is a memory allocation hook, 10 // invoked to create a LinearMemory. 11 type MemoryAllocator interface { 12 // Allocate should create a new LinearMemory with the given specification: 13 // cap is the suggested initial capacity for the backing []byte, 14 // and max the maximum length that will ever be requested. 15 // 16 // Notes: 17 // - To back a shared memory, the address of the backing []byte cannot 18 // change. This is checked at runtime. Implementations should document 19 // if the returned LinearMemory meets this requirement. 20 Allocate(cap, max uint64) LinearMemory 21 } 22 23 // MemoryAllocatorFunc is a convenience for defining inlining a MemoryAllocator. 24 type MemoryAllocatorFunc func(cap, max uint64) LinearMemory 25 26 // Allocate implements MemoryAllocator.Allocate. 27 func (f MemoryAllocatorFunc) Allocate(cap, max uint64) LinearMemory { 28 return f(cap, max) 29 } 30 31 // LinearMemory is an expandable []byte that backs a Wasm linear memory. 32 type LinearMemory interface { 33 // Reallocates the linear memory to size bytes in length. 34 // 35 // Notes: 36 // - To back a shared memory, Reallocate can't change the address of the 37 // backing []byte (only its length/capacity may change). 38 Reallocate(size uint64) []byte 39 // Free the backing memory buffer. 40 Free() 41 } 42 43 // WithMemoryAllocator registers the given MemoryAllocator into the given 44 // context.Context. 45 func WithMemoryAllocator(ctx context.Context, allocator MemoryAllocator) context.Context { 46 if allocator != nil { 47 return context.WithValue(ctx, expctxkeys.MemoryAllocatorKey{}, allocator) 48 } 49 return ctx 50 }