~satchmo/libvmm

2aae00d0a5623417f8c5bb21644b38bb5d54cc0d — Jason Phan 4 months ago 8d55686
memory: Add FileOffset
2 files changed, 57 insertions(+), 0 deletions(-)

M vmm/memory/detail/guest.hpp
M vmm/memory/memory.hpp
M vmm/memory/detail/guest.hpp => vmm/memory/detail/guest.hpp +55 -0
@@ 6,8 6,13 @@

#include <cassert> // assert
#include <cstdint> // uint*_t
#include <filesystem> // path
#include <fstream> // fstream
#include <ios> // ios_base
#include <iostream> // ostream
#include <limits> // numeric_limits
#include <memory> // shared_ptr
#include <string> // string

#include "vmm/memory/detail/address.hpp" // Address, AddressValue



@@ 93,4 98,54 @@ class MemoryRegionAddress : public Address<MemoryRegionAddress, uint64_t>
        auto align(const size_type alignment) noexcept -> reference override;
};

// The starting point of a file that 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

M vmm/memory/memory.hpp => vmm/memory/memory.hpp +2 -0
@@ 18,4 18,6 @@ using Address = vmm::memory::detail::Address<T, S>;
using GuestAddress = vmm::memory::detail::GuestAddress;
using MemoryRegionAddress = vmm::memory::detail::MemoryRegionAddress;

using FileOffset = vmm::memory::detail::FileOffset;

}  // vmm::memory