1 #ifndef OSMIUM_UTIL_MEMORY_MAPPING_HPP 2 #define OSMIUM_UTIL_MEMORY_MAPPING_HPP 40 #include <system_error> 46 # include <sys/mman.h> 51 # include <sys/types.h> 132 using flag_type = int;
141 throw std::runtime_error(
"Zero-sized mapping is not allowed.");
147 HANDLE get_handle()
const noexcept;
148 HANDLE osmium::util::MemoryMapping::create_file_mapping()
const noexcept;
149 void* osmium::util::MemoryMapping::map_view_of_file()
const noexcept;
218 }
catch (
const std::system_error&) {
242 void resize(
size_t new_size);
248 explicit operator bool() const noexcept {
266 int fd() const noexcept {
282 template <
typename T =
void>
285 return reinterpret_cast<T*
>(
m_addr);
287 throw std::runtime_error(
"invalid memory mapping");
315 void resize(
size_t) =
delete;
329 template <
typename T>
357 m_mapping(sizeof(T) * size, mode, fd, sizeof(T) * offset) {
413 m_mapping.
resize(
sizeof(T) * new_size);
420 explicit operator bool() const noexcept {
430 assert(m_mapping.
size() %
sizeof(T) == 0);
431 return m_mapping.
size() /
sizeof(T);
439 int fd() const noexcept {
440 return m_mapping.
fd();
486 template <
typename T>
500 void resize(
size_t) =
delete;
514 #pragma GCC diagnostic push 515 #pragma GCC diagnostic ignored "-Wold-style-cast" 518 return m_addr != MAP_FAILED;
525 #pragma GCC diagnostic pop 528 #ifndef MAP_ANONYMOUS 529 # define MAP_ANONYMOUS MAP_ANON 536 return PROT_READ | PROT_WRITE;
557 throw std::system_error(errno, std::system_category(),
"mmap failed");
567 other.make_invalid();
577 other.make_invalid();
584 throw std::system_error(errno, std::system_category(),
"munmap failed");
591 assert(new_size > 0 &&
"can not resize to zero size");
596 throw std::system_error(errno, std::system_category(),
"mremap failed");
600 assert(
false &&
"can't resize anonymous mappings on non-linux systems");
608 throw std::system_error(errno, std::system_category(),
"mmap (remap) failed");
628 inline DWORD dword_hi(uint64_t x) {
629 return static_cast<DWORD
>(x >> 32);
632 inline DWORD dword_lo(uint64_t x) {
633 return static_cast<DWORD
>(x & 0xffffffff);
643 return PAGE_READONLY;
645 return PAGE_WRITECOPY;
647 return PAGE_READWRITE;
654 return FILE_MAP_READ;
656 return FILE_MAP_COPY;
658 return FILE_MAP_WRITE;
662 inline HANDLE osmium::util::MemoryMapping::get_handle()
const noexcept {
664 return INVALID_HANDLE_VALUE;
666 return reinterpret_cast<HANDLE
>(_get_osfhandle(
m_fd));
669 inline HANDLE osmium::util::MemoryMapping::create_file_mapping()
const noexcept {
671 _setmode(
m_fd, _O_BINARY);
673 return CreateFileMapping(get_handle(),
nullptr,
get_protection(), osmium::util::dword_hi(static_cast<uint64_t>(
m_size) +
m_offset), osmium::util::dword_lo(static_cast<uint64_t>(
m_size) +
m_offset),
nullptr);
676 inline void* osmium::util::MemoryMapping::map_view_of_file()
const noexcept {
693 m_handle(create_file_mapping()),
697 throw std::system_error(GetLastError(), std::system_category(),
"CreateFileMapping failed");
700 m_addr = map_view_of_file();
702 throw std::system_error(GetLastError(), std::system_category(),
"MapViewOfFile failed");
711 m_handle(std::move(other.m_handle)),
713 other.make_invalid();
714 other.m_handle =
nullptr;
723 m_handle = std::move(other.m_handle);
725 other.make_invalid();
726 other.m_handle =
nullptr;
732 if (! UnmapViewOfFile(
m_addr)) {
733 throw std::system_error(GetLastError(), std::system_category(),
"UnmapViewOfFile failed");
739 if (! CloseHandle(m_handle)) {
740 throw std::system_error(GetLastError(), std::system_category(),
"CloseHandle failed");
752 m_handle = create_file_mapping();
754 throw std::system_error(GetLastError(), std::system_category(),
"CreateFileMapping failed");
757 m_addr = map_view_of_file();
759 throw std::system_error(GetLastError(), std::system_category(),
"MapViewOfFile failed");
765 #endif // OSMIUM_UTIL_MEMORY_MAPPING_HPP ~MemoryMapping() noexcept
Definition: memory_mapping.hpp:215
bool is_valid() const noexcept
Definition: memory_mapping.hpp:517
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
flag_type get_protection() const noexcept
Definition: memory_mapping.hpp:532
MemoryMapping m_mapping
Definition: memory_mapping.hpp:332
int flag_type
Definition: memory_mapping.hpp:132
MemoryMapping(size_t size, mapping_mode mode, int fd=-1, off_t offset=0)
Definition: memory_mapping.hpp:549
size_t file_size(int fd)
Definition: file.hpp:67
const T * begin() const
Definition: memory_mapping.hpp:476
flag_type get_flags() const noexcept
Definition: memory_mapping.hpp:539
int fd() const noexcept
Definition: memory_mapping.hpp:266
void unmap()
Definition: memory_mapping.hpp:581
Definition: memory_mapping.hpp:95
mapping_mode
Definition: memory_mapping.hpp:98
void resize(size_t new_size)
Definition: memory_mapping.hpp:412
static size_t check_size(size_t size)
Definition: memory_mapping.hpp:139
int resize_fd(int fd)
Definition: memory_mapping.hpp:152
T * end()
Definition: memory_mapping.hpp:464
void * m_addr
The address where the memory is mapped.
Definition: memory_mapping.hpp:123
off_t m_offset
Offset into the file.
Definition: memory_mapping.hpp:110
#define MAP_ANONYMOUS
Definition: memory_mapping.hpp:529
int m_fd
File handle we got the mapping from.
Definition: memory_mapping.hpp:113
size_t size() const noexcept
Definition: memory_mapping.hpp:257
mapping_mode m_mapping_mode
Mapping mode.
Definition: memory_mapping.hpp:116
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
AnonymousMemoryMapping(size_t size)
Definition: memory_mapping.hpp:306
Definition: memory_mapping.hpp:487
OSMIUM_DEPRECATED TypedMemoryMapping(size_t size, bool writable, int fd, off_t offset=0)
Definition: memory_mapping.hpp:365
TypedMemoryMapping(size_t size, MemoryMapping::mapping_mode mode, int fd, off_t offset=0)
Definition: memory_mapping.hpp:356
size_t size() const noexcept
Definition: memory_mapping.hpp:429
T * begin()
Definition: memory_mapping.hpp:455
Definition: memory_mapping.hpp:302
void make_invalid() noexcept
Definition: memory_mapping.hpp:521
const T * cbegin() const
Definition: memory_mapping.hpp:468
AnonymousTypedMemoryMapping(size_t size)
Definition: memory_mapping.hpp:491
int fd() const noexcept
Definition: memory_mapping.hpp:439
void resize(size_t new_size)
Definition: memory_mapping.hpp:590
MemoryMapping & operator=(const MemoryMapping &)=delete
You can not copy a MemoryMapping.
Definition: memory_mapping.hpp:330
const T * end() const
Definition: memory_mapping.hpp:480
void unmap()
Definition: memory_mapping.hpp:398
bool writable() const noexcept
Definition: memory_mapping.hpp:273
void resize_file(int fd, size_t new_size)
Definition: file.hpp:132
size_t m_size
The size of the mapping.
Definition: memory_mapping.hpp:107
T * get_addr() const
Definition: memory_mapping.hpp:283
TypedMemoryMapping(size_t size)
Definition: memory_mapping.hpp:342
OSMIUM_DEPRECATED MemoryMapping(size_t size, bool writable=true, int fd=-1, off_t offset=0)
Definition: memory_mapping.hpp:190
const T * cend() const
Definition: memory_mapping.hpp:472
bool writable() const noexcept
Definition: memory_mapping.hpp:446