Skip to content

src/runtime/wasm_memory.hpp

Namespaces

Name
sgns
sgns::runtime

Classes

Name
class sgns::runtime::WasmMemory

Source code

#ifndef SUPERGENIUS_SRC_MEMORY_HPP
#define SUPERGENIUS_SRC_MEMORY_HPP

#include <array>

#include <boost/optional.hpp>
#include <gsl/span>
#include "base/buffer.hpp"
#include "runtime/types.hpp"

namespace sgns::runtime {

  // The underlying memory can be accessed through unaligned pointers which
  // isn't well-behaved in C++. WebAssembly nonetheless expects it to behave
  // properly. Avoid emitting unaligned load/store by checking for alignment
  // explicitly, and performing memcpy if unaligned.
  //
  // The allocated memory tries to have the same alignment as the memory being
  // simulated.
  class WasmMemory {
   public:
    virtual ~WasmMemory() = default;

    constexpr static uint32_t kMaxMemorySize =
        std::numeric_limits<uint32_t>::max();

    virtual void reset() = 0;

    virtual WasmSize size() const = 0;

    virtual void resize(WasmSize newSize) = 0;

    virtual WasmPointer allocate(WasmSize size) = 0;

    virtual boost::optional<WasmSize> deallocate(WasmPointer ptr) = 0;

    virtual int8_t load8s(WasmPointer addr) const = 0;
    virtual uint8_t load8u(WasmPointer addr) const = 0;
    virtual int16_t load16s(WasmPointer addr) const = 0;
    virtual uint16_t load16u(WasmPointer addr) const = 0;
    virtual int32_t load32s(WasmPointer addr) const = 0;
    virtual uint32_t load32u(WasmPointer addr) const = 0;
    virtual int64_t load64s(WasmPointer addr) const = 0;
    virtual uint64_t load64u(WasmPointer addr) const = 0;
    virtual std::array<uint8_t, 16> load128(WasmPointer addr) const = 0;

    virtual base::Buffer loadN(WasmPointer addr, WasmSize n) const = 0;
    virtual std::string loadStr(WasmPointer addr, WasmSize n) const = 0;

    virtual void store8(WasmPointer addr, int8_t value) = 0;
    virtual void store16(WasmPointer addr, int16_t value) = 0;
    virtual void store32(WasmPointer addr, int32_t value) = 0;
    virtual void store64(WasmPointer addr, int64_t value) = 0;
    virtual void store128(WasmPointer addr,
                          const std::array<uint8_t, 16> &value) = 0;
    virtual void storeBuffer(WasmPointer addr,
                             gsl::span<const uint8_t> value) = 0;

    virtual WasmSpan storeBuffer(gsl::span<const uint8_t> value) = 0;
  };
}  // namespace sgns::runtime

#endif  // SUPERGENIUS_SRC_MEMORY_HPP

Updated on 2026-03-04 at 13:10:45 -0800