Wasmtime
types/memory.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_TYPES_MEMORY_HH
6#define WASMTIME_TYPES_MEMORY_HH
7
8#include <memory>
9#include <optional>
10#include <wasm.h>
11#include <wasmtime/error.hh>
12#include <wasmtime/memory.h>
13
14namespace wasmtime {
15
20 friend class Memory;
21
22 struct deleter {
23 void operator()(wasm_memorytype_t *p) const {
24 assert(p != nullptr);
26 }
27 };
28
29 std::unique_ptr<wasm_memorytype_t, deleter> ptr;
30
31public:
34 class Ref {
35 friend class MemoryType;
36
37 const wasm_memorytype_t *ptr;
38
39 private:
40 Ref() : ptr(nullptr) {}
41
42 public:
44 Ref(const wasm_memorytype_t *ptr) : ptr(ptr) {}
46 Ref(const MemoryType &ty) : Ref(ty.ptr.get()) {}
47
49 uint64_t min() const { return wasmtime_memorytype_minimum(ptr); }
50
53 std::optional<uint64_t> max() const {
54 uint64_t max = 0;
55 auto present = wasmtime_memorytype_maximum(ptr, &max);
56 if (present) {
57 return max;
58 }
59 return std::nullopt;
60 }
61
63 bool is_64() const { return wasmtime_memorytype_is64(ptr); }
64
66 bool is_shared() const { return wasmtime_memorytype_isshared(ptr); }
67
69 uint64_t page_size() const { return wasmtime_memorytype_page_size(ptr); }
70
72 uint8_t page_size_log2() const {
74 }
75 };
76
77private:
78 Ref ref;
79 MemoryType(wasm_memorytype_t *ptr) : ptr(ptr), ref(ptr) {}
80
81public:
86 static Result<MemoryType> create(uint32_t min,
87 std::optional<uint32_t> max = std::nullopt) {
88 return Builder().min(min).max(max).build();
89 }
90
93 create64(uint64_t min, std::optional<uint64_t> max = std::nullopt) {
94 return Builder().min(min).max(max).memory64(true).build();
95 }
96
101 MemoryType(const MemoryType &other)
102 : MemoryType(wasm_memorytype_copy(other.ptr.get())) {}
105 ptr.reset(wasm_memorytype_copy(other.ptr.get()));
106 ref = ptr.get();
107 return *this;
108 }
109 ~MemoryType() = default;
111 MemoryType(MemoryType &&other) = default;
113 MemoryType &operator=(MemoryType &&other) = default;
114
117 Ref *operator->() { return &ref; }
120 Ref *operator*() { return &ref; }
121
123 class Builder {
124 uint64_t _min;
125 std::optional<uint64_t> _max;
126 bool _memory64;
127 bool _shared;
128 uint8_t _page_size_log2;
129
130 public:
133 : _min(0), _memory64(false), _shared(false), _page_size_log2(16) {}
134
136 Builder &min(uint64_t min) {
137 _min = min;
138 return *this;
139 }
140
142 Builder &max(std::optional<uint64_t> max) {
143 _max = max;
144 return *this;
145 }
146
148 Builder &memory64(bool enable) {
149 _memory64 = enable;
150 return *this;
151 }
152
154 Builder &shared(bool enable) {
155 _shared = enable;
156 return *this;
157 }
158
171 _page_size_log2 = page_size_log2;
172 return *this;
173 }
174
177 wasm_memorytype_t *p = nullptr;
178 auto *err = wasmtime_memorytype_new(
179 _min, _max.has_value(), _max.has_value() ? *_max : 0, _memory64,
180 _shared, _page_size_log2, &p);
181 if (err != nullptr) {
182 return Error(err);
183 }
184 assert(p != nullptr);
185 return MemoryType(p);
186 }
187 };
188};
189
190}; // namespace wasmtime
191
192#endif // WASMTIME_TYPES_MEMORY_HH
Errors coming from Wasmtime.
Definition: error.hh:26
Helper class to build a MemoryType.
Definition: types/memory.hh:123
Builder & max(std::optional< uint64_t > max)
Configure the maximal size, in pages, of linear memory.
Definition: types/memory.hh:142
Builder & page_size_log2(uint8_t page_size_log2)
Configure the page size (in bytes) of this memory type, via its log2 value.
Definition: types/memory.hh:170
Result< MemoryType > build() const
Construct the final MemoryType value.
Definition: types/memory.hh:176
Builder & memory64(bool enable)
Configure whether this is a 64-bit linear memory.
Definition: types/memory.hh:148
Builder & min(uint64_t min)
Configure the minimum size, in pages, of linear memory.
Definition: types/memory.hh:136
Builder()
Default constructor for a memory type with 0 initial size.
Definition: types/memory.hh:132
Builder & shared(bool enable)
Configure whether this is a shared linear memory.
Definition: types/memory.hh:154
Non-owning reference to a MemoryType, must not be used after the original owner has been deleted.
Definition: types/memory.hh:34
Ref(const wasm_memorytype_t *ptr)
Creates a reference from the raw C API representation.
Definition: types/memory.hh:44
bool is_64() const
Returns whether or not this is a 64-bit memory type.
Definition: types/memory.hh:63
bool is_shared() const
Returns whether or not this is a shared memory type.
Definition: types/memory.hh:66
std::optional< uint64_t > max() const
Definition: types/memory.hh:53
uint64_t page_size() const
Returns the memory's page size, in bytes.
Definition: types/memory.hh:69
uint64_t min() const
Returns the minimum size, in WebAssembly pages, of this memory.
Definition: types/memory.hh:49
Ref(const MemoryType &ty)
Creates a reference from an original MemoryType.
Definition: types/memory.hh:46
uint8_t page_size_log2() const
Returns the log2 of the memory's page size, in bytes.
Definition: types/memory.hh:72
Type information about a WebAssembly linear memory.
Definition: types/memory.hh:19
Ref * operator*()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: types/memory.hh:120
MemoryType & operator=(const MemoryType &other)
Copies the provided type into a new type.
Definition: types/memory.hh:104
MemoryType(const MemoryType &other)
Copies the provided type into a new type.
Definition: types/memory.hh:101
MemoryType(Ref other)
Definition: types/memory.hh:99
MemoryType & operator=(MemoryType &&other)=default
Moves the type information from another type into this one.
static Result< MemoryType > create64(uint64_t min, std::optional< uint64_t > max=std::nullopt)
Same as MemoryType::create, except creates a 64-bit memory.
Definition: types/memory.hh:93
static Result< MemoryType > create(uint32_t min, std::optional< uint32_t > max=std::nullopt)
Definition: types/memory.hh:86
MemoryType(MemoryType &&other)=default
Moves the type information from another type into this one.
Ref * operator->()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: types/memory.hh:117
A WebAssembly linear memory.
Definition: memory.hh:27
Fallible result type used for Wasmtime.
Definition: error.hh:70
WASM_API_EXTERN bool wasmtime_memorytype_maximum(const wasm_memorytype_t *ty, uint64_t *max)
Returns the maximum size, in pages, of the specified memory type.
WASM_API_EXTERN uint64_t wasmtime_memorytype_minimum(const wasm_memorytype_t *ty)
Returns the minimum size, in pages, of the specified memory type.
WASM_API_EXTERN wasmtime_error_t * wasmtime_memorytype_new(uint64_t min, bool max_present, uint64_t max, bool is_64, bool shared, uint8_t page_size_log2, wasm_memorytype_t **ret)
Creates a new memory type from the specified parameters.
WASM_API_EXTERN bool wasmtime_memorytype_is64(const wasm_memorytype_t *ty)
Returns whether this type of memory represents a 64-bit memory.
WASM_API_EXTERN bool wasmtime_memorytype_isshared(const wasm_memorytype_t *ty)
Returns whether this type of memory represents a shared memory.
WASM_API_EXTERN uint64_t wasmtime_memorytype_page_size(const wasm_memorytype_t *ty)
Returns the page size, in bytes, of this memory type.
WASM_API_EXTERN uint8_t wasmtime_memorytype_page_size_log2(const wasm_memorytype_t *ty)
Returns the log2 of this memory type's page size, in bytes.
An opaque object representing the type of a memory.
void wasm_memorytype_delete(wasm_memorytype_t *)
Deletes a type.
wasm_memorytype_t * wasm_memorytype_copy(const wasm_memorytype_t *)
Creates a new value which matches the provided one.