Wasmtime
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/memory.h>
12
13namespace wasmtime {
14
19 friend class Memory;
20
21 struct deleter {
22 void operator()(wasm_memorytype_t *p) const { wasm_memorytype_delete(p); }
23 };
24
25 std::unique_ptr<wasm_memorytype_t, deleter> ptr;
26
27public:
30 class Ref {
31 friend class MemoryType;
32
33 const wasm_memorytype_t *ptr;
34
35 public:
37 Ref(const wasm_memorytype_t *ptr) : ptr(ptr) {}
39 Ref(const MemoryType &ty) : Ref(ty.ptr.get()) {}
40
42 uint64_t min() const { return wasmtime_memorytype_minimum(ptr); }
43
46 std::optional<uint64_t> max() const {
47 uint64_t max = 0;
48 auto present = wasmtime_memorytype_maximum(ptr, &max);
49 if (present) {
50 return max;
51 }
52 return std::nullopt;
53 }
54
56 bool is_64() const { return wasmtime_memorytype_is64(ptr); }
57
59 bool is_shared() const { return wasmtime_memorytype_isshared(ptr); }
60 };
61
62private:
63 Ref ref;
64 MemoryType(wasm_memorytype_t *ptr) : ptr(ptr), ref(ptr) {}
65
66public:
70 explicit MemoryType(uint32_t min)
71 : MemoryType(wasmtime_memorytype_new(min, false, 0, false, false)) {}
74 MemoryType(uint32_t min, uint32_t max)
75 : MemoryType(wasmtime_memorytype_new(min, true, max, false, false)) {}
76
78 static MemoryType New64(uint64_t min) {
79 return MemoryType(wasmtime_memorytype_new(min, false, 0, true, false));
80 }
81
83 static MemoryType New64(uint64_t min, uint64_t max) {
84 return MemoryType(wasmtime_memorytype_new(min, true, max, true, false));
85 }
86
91 MemoryType(const MemoryType &other)
92 : MemoryType(wasm_memorytype_copy(other.ptr.get())) {}
95 ptr.reset(wasm_memorytype_copy(other.ptr.get()));
96 return *this;
97 }
98 ~MemoryType() = default;
100 MemoryType(MemoryType &&other) = default;
102 MemoryType &operator=(MemoryType &&other) = default;
103
106 Ref *operator->() { return &ref; }
109 Ref *operator*() { return &ref; }
110
112 class Builder {
113 uint64_t _min;
114 std::optional<uint64_t> _max;
115 bool _memory64;
116 bool _shared;
117
118 public:
120 Builder() : _min(0), _memory64(false), _shared(false) {}
121
123 Builder& min(uint64_t min) {
124 _min = min;
125 return *this;
126 }
127
129 Builder& max(std::optional<uint64_t> max) {
130 _max = max;
131 return *this;
132 }
133
135 Builder& memory64(bool enable) {
136 _memory64 = enable;
137 return *this;
138 }
139
141 Builder& shared(bool enable) {
142 _shared = enable;
143 return *this;
144 }
145
149 _min, _max.has_value(), _max.has_value() ? *_max : 0,
150 _memory64, _shared));
151 }
152 };
153};
154
155}; // namespace wasmtime
156
157#endif // WASMTIME_TYPES_MEMORY_HH
Helper class to build a MemoryType.
Definition: memory.hh:112
Builder & max(std::optional< uint64_t > max)
Configure the maximal size, in pages, of linear memory.
Definition: memory.hh:129
Builder & memory64(bool enable)
Configure whether this is a 64-bit linear memory.
Definition: memory.hh:135
Builder & min(uint64_t min)
Configure the minimum size, in pages, of linear memory.
Definition: memory.hh:123
MemoryType build() const
Construct the final MemoryType value.
Definition: memory.hh:147
Builder()
Default constructor for a memory type with 0 initial size.
Definition: memory.hh:120
Builder & shared(bool enable)
Configure whether this is a shared linear memory.
Definition: memory.hh:141
Non-owning reference to a MemoryType, must not be used after the original owner has been deleted.
Definition: memory.hh:30
Ref(const wasm_memorytype_t *ptr)
Creates a reference from the raw C API representation.
Definition: memory.hh:37
bool is_64() const
Returns whether or not this is a 64-bit memory type.
Definition: memory.hh:56
bool is_shared() const
Returns whether or not this is a shared memory type.
Definition: memory.hh:59
std::optional< uint64_t > max() const
Definition: memory.hh:46
uint64_t min() const
Returns the minimum size, in WebAssembly pages, of this memory.
Definition: memory.hh:42
Ref(const MemoryType &ty)
Creates a reference from an original MemoryType.
Definition: memory.hh:39
Type information about a WebAssembly linear memory.
Definition: memory.hh:18
Ref * operator*()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: memory.hh:109
MemoryType & operator=(const MemoryType &other)
Copies the provided type into a new type.
Definition: memory.hh:94
MemoryType(const MemoryType &other)
Copies the provided type into a new type.
Definition: memory.hh:91
MemoryType(Ref other)
Definition: memory.hh:89
static MemoryType New64(uint64_t min, uint64_t max)
Same as the MemoryType constructor, except creates a 64-bit memory.
Definition: memory.hh:83
static MemoryType New64(uint64_t min)
Same as the MemoryType constructor, except creates a 64-bit memory.
Definition: memory.hh:78
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: memory.hh:74
Ref * operator->()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: memory.hh:106
MemoryType(uint32_t min)
Definition: memory.hh:70
A WebAssembly linear memory.
Definition: wasmtime.hh:864
uint64_t wasmtime_memorytype_minimum(const wasm_memorytype_t *ty)
Returns the minimum size, in pages, of the specified memory type.
bool wasmtime_memorytype_is64(const wasm_memorytype_t *ty)
Returns whether this type of memory represents a 64-bit memory.
bool wasmtime_memorytype_isshared(const wasm_memorytype_t *ty)
Returns whether this type of memory represents a shared memory.
wasm_memorytype_t * wasmtime_memorytype_new(uint64_t min, bool max_present, uint64_t max, bool is_64, bool shared)
Creates a new memory type from the specified parameters.
bool wasmtime_memorytype_maximum(const wasm_memorytype_t *ty, uint64_t *max)
Returns the maximum size, in pages, of the specified memory type.
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.