Wasmtime
memory.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_MEMORY_HH
6#define WASMTIME_MEMORY_HH
7
8#include <wasmtime/error.hh>
9#include <wasmtime/memory.h>
10#include <wasmtime/span.hh>
11#include <wasmtime/store.hh>
13
14namespace wasmtime {
15
27class Memory {
28 friend class Instance;
29 wasmtime_memory_t memory;
30
31public:
33 Memory(wasmtime_memory_t memory) : memory(memory) {}
34
37 wasmtime_memory_t memory;
38 auto *error = wasmtime_memory_new(cx.ptr, ty.ptr.get(), &memory);
39 if (error != nullptr) {
40 return Error(error);
41 }
42 return Memory(memory);
43 }
44
47 return wasmtime_memory_type(cx.ptr, &memory);
48 }
49
51 uint64_t size(Store::Context cx) const {
52 return wasmtime_memory_size(cx.ptr, &memory);
53 }
54
61 auto *base = wasmtime_memory_data(cx.ptr, &memory);
62 auto size = wasmtime_memory_data_size(cx.ptr, &memory);
63 return {base, size};
64 }
65
70 Result<uint64_t> grow(Store::Context cx, uint64_t delta) const {
71 uint64_t prev = 0;
72 auto *error = wasmtime_memory_grow(cx.ptr, &memory, delta, &prev);
73 if (error != nullptr) {
74 return Error(error);
75 }
76 return prev;
77 }
78
80 const wasmtime_memory_t &capi() const { return memory; }
81};
82
83} // namespace wasmtime
84
85#endif // WASMTIME_MEMORY_HH
Errors coming from Wasmtime.
Definition: error.hh:25
A WebAssembly instance.
Definition: instance.hh:31
Type information about a WebAssembly linear memory.
Definition: types/memory.hh:18
A WebAssembly linear memory.
Definition: memory.hh:27
Memory(wasmtime_memory_t memory)
Creates a new memory from the raw underlying C API representation.
Definition: memory.hh:33
static Result< Memory > create(Store::Context cx, const MemoryType &ty)
Creates a new host-defined memory with the type specified.
Definition: memory.hh:36
const wasmtime_memory_t & capi() const
Returns the raw underlying C API memory this is using.
Definition: memory.hh:80
Span< uint8_t > data(Store::Context cx) const
Definition: memory.hh:60
MemoryType type(Store::Context cx) const
Returns the type of this memory.
Definition: memory.hh:46
uint64_t size(Store::Context cx) const
Returns the size, in WebAssembly pages, of this memory.
Definition: memory.hh:51
Result< uint64_t > grow(Store::Context cx, uint64_t delta) const
Definition: memory.hh:70
Fallible result type used for Wasmtime.
Definition: error.hh:82
Span class used when c++20 is not available.
Definition: span.hh:47
An interior pointer into a Store.
Definition: store.hh:60
uint8_t * wasmtime_memory_data(const wasmtime_context_t *store, const wasmtime_memory_t *memory)
Returns the base pointer in memory where the linear memory starts.
wasmtime_error_t * wasmtime_memory_grow(wasmtime_context_t *store, const wasmtime_memory_t *memory, uint64_t delta, uint64_t *prev_size)
Attempts to grow the specified memory by delta pages.
wasmtime_error_t * wasmtime_memory_new(wasmtime_context_t *store, const wasm_memorytype_t *ty, wasmtime_memory_t *ret)
Creates a new WebAssembly linear memory.
size_t wasmtime_memory_data_size(const wasmtime_context_t *store, const wasmtime_memory_t *memory)
Returns the byte length of this linear memory.
uint64_t wasmtime_memory_size(const wasmtime_context_t *store, const wasmtime_memory_t *memory)
Returns the length, in WebAssembly pages, of this linear memory.
wasm_memorytype_t * wasmtime_memory_type(const wasmtime_context_t *store, const wasmtime_memory_t *memory)
Returns the type of the memory specified.
Representation of a memory in Wasmtime.
Definition: extern.h:57