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:
85 explicit MemoryType(uint32_t min) {
86 wasm_memorytype_t *p = nullptr;
87 auto *err = wasmtime_memorytype_new(min, false, 0, false, false, 16, &p);
88 assert(err == nullptr);
89 assert(p != nullptr);
90 ptr.reset(p);
91 ref.ptr = p;
92 }
93
96 MemoryType(uint32_t min, uint32_t max) {
97 wasm_memorytype_t *p = nullptr;
98 auto *err = wasmtime_memorytype_new(min, true, max, false, false, 16, &p);
99 assert(err == nullptr);
100 assert(p != nullptr);
101 ptr.reset(p);
102 ref.ptr = p;
103 }
104
106 static MemoryType New64(uint64_t min) {
107 wasm_memorytype_t *ptr = nullptr;
108 auto *err = wasmtime_memorytype_new(min, false, 0, true, false, 16, &ptr);
109 assert(err == nullptr);
110 assert(ptr != nullptr);
111 return MemoryType(ptr);
112 }
113
115 static MemoryType New64(uint64_t min, uint64_t max) {
116 wasm_memorytype_t *ptr = nullptr;
117 auto *err = wasmtime_memorytype_new(min, true, max, true, false, 16, &ptr);
118 assert(err == nullptr);
119 assert(ptr != nullptr);
120 return MemoryType(ptr);
121 }
122
127 MemoryType(const MemoryType &other)
128 : MemoryType(wasm_memorytype_copy(other.ptr.get())) {}
131 ptr.reset(wasm_memorytype_copy(other.ptr.get()));
132 return *this;
133 }
134 ~MemoryType() = default;
136 MemoryType(MemoryType &&other) = default;
138 MemoryType &operator=(MemoryType &&other) = default;
139
142 Ref *operator->() { return &ref; }
145 Ref *operator*() { return &ref; }
146
148 class Builder {
149 uint64_t _min;
150 std::optional<uint64_t> _max;
151 bool _memory64;
152 bool _shared;
153 uint8_t _page_size_log2;
154
155 public:
158 : _min(0), _memory64(false), _shared(false), _page_size_log2(16) {}
159
161 Builder &min(uint64_t min) {
162 _min = min;
163 return *this;
164 }
165
167 Builder &max(std::optional<uint64_t> max) {
168 _max = max;
169 return *this;
170 }
171
173 Builder &memory64(bool enable) {
174 _memory64 = enable;
175 return *this;
176 }
177
179 Builder &shared(bool enable) {
180 _shared = enable;
181 return *this;
182 }
183
196 _page_size_log2 = page_size_log2;
197 return *this;
198 }
199
202 wasm_memorytype_t *p = nullptr;
203 auto *err = wasmtime_memorytype_new(
204 _min, _max.has_value(), _max.has_value() ? *_max : 0, _memory64,
205 _shared, _page_size_log2, &p);
206 if (err != nullptr) {
207 return Error(err);
208 }
209 assert(p != nullptr);
210 return MemoryType(p);
211 }
212 };
213};
214
215}; // namespace wasmtime
216
217#endif // WASMTIME_TYPES_MEMORY_HH
Errors coming from Wasmtime.
Definition: error.hh:26
Helper class to build a MemoryType.
Definition: types/memory.hh:148
Builder & max(std::optional< uint64_t > max)
Configure the maximal size, in pages, of linear memory.
Definition: types/memory.hh:167
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:195
Result< MemoryType > build() const
Construct the final MemoryType value.
Definition: types/memory.hh:201
Builder & memory64(bool enable)
Configure whether this is a 64-bit linear memory.
Definition: types/memory.hh:173
Builder & min(uint64_t min)
Configure the minimum size, in pages, of linear memory.
Definition: types/memory.hh:161
Builder()
Default constructor for a memory type with 0 initial size.
Definition: types/memory.hh:157
Builder & shared(bool enable)
Configure whether this is a shared linear memory.
Definition: types/memory.hh:179
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:145
MemoryType & operator=(const MemoryType &other)
Copies the provided type into a new type.
Definition: types/memory.hh:130
MemoryType(const MemoryType &other)
Copies the provided type into a new type.
Definition: types/memory.hh:127
MemoryType(Ref other)
Definition: types/memory.hh:125
static MemoryType New64(uint64_t min, uint64_t max)
Same as the MemoryType constructor, except creates a 64-bit memory.
Definition: types/memory.hh:115
static MemoryType New64(uint64_t min)
Same as the MemoryType constructor, except creates a 64-bit memory.
Definition: types/memory.hh:106
MemoryType & operator=(MemoryType &&other)=default
Moves the type information from another type into this one.
MemoryType(MemoryType &&other)=default
Moves the type information from another type into this one.
MemoryType(uint32_t min, uint32_t max)
Definition: types/memory.hh:96
Ref * operator->()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: types/memory.hh:142
MemoryType(uint32_t min)
Definition: types/memory.hh:85
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.