Kagome
Polkadot Runtime Engine in C++17
memory_allocator.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_RUNTIME_COMMON_MEMORY_ALLOCATOR_HPP
7 #define KAGOME_CORE_RUNTIME_COMMON_MEMORY_ALLOCATOR_HPP
8 
9 #include <map>
10 #include <unordered_map>
11 
12 #include <optional>
13 
14 #include "common/literals.hpp"
15 #include "log/logger.hpp"
16 #include "primitives/math.hpp"
17 #include "runtime/types.hpp"
18 
19 namespace kagome::runtime {
20 
21  // Alignment for pointers, same with substrate:
22  // https://github.com/paritytech/substrate/blob/743981a083f244a090b40ccfb5ce902199b55334/primitives/allocator/src/freeing_bump.rs#L56
23  constexpr uint8_t kAlignment = sizeof(size_t);
24  constexpr size_t kDefaultHeapBase = [] {
25  using namespace kagome::common::literals;
26  return 1_MB; // 1Mb
27  }();
28 
36  template <typename T>
37  static constexpr T roundUpAlign(T t) {
38  return math::roundUp<kAlignment>(t);
39  }
40 
45  class MemoryAllocator final {
46  public:
47  struct MemoryHandle {
48  std::function<void(size_t)> resize;
49  std::function<size_t()> getSize;
50  };
51  MemoryAllocator(MemoryHandle memory, size_t size, WasmPointer heap_base);
52 
53  WasmPointer allocate(WasmSize size);
54  std::optional<WasmSize> deallocate(WasmPointer ptr);
55 
56  template <typename T>
57  bool checkAddress(WasmPointer addr) noexcept {
58  return offset_ > addr and offset_ - addr >= sizeof(T);
59  }
60 
61  bool checkAddress(WasmPointer addr, size_t size) noexcept {
62  return offset_ > addr and offset_ - addr >= size;
63  }
64 
66  std::optional<WasmSize> getDeallocatedChunkSize(WasmPointer ptr) const;
67  std::optional<WasmSize> getAllocatedChunkSize(WasmPointer ptr) const;
68  size_t getAllocatedChunksNum() const;
69  size_t getDeallocatedChunksNum() const;
70 
71  private:
79  WasmPointer freealloc(WasmSize size);
80 
87  WasmPointer growAlloc(WasmSize size);
88 
89  void resize(WasmSize size);
90 
91  private:
93 
94  // map containing addresses of allocated MemoryImpl chunks
95  std::unordered_map<WasmPointer, WasmSize> allocated_;
96 
97  // map containing addresses to the deallocated MemoryImpl chunks
98  std::map<WasmPointer, WasmSize> deallocated_;
99 
100  // Offset on the tail of the last allocated MemoryImpl chunk
101  size_t offset_;
102 
103  size_t size_;
104 
106  };
107 
108 } // namespace kagome::runtime
109 
110 #endif // KAGOME_CORE_RUNTIME_COMMON_MEMORY_ALLOCATOR_HPP
uint32_t WasmSize
Size type is uint32_t because we are working in 32 bit address space.
Definition: types.hpp:35
std::unordered_map< WasmPointer, WasmSize > allocated_
constexpr size_t kDefaultHeapBase
static constexpr T roundUpAlign(T t)
std::map< WasmPointer, WasmSize > deallocated_
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
bool checkAddress(WasmPointer addr, size_t size) noexcept
uint32_t WasmPointer
type of wasm memory is 32 bit integer
Definition: types.hpp:26
constexpr uint8_t kAlignment
bool checkAddress(WasmPointer addr) noexcept