#pragma once #include #include #include "span.hpp" namespace hv { typedef std::uint8_t byte; struct memory_map { /// Create a copy-on-write memory map to a file descriptor inline static memory_map cow(std::uint64_t size, int&& fd) noexcept { return memory_map(size, std::move(fd), true); } /// Create a copy-on-write memory map to a file by name inline static memory_map cow(std::uint64_t size, const char* file) noexcept { return memory_map(size, file, true); } memory_map(std::uint64_t size, const char* const filename) noexcept; memory_map(std::uint64_t size, int&& fd) noexcept; ~memory_map(); inline byte& operator[](std::size_t index) { if (index>=size) throw "tried to index out of bounds of map"; return ((byte*)block0)[index]; } inline const byte& operator[](std::size_t index) const { if (index>=size) throw "tried to index out of bounds of map"; return ((byte*)block0)[index]; } inline hv::span span() { return hv::span((byte*)block0, size); } inline void* base() { return block0; } inline const void* base() const { return block0; } const bool is_cow; const std::uint64_t size; private: memory_map(std::uint64_t size, int&& fd, bool _cow) noexcept; memory_map(std::uint64_t size, const char* filename, bool _cow) noexcept; const int fd; void* const block0; }; struct const_memory_map { const_memory_map(std::uint64_t size, const char* const filename) noexcept; const_memory_map(std::uint64_t size, int&& fd) noexcept; ~const_memory_map(); inline const byte& operator[](std::size_t index) const { if (index>=size) throw "tried to index out of bounds of map"; return ((byte*)block0)[index]; } inline const void* base() const { return block0; } const std::uint64_t size; private: const int fd; const void* const block0; }; }