Kagome
Polkadot Runtime Engine in C++17
memory.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_MEMORY_HPP
7 #define KAGOME_MEMORY_HPP
8 
9 #include <array>
10 
11 #include <gsl/span>
12 #include <optional>
13 
14 #include "common/buffer.hpp"
15 #include "common/literals.hpp"
16 #include "runtime/types.hpp"
17 
18 namespace kagome::runtime {
19 
20  constexpr inline size_t kInitialMemorySize = []() {
21  using kagome::common::literals::operator""_MB;
22  return 2_MB;
23  }();
24 
25  // according to $3.1.2.1 in the Polkadot Host Spec
26  constexpr inline size_t kMemoryPageSize = []() {
27  using kagome::common::literals::operator""_kB;
28  return 64_kB;
29  }();
30 
39  class Memory {
40  public:
41  virtual ~Memory() = default;
42 
43  constexpr static uint32_t kMaxMemorySize =
44  std::numeric_limits<uint32_t>::max();
45 
49  virtual WasmSize size() const = 0;
50 
55  virtual void resize(WasmSize new_size) = 0;
56 
63  virtual WasmPointer allocate(WasmSize size) = 0;
64 
71  virtual std::optional<WasmSize> deallocate(WasmPointer ptr) = 0;
72 
76  virtual int8_t load8s(WasmPointer addr) const = 0;
77  virtual uint8_t load8u(WasmPointer addr) const = 0;
78  virtual int16_t load16s(WasmPointer addr) const = 0;
79  virtual uint16_t load16u(WasmPointer addr) const = 0;
80  virtual int32_t load32s(WasmPointer addr) const = 0;
81  virtual uint32_t load32u(WasmPointer addr) const = 0;
82  virtual int64_t load64s(WasmPointer addr) const = 0;
83  virtual uint64_t load64u(WasmPointer addr) const = 0;
84  virtual std::array<uint8_t, 16> load128(WasmPointer addr) const = 0;
85 
92  virtual common::Buffer loadN(WasmPointer addr, WasmSize n) const = 0;
99  virtual std::string loadStr(WasmPointer addr, WasmSize n) const = 0;
100 
104  virtual void store8(WasmPointer addr, int8_t value) = 0;
105  virtual void store16(WasmPointer addr, int16_t value) = 0;
106  virtual void store32(WasmPointer addr, int32_t value) = 0;
107  virtual void store64(WasmPointer addr, int64_t value) = 0;
108  virtual void store128(WasmPointer addr,
109  const std::array<uint8_t, 16> &value) = 0;
110  virtual void storeBuffer(WasmPointer addr,
111  gsl::span<const uint8_t> value) = 0;
112 
118  virtual WasmSpan storeBuffer(gsl::span<const uint8_t> value) = 0;
119  };
120 } // namespace kagome::runtime
121 
122 #endif // KAGOME_MEMORY_HPP
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
virtual int16_t load16s(WasmPointer addr) const =0
virtual uint16_t load16u(WasmPointer addr) const =0
virtual void store128(WasmPointer addr, const std::array< uint8_t, 16 > &value)=0
virtual int32_t load32s(WasmPointer addr) const =0
uint32_t WasmSize
Size type is uint32_t because we are working in 32 bit address space.
Definition: types.hpp:35
virtual void store32(WasmPointer addr, int32_t value)=0
virtual common::Buffer loadN(WasmPointer addr, WasmSize n) const =0
virtual uint32_t load32u(WasmPointer addr) const =0
virtual void store16(WasmPointer addr, int16_t value)=0
virtual void resize(WasmSize new_size)=0
virtual WasmPointer allocate(WasmSize size)=0
virtual ~Memory()=default
constexpr size_t kInitialMemorySize
Definition: memory.hpp:20
virtual uint64_t load64u(WasmPointer addr) const =0
virtual int64_t load64s(WasmPointer addr) const =0
virtual std::array< uint8_t, 16 > load128(WasmPointer addr) const =0
virtual uint8_t load8u(WasmPointer addr) const =0
static constexpr uint32_t kMaxMemorySize
Definition: memory.hpp:43
uint64_t WasmSpan
combination of pointer and size, where less significant part represents wasm pointer, and most significant represents size
Definition: types.hpp:31
virtual WasmSize size() const =0
Return the size of the memory.
uint32_t WasmPointer
type of wasm memory is 32 bit integer
Definition: types.hpp:26
constexpr size_t kMemoryPageSize
Definition: memory.hpp:26
virtual int8_t load8s(WasmPointer addr) const =0
virtual void storeBuffer(WasmPointer addr, gsl::span< const uint8_t > value)=0
virtual std::string loadStr(WasmPointer addr, WasmSize n) const =0
virtual void store64(WasmPointer addr, int64_t value)=0
virtual void store8(WasmPointer addr, int8_t value)=0
virtual std::optional< WasmSize > deallocate(WasmPointer ptr)=0