| 1 | package allocator |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
| 6 | "github.com/tetratelabs/wazero/experimental" |
| 7 | ) |
| 8 | |
| 9 | var errInvalidReallocation = errors.New("allocator: invalid reallocation request: size exceeds reserved address space") |
| 10 | |
| 11 | // NewNonMoving returns a [experimental.MemoryAllocator] that will reserve |
| 12 | // address space up to the maximum requested by a WebAssembly module during |
| 13 | // instantiation, ensuring that the base address of memory never changes even |
| 14 | // when the module requests memory to grow - the memory is never moved. |
| 15 | // |
| 16 | // On unix or windows, this reservation will not commit memory, allowing the |
| 17 | // allocation to succeed even if the requested size is more than available memory. |
| 18 | // On other platforms, it will follow the same behavior as wazero's default for |
| 19 | // shared memory, which uses Go's make([]byte, size) to pre-allocate a slice of |
| 20 | // the requested size, which may or may not work when more than available memory |
| 21 | // depending on the machine's settings related to overcommitting memory. |
| 22 | // |
| 23 | // On unix, this internally uses Mmap while on windows it uses VirtualAlloc, so |
| 24 | // it is appropriate to use the allocator for advanced use cases such as mapping |
| 25 | // an external file into the Wasm module's memory space. |
| 26 | func NewNonMoving() experimental.MemoryAllocator { |
| 27 | return experimental.MemoryAllocatorFunc(alloc) |
| 28 | } |