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>
131 using flag_type = int;
140 throw std::runtime_error(
"Zero-sized mapping is not allowed.");
146 HANDLE get_handle() const noexcept;
217 }
catch (std::system_error&) {
241 void resize(
size_t new_size);
247 explicit operator bool() const noexcept {
265 int fd() const noexcept {
281 template <
typename T =
void>
284 return reinterpret_cast<T*
>(
m_addr);
286 throw std::runtime_error(
"invalid memory mapping");
314 void resize(
size_t) =
delete;
328 template <
typename T>
342 m_mapping(sizeof(T) * size,
MemoryMapping::mapping_mode::write_private) {
356 m_mapping(sizeof(T) * size, mode, fd, sizeof(T) * offset) {
365 m_mapping(sizeof(T) * size, writable ?
MemoryMapping::mapping_mode::write_shared :
MemoryMapping::mapping_mode::readonly, fd, sizeof(T) * offset) {
412 m_mapping.
resize(
sizeof(T) * new_size);
419 explicit operator bool() const noexcept {
429 assert(m_mapping.
size() %
sizeof(T) == 0);
430 return m_mapping.
size() /
sizeof(T);
438 int fd() const noexcept {
439 return m_mapping.
fd();
485 template <
typename T>
499 void resize(
size_t) =
delete;
513 #pragma GCC diagnostic push
514 #pragma GCC diagnostic ignored "-Wold-style-cast"
517 return m_addr != MAP_FAILED;
524 #pragma GCC diagnostic pop
527 #ifndef MAP_ANONYMOUS
528 # define MAP_ANONYMOUS MAP_ANON
532 if (m_mapping_mode == mapping_mode::readonly) {
535 return PROT_READ | PROT_WRITE;
542 if (m_mapping_mode == mapping_mode::write_shared) {
549 m_size(check_size(size)),
552 m_mapping_mode(mode),
553 m_addr(::mmap(nullptr, m_size, get_protection(), get_flags(), m_fd, m_offset)) {
556 throw std::system_error(errno, std::system_category(),
"mmap failed");
561 m_size(other.m_size),
562 m_offset(other.m_offset),
564 m_mapping_mode(other.m_mapping_mode),
565 m_addr(other.m_addr) {
566 other.make_invalid();
571 m_size = other.m_size;
572 m_offset = other.m_offset;
574 m_mapping_mode = other.m_mapping_mode;
575 m_addr = other.m_addr;
576 other.make_invalid();
582 if (::munmap(m_addr, m_size) != 0) {
583 throw std::system_error(errno, std::system_category(),
"munmap failed");
590 assert(new_size > 0 &&
"can not resize to zero size");
593 m_addr = ::mremap(m_addr, m_size, new_size, MREMAP_MAYMOVE);
595 throw std::system_error(errno, std::system_category(),
"mremap failed");
599 assert(
false &&
"can't resize anonymous mappings on non-linux systems");
605 m_addr = ::mmap(
nullptr, new_size, get_protection(), get_flags(), m_fd, m_offset);
607 throw std::system_error(errno, std::system_category(),
"mmap (remap) failed");
627 inline DWORD dword_hi(uint64_t x) {
628 return static_cast<DWORD
>(x >> 32);
631 inline DWORD dword_lo(uint64_t x) {
632 return static_cast<DWORD
>(x & 0xffffffff);
640 switch (m_mapping_mode) {
641 case mapping_mode::readonly:
642 return PAGE_READONLY;
643 case mapping_mode::write_private:
644 return PAGE_WRITECOPY;
645 case mapping_mode::write_shared:
646 return PAGE_READWRITE;
651 switch (m_mapping_mode) {
652 case mapping_mode::readonly:
653 return FILE_MAP_READ;
654 case mapping_mode::write_private:
655 return FILE_MAP_COPY;
656 case mapping_mode::write_shared:
657 return FILE_MAP_WRITE;
661 inline HANDLE osmium::util::MemoryMapping::get_handle() const noexcept {
663 return INVALID_HANDLE_VALUE;
665 return reinterpret_cast<HANDLE
>(_get_osfhandle(m_fd));
668 inline HANDLE osmium::util::MemoryMapping::create_file_mapping() const noexcept {
670 _setmode(m_fd, _O_BINARY);
672 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);
675 inline void* osmium::util::MemoryMapping::map_view_of_file() const noexcept {
676 return MapViewOfFile(m_handle, get_flags(), osmium::util::dword_hi(m_offset), osmium::util::dword_lo(m_offset), m_size);
680 return m_addr !=
nullptr;
688 m_size(check_size(size)),
691 m_mapping_mode(mode),
692 m_handle(create_file_mapping()),
696 throw std::system_error(GetLastError(), std::system_category(),
"CreateFileMapping failed");
699 m_addr = map_view_of_file();
701 throw std::system_error(GetLastError(), std::system_category(),
"MapViewOfFile failed");
706 m_size(other.m_size),
707 m_offset(other.m_offset),
709 m_mapping_mode(other.m_mapping_mode),
710 m_handle(
std::move(other.m_handle)),
711 m_addr(other.m_addr) {
712 other.make_invalid();
713 other.m_handle =
nullptr;
718 m_size = other.m_size;
719 m_offset = other.m_offset;
721 m_mapping_mode = other.m_mapping_mode;
722 m_handle = std::move(other.m_handle);
723 m_addr = other.m_addr;
724 other.make_invalid();
725 other.m_handle =
nullptr;
731 if (! UnmapViewOfFile(m_addr)) {
732 throw std::system_error(GetLastError(), std::system_category(),
"UnmapViewOfFile failed");
738 if (! CloseHandle(m_handle)) {
739 throw std::system_error(GetLastError(), std::system_category(),
"CloseHandle failed");
751 m_handle = create_file_mapping();
753 throw std::system_error(GetLastError(), std::system_category(),
"CreateFileMapping failed");
756 m_addr = map_view_of_file();
758 throw std::system_error(GetLastError(), std::system_category(),
"MapViewOfFile failed");
764 #endif // OSMIUM_UTIL_MEMORY_MAPPING_HPP
~MemoryMapping() noexcept
Definition: memory_mapping.hpp:214
const T * end() const
Definition: memory_mapping.hpp:479
bool is_valid() const noexcept
Definition: memory_mapping.hpp:516
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
flag_type get_protection() const noexcept
Definition: memory_mapping.hpp:531
MemoryMapping m_mapping
Definition: memory_mapping.hpp:331
int flag_type
Definition: memory_mapping.hpp:131
MemoryMapping(size_t size, mapping_mode mode, int fd=-1, off_t offset=0)
Definition: memory_mapping.hpp:548
~TypedMemoryMapping() noexcept=default
size_t file_size(int fd)
Definition: file.hpp:69
flag_type get_flags() const noexcept
Definition: memory_mapping.hpp:538
int fd() const noexcept
Definition: memory_mapping.hpp:265
void unmap()
Definition: memory_mapping.hpp:580
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:411
static size_t check_size(size_t size)
Definition: memory_mapping.hpp:138
int resize_fd(int fd)
Definition: memory_mapping.hpp:151
T * end()
Definition: memory_mapping.hpp:463
void * m_addr
The address where the memory is mapped.
Definition: memory_mapping.hpp:122
const T * cbegin() const
Definition: memory_mapping.hpp:467
off_t m_offset
Offset into the file.
Definition: memory_mapping.hpp:109
#define MAP_ANONYMOUS
Definition: memory_mapping.hpp:528
int m_fd
File handle we got the mapping from.
Definition: memory_mapping.hpp:112
size_t size() const noexcept
Definition: memory_mapping.hpp:256
mapping_mode m_mapping_mode
Mapping mode.
Definition: memory_mapping.hpp:115
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
AnonymousMemoryMapping(size_t size)
Definition: memory_mapping.hpp:305
Definition: memory_mapping.hpp:486
OSMIUM_DEPRECATED TypedMemoryMapping(size_t size, bool writable, int fd, off_t offset=0)
Definition: memory_mapping.hpp:364
TypedMemoryMapping(size_t size, MemoryMapping::mapping_mode mode, int fd, off_t offset=0)
Definition: memory_mapping.hpp:355
size_t size() const noexcept
Definition: memory_mapping.hpp:428
T * begin()
Definition: memory_mapping.hpp:454
Definition: memory_mapping.hpp:301
void make_invalid() noexcept
Definition: memory_mapping.hpp:520
AnonymousTypedMemoryMapping(size_t size)
Definition: memory_mapping.hpp:490
int fd() const noexcept
Definition: memory_mapping.hpp:438
void resize(size_t new_size)
Definition: memory_mapping.hpp:589
MemoryMapping & operator=(const MemoryMapping &)=delete
You can not copy a MemoryMapping.
Definition: memory_mapping.hpp:329
TypedMemoryMapping & operator=(const TypedMemoryMapping &)=delete
You can not copy a MemoryMapping.
void unmap()
Definition: memory_mapping.hpp:397
bool writable() const noexcept
Definition: memory_mapping.hpp:272
void resize_file(int fd, size_t new_size)
Definition: file.hpp:96
const T * begin() const
Definition: memory_mapping.hpp:475
size_t m_size
The size of the mapping.
Definition: memory_mapping.hpp:106
const T * cend() const
Definition: memory_mapping.hpp:471
TypedMemoryMapping(size_t size)
Definition: memory_mapping.hpp:341
OSMIUM_DEPRECATED MemoryMapping(size_t size, bool writable=true, int fd=-1, off_t offset=0)
Definition: memory_mapping.hpp:189
T * get_addr() const
Definition: memory_mapping.hpp:282
bool writable() const noexcept
Definition: memory_mapping.hpp:445