1 #ifndef OSMIUM_UTIL_MEMORY_MAPPING_HPP
2 #define OSMIUM_UTIL_MEMORY_MAPPING_HPP
39 #include <system_error>
45 # include <sys/mman.h>
50 # include <sys/types.h>
150 HANDLE get_handle() const noexcept;
221 }
catch (std::system_error&) {
245 void resize(
size_t new_size);
251 explicit operator bool() const noexcept {
269 int fd() const noexcept {
285 template <
typename T =
void>
288 return reinterpret_cast<T*
>(
m_addr);
290 throw std::runtime_error(
"invalid memory mapping");
318 void resize(
size_t) =
delete;
332 template <
typename T>
346 m_mapping(sizeof(T) * size,
MemoryMapping::mapping_mode::write_private) {
360 m_mapping(sizeof(T) * size, mode, fd, sizeof(T) * offset) {
369 m_mapping(sizeof(T) * size, writable ?
MemoryMapping::mapping_mode::write_shared :
MemoryMapping::mapping_mode::readonly, fd, sizeof(T) * offset) {
416 m_mapping.
resize(
sizeof(T) * new_size);
423 explicit operator bool() const noexcept {
433 assert(m_mapping.
size() %
sizeof(T) == 0);
434 return m_mapping.
size() /
sizeof(T);
442 int fd() const noexcept {
443 return m_mapping.
fd();
489 template <
typename T>
503 void resize(
size_t) =
delete;
517 #pragma GCC diagnostic push
518 #pragma GCC diagnostic ignored "-Wold-style-cast"
521 return m_addr != MAP_FAILED;
528 #pragma GCC diagnostic pop
531 #ifndef MAP_ANONYMOUS
532 # define MAP_ANONYMOUS MAP_ANON
536 if (m_mapping_mode == mapping_mode::readonly) {
539 return PROT_READ | PROT_WRITE;
546 if (m_mapping_mode == mapping_mode::write_shared) {
553 m_size(initial_size(size)),
556 m_mapping_mode(mode),
557 m_addr(::mmap(nullptr, m_size, get_protection(), get_flags(), m_fd, m_offset)) {
560 throw std::system_error(errno, std::system_category(),
"mmap failed");
565 m_size(other.m_size),
566 m_offset(other.m_offset),
568 m_mapping_mode(other.m_mapping_mode),
569 m_addr(other.m_addr) {
570 other.make_invalid();
575 m_size = other.m_size;
576 m_offset = other.m_offset;
578 m_mapping_mode = other.m_mapping_mode;
579 m_addr = other.m_addr;
580 other.make_invalid();
586 if (::munmap(m_addr, m_size) != 0) {
587 throw std::system_error(errno, std::system_category(),
"munmap failed");
594 assert(new_size > 0 &&
"can not resize to zero size");
597 m_addr = ::mremap(m_addr, m_size, new_size, MREMAP_MAYMOVE);
599 throw std::system_error(errno, std::system_category(),
"mremap failed");
603 assert(
false &&
"can't resize anonymous mappings on non-linux systems");
609 m_addr = ::mmap(
nullptr, new_size, get_protection(), get_flags(), m_fd, m_offset);
611 throw std::system_error(errno, std::system_category(),
"mmap (remap) failed");
631 inline DWORD dword_hi(uint64_t x) {
632 return static_cast<DWORD
>(x >> 32);
635 inline DWORD dword_lo(uint64_t x) {
636 return static_cast<DWORD
>(x & 0xffffffff);
644 switch (m_mapping_mode) {
645 case mapping_mode::readonly:
646 return PAGE_READONLY;
647 case mapping_mode::write_private:
648 return PAGE_WRITECOPY;
649 case mapping_mode::write_shared:
650 return PAGE_READWRITE;
655 switch (m_mapping_mode) {
656 case mapping_mode::readonly:
657 return FILE_MAP_READ;
658 case mapping_mode::write_private:
659 return FILE_MAP_COPY;
660 case mapping_mode::write_shared:
661 return FILE_MAP_WRITE;
665 inline HANDLE osmium::util::MemoryMapping::get_handle() const noexcept {
667 return INVALID_HANDLE_VALUE;
669 return reinterpret_cast<HANDLE
>(_get_osfhandle(m_fd));
672 inline HANDLE osmium::util::MemoryMapping::create_file_mapping() const noexcept {
674 _setmode(m_fd, _O_BINARY);
676 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);
679 inline void* osmium::util::MemoryMapping::map_view_of_file() const noexcept {
680 return MapViewOfFile(m_handle, get_flags(), osmium::util::dword_hi(m_offset), osmium::util::dword_lo(m_offset), m_size);
684 return m_addr !=
nullptr;
692 m_size(initial_size(size)),
695 m_mapping_mode(mode),
696 m_handle(create_file_mapping()),
700 throw std::system_error(GetLastError(), std::system_category(),
"CreateFileMapping failed");
703 m_addr = map_view_of_file();
705 throw std::system_error(GetLastError(), std::system_category(),
"MapViewOfFile failed");
710 m_size(other.m_size),
711 m_offset(other.m_offset),
713 m_mapping_mode(other.m_mapping_mode),
714 m_handle(
std::move(other.m_handle)),
715 m_addr(other.m_addr) {
716 other.make_invalid();
717 other.m_handle =
nullptr;
722 m_size = other.m_size;
723 m_offset = other.m_offset;
725 m_mapping_mode = other.m_mapping_mode;
726 m_handle = std::move(other.m_handle);
727 m_addr = other.m_addr;
728 other.make_invalid();
729 other.m_handle =
nullptr;
735 if (! UnmapViewOfFile(m_addr)) {
736 throw std::system_error(GetLastError(), std::system_category(),
"UnmapViewOfFile failed");
742 if (! CloseHandle(m_handle)) {
743 throw std::system_error(GetLastError(), std::system_category(),
"CloseHandle failed");
755 m_handle = create_file_mapping();
757 throw std::system_error(GetLastError(), std::system_category(),
"CreateFileMapping failed");
760 m_addr = map_view_of_file();
762 throw std::system_error(GetLastError(), std::system_category(),
"MapViewOfFile failed");
768 #endif // OSMIUM_UTIL_MEMORY_MAPPING_HPP
~MemoryMapping() noexcept
Definition: memory_mapping.hpp:218
const T * end() const
Definition: memory_mapping.hpp:483
bool is_valid() const noexcept
Definition: memory_mapping.hpp:520
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
flag_type get_protection() const noexcept
Definition: memory_mapping.hpp:535
MemoryMapping m_mapping
Definition: memory_mapping.hpp:335
MemoryMapping(size_t size, mapping_mode mode, int fd=-1, off_t offset=0)
Definition: memory_mapping.hpp:552
~TypedMemoryMapping() noexcept=default
size_t file_size(int fd)
Definition: file.hpp:69
flag_type get_flags() const noexcept
Definition: memory_mapping.hpp:542
int fd() const noexcept
Definition: memory_mapping.hpp:269
static size_t initial_size(size_t size)
Definition: memory_mapping.hpp:142
void unmap()
Definition: memory_mapping.hpp:584
Definition: memory_mapping.hpp:94
Definition: reader_iterator.hpp:39
mapping_mode
Definition: memory_mapping.hpp:97
void resize(size_t new_size)
Definition: memory_mapping.hpp:415
int flag_type
Definition: memory_mapping.hpp:131
int resize_fd(int fd)
Definition: memory_mapping.hpp:155
T * end()
Definition: memory_mapping.hpp:467
size_t get_pagesize()
Definition: file.hpp:105
void * m_addr
The address where the memory is mapped.
Definition: memory_mapping.hpp:122
const T * cbegin() const
Definition: memory_mapping.hpp:471
off_t m_offset
Offset into the file.
Definition: memory_mapping.hpp:109
#define MAP_ANONYMOUS
Definition: memory_mapping.hpp:532
int m_fd
File handle we got the mapping from.
Definition: memory_mapping.hpp:112
size_t size() const noexcept
Definition: memory_mapping.hpp:260
mapping_mode m_mapping_mode
Mapping mode.
Definition: memory_mapping.hpp:115
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
AnonymousMemoryMapping(size_t size)
Definition: memory_mapping.hpp:309
Definition: memory_mapping.hpp:490
OSMIUM_DEPRECATED TypedMemoryMapping(size_t size, bool writable, int fd, off_t offset=0)
Definition: memory_mapping.hpp:368
TypedMemoryMapping(size_t size, MemoryMapping::mapping_mode mode, int fd, off_t offset=0)
Definition: memory_mapping.hpp:359
size_t size() const noexcept
Definition: memory_mapping.hpp:432
T * begin()
Definition: memory_mapping.hpp:458
Definition: memory_mapping.hpp:305
void make_invalid() noexcept
Definition: memory_mapping.hpp:524
AnonymousTypedMemoryMapping(size_t size)
Definition: memory_mapping.hpp:494
int fd() const noexcept
Definition: memory_mapping.hpp:442
void resize(size_t new_size)
Definition: memory_mapping.hpp:593
MemoryMapping & operator=(const MemoryMapping &)=delete
You can not copy a MemoryMapping.
Definition: memory_mapping.hpp:333
TypedMemoryMapping & operator=(const TypedMemoryMapping &)=delete
You can not copy a MemoryMapping.
void unmap()
Definition: memory_mapping.hpp:401
bool writable() const noexcept
Definition: memory_mapping.hpp:276
void resize_file(int fd, size_t new_size)
Definition: file.hpp:96
const T * begin() const
Definition: memory_mapping.hpp:479
size_t m_size
The size of the mapping.
Definition: memory_mapping.hpp:106
const T * cend() const
Definition: memory_mapping.hpp:475
TypedMemoryMapping(size_t size)
Definition: memory_mapping.hpp:345
OSMIUM_DEPRECATED MemoryMapping(size_t size, bool writable=true, int fd=-1, off_t offset=0)
Definition: memory_mapping.hpp:193
T * get_addr() const
Definition: memory_mapping.hpp:286
bool writable() const noexcept
Definition: memory_mapping.hpp:449