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.
39 lines
1.3 KiB
39 lines
1.3 KiB
#include <sys/mman.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
|
|
#include <memory-map.hpp>
|
|
|
|
namespace hv
|
|
{
|
|
memory_map::memory_map(std::uint64_t size, int&& fd) noexcept
|
|
: is_cow(false), size(size), fd(fd), block0(mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0))
|
|
{
|
|
if(!block0) std::terminate();
|
|
}
|
|
memory_map::memory_map(std::uint64_t size, int&& fd, bool _cow) noexcept
|
|
: is_cow(true), size(size), fd(fd), block0(mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0))
|
|
{
|
|
if(!block0) std::terminate();
|
|
}
|
|
memory_map::memory_map(std::uint64_t size, const char* file) noexcept
|
|
: memory_map(size, open(file, O_RDWR)) {}
|
|
memory_map::memory_map(std::uint64_t size, const char* file, bool _cow) noexcept
|
|
: memory_map(size, open(file, O_RDONLY), _cow) {}
|
|
memory_map::~memory_map()
|
|
{
|
|
munmap(block0, size);
|
|
close(fd);
|
|
}
|
|
|
|
const_memory_map::const_memory_map(std::uint64_t size, int&& fd) noexcept
|
|
: size(size), fd(fd), block0(mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0)) {}
|
|
const_memory_map::const_memory_map(std::uint64_t size, const char* file) noexcept
|
|
: const_memory_map(size, open(file, O_RDONLY)) {}
|
|
const_memory_map::~const_memory_map()
|
|
{
|
|
munmap(const_cast<void*>(block0), size);
|
|
close(fd);
|
|
}
|
|
}
|