You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
1.9 KiB
81 lines
1.9 KiB
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <utility>
|
|
#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) std::terminate();
|
|
return ((byte*)block0)[index];
|
|
}
|
|
inline const byte& operator[](std::size_t index) const
|
|
{
|
|
if (index>=size) std::terminate();
|
|
return ((byte*)block0)[index];
|
|
}
|
|
|
|
inline hv::span<byte> 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) std::terminate();
|
|
return ((byte*)block0)[index];
|
|
}
|
|
|
|
inline const void* base() const
|
|
{
|
|
return block0;
|
|
}
|
|
const std::uint64_t size;
|
|
private:
|
|
const int fd;
|
|
const void* const block0;
|
|
};
|
|
}
|