~satchmo/libvmm

fca0f83e2ab85960552b584818dd45ab72778133 — Jason Phan 2 years ago 61e0303
memory: Add FileOffset back
1 files changed, 51 insertions(+), 0 deletions(-)

M vmm/memory/detail/guest.hpp
M vmm/memory/detail/guest.hpp => vmm/memory/detail/guest.hpp +51 -0
@@ 72,4 72,55 @@ class MemoryRegionAddress : public Address<MemoryRegionAddress, uint64_t>
        size_type m_addr{};
};

// The starting point of a file which backs a GuestMemoryRegion.
class FileOffset
{
    private:
        std::shared_ptr<std::fstream> m_fstream;
        long m_start{};
    public:
        FileOffset(const char* filename, std::ios_base::openmode mode,
                   const long start={})
            : m_fstream{std::make_shared<std::fstream>(filename, mode)},
              m_start{start}
        {
            if (start) {
                // TODO (?): Only call these if the appropriate openmode is set.
                m_fstream->seekg(start);
                m_fstream->seekp(start);
            }
        }

        explicit FileOffset(const char* filename, const long start={})
            : FileOffset(filename, std::ios_base::in | std::ios_base::out, start) {}

        explicit FileOffset(const std::string& filename, const long start=0)
            : FileOffset(filename.c_str(), start) {}

        FileOffset(const std::string& filename,
                   const std::ios_base::openmode mode,
                   const long start={})
            : FileOffset(filename.c_str(), mode, start) {}

        explicit FileOffset(const std::filesystem::path& filename, const long start=0)
            : FileOffset(filename.c_str(), start) {}

        FileOffset(const std::filesystem::path& filename,
                   const std::ios_base::openmode mode,
                   const long start={})
            : FileOffset(filename.c_str(), mode, start) {}

        // TODO: thread-safety
        [[nodiscard]] auto data() const -> std::basic_filebuf<std::fstream::char_type,
                                                              std::fstream::traits_type>*
        {
            return m_fstream->rdbuf();
        }

        [[nodiscard]] constexpr auto start() const noexcept -> long
        {
            return m_start;
        }
};

}  // vmm::memory::detail