1 #ifndef OSMIUM_UTIL_MEMORY_MAPPING_HPP
2 #define OSMIUM_UTIL_MEMORY_MAPPING_HPP
39 #include <system_error>
44 # include <sys/mman.h>
49 # include <sys/types.h>
149 HANDLE get_handle() const noexcept;
215 }
catch (std::system_error&) {
238 void resize(
size_t new_size);
244 operator bool() const noexcept {
262 int fd() const noexcept {
278 template <
typename T =
void>
281 return reinterpret_cast<T*
>(
m_addr);
283 throw std::runtime_error(
"invalid memory mapping");
311 void resize(
size_t) =
delete;
325 template <
typename T>
339 m_mapping(sizeof(T) * size,
MemoryMapping::mapping_mode::write_private) {
353 m_mapping(sizeof(T) * size, mode, fd, sizeof(T) * offset) {
358 m_mapping(sizeof(T) * size, writable ?
MemoryMapping::mapping_mode::write_shared :
MemoryMapping::mapping_mode::readonly, fd, sizeof(T) * offset) {
405 m_mapping.
resize(
sizeof(T) * new_size);
412 operator bool() const noexcept {
422 assert(m_mapping.
size() %
sizeof(T) == 0);
423 return m_mapping.
size() /
sizeof(T);
431 int fd() const noexcept {
432 return m_mapping.
fd();
478 template <
typename T>
492 void resize(
size_t) =
delete;
506 #pragma GCC diagnostic push
507 #pragma GCC diagnostic ignored "-Wold-style-cast"
510 return m_addr != MAP_FAILED;
517 #pragma GCC diagnostic pop
520 #ifndef MAP_ANONYMOUS
521 # define MAP_ANONYMOUS MAP_ANON
525 if (m_mapping_mode == mapping_mode::readonly) {
528 return PROT_READ | PROT_WRITE;
535 if (m_mapping_mode == mapping_mode::write_shared) {
542 m_size(initial_size(size)),
545 m_mapping_mode(mode),
546 m_addr(::mmap(nullptr, m_size, get_protection(), get_flags(), m_fd, m_offset)) {
549 throw std::system_error(errno, std::system_category(),
"mmap failed");
554 m_size(other.m_size),
555 m_offset(other.m_offset),
557 m_mapping_mode(other.m_mapping_mode),
558 m_addr(other.m_addr) {
559 other.make_invalid();
564 m_size = other.m_size;
565 m_offset = other.m_offset;
567 m_mapping_mode = other.m_mapping_mode;
568 m_addr = other.m_addr;
569 other.make_invalid();
575 if (::munmap(m_addr, m_size) != 0) {
576 throw std::system_error(errno, std::system_category(),
"munmap failed");
583 assert(new_size > 0 &&
"can not resize to zero size");
586 m_addr = ::mremap(m_addr, m_size, new_size, MREMAP_MAYMOVE);
588 throw std::system_error(errno, std::system_category(),
"mremap failed");
592 assert(
false &&
"can't resize anonymous mappings on non-linux systems");
598 m_addr = ::mmap(
nullptr, new_size, get_protection(), get_flags(), m_fd, m_offset);
600 throw std::system_error(errno, std::system_category(),
"mmap (remap) failed");
620 inline DWORD dword_hi(uint64_t x) {
621 return static_cast<DWORD
>(x >> 32);
624 inline DWORD dword_lo(uint64_t x) {
625 return static_cast<DWORD
>(x & 0xffffffff);
633 switch (m_mapping_mode) {
634 case mapping_mode::readonly:
635 return PAGE_READONLY;
636 case mapping_mode::write_private:
637 return PAGE_WRITECOPY;
638 case mapping_mode::write_shared:
639 return PAGE_READWRITE;
644 switch (m_mapping_mode) {
645 case mapping_mode::readonly:
646 return FILE_MAP_READ;
647 case mapping_mode::write_private:
648 return FILE_MAP_COPY;
649 case mapping_mode::write_shared:
650 return FILE_MAP_WRITE;
654 inline HANDLE osmium::util::MemoryMapping::get_handle() const noexcept {
656 return INVALID_HANDLE_VALUE;
658 return reinterpret_cast<HANDLE
>(_get_osfhandle(m_fd));
661 inline HANDLE osmium::util::MemoryMapping::create_file_mapping() const noexcept {
663 _setmode(m_fd, _O_BINARY);
665 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);
668 inline void* osmium::util::MemoryMapping::map_view_of_file() const noexcept {
669 return MapViewOfFile(m_handle, get_flags(), osmium::util::dword_hi(m_offset), osmium::util::dword_lo(m_offset), m_size);
673 return m_addr !=
nullptr;
681 m_size(initial_size(size)),
684 m_mapping_mode(mode),
685 m_handle(create_file_mapping()),
689 throw std::system_error(GetLastError(), std::system_category(),
"CreateFileMapping failed");
692 m_addr = map_view_of_file();
694 throw std::system_error(GetLastError(), std::system_category(),
"MapViewOfFile failed");
699 m_size(other.m_size),
700 m_offset(other.m_offset),
702 m_mapping_mode(other.m_mapping_mode),
703 m_handle(
std::move(other.m_handle)),
704 m_addr(other.m_addr) {
705 other.make_invalid();
706 other.m_handle =
nullptr;
711 m_size = other.m_size;
712 m_offset = other.m_offset;
714 m_mapping_mode = other.m_mapping_mode;
715 m_handle = std::move(other.m_handle);
716 m_addr = other.m_addr;
717 other.make_invalid();
718 other.m_handle =
nullptr;
724 if (! UnmapViewOfFile(m_addr)) {
725 throw std::system_error(GetLastError(), std::system_category(),
"UnmapViewOfFile failed");
731 if (! CloseHandle(m_handle)) {
732 throw std::system_error(GetLastError(), std::system_category(),
"CloseHandle failed");
744 m_handle = create_file_mapping();
746 throw std::system_error(GetLastError(), std::system_category(),
"CreateFileMapping failed");
749 m_addr = map_view_of_file();
751 throw std::system_error(GetLastError(), std::system_category(),
"MapViewOfFile failed");
757 #endif // OSMIUM_UTIL_MEMORY_MAPPING_HPP
~MemoryMapping() noexcept
Definition: memory_mapping.hpp:212
const T * end() const
Definition: memory_mapping.hpp:472
bool is_valid() const noexcept
Definition: memory_mapping.hpp:509
flag_type get_protection() const noexcept
Definition: memory_mapping.hpp:524
MemoryMapping m_mapping
Definition: memory_mapping.hpp:328
MemoryMapping(size_t size, mapping_mode mode, int fd=-1, off_t offset=0)
Definition: memory_mapping.hpp:541
size_t file_size(int fd)
Definition: file.hpp:67
flag_type get_flags() const noexcept
Definition: memory_mapping.hpp:531
int fd() const noexcept
Definition: memory_mapping.hpp:262
TypedMemoryMapping(size_t size, bool writable, int fd, off_t offset=0)
DEPRECATED: For backwards compatibility.
Definition: memory_mapping.hpp:357
static size_t initial_size(size_t size)
Definition: memory_mapping.hpp:141
void unmap()
Definition: memory_mapping.hpp:573
Definition: memory_mapping.hpp:93
Definition: reader_iterator.hpp:39
mapping_mode
Definition: memory_mapping.hpp:96
void resize(size_t new_size)
Definition: memory_mapping.hpp:404
int flag_type
Definition: memory_mapping.hpp:130
int resize_fd(int fd)
Definition: memory_mapping.hpp:154
T * end()
Definition: memory_mapping.hpp:456
~TypedMemoryMapping()=default
size_t get_pagesize()
Definition: file.hpp:103
void * m_addr
The address where the memory is mapped.
Definition: memory_mapping.hpp:121
const T * cbegin() const
Definition: memory_mapping.hpp:460
off_t m_offset
Offset into the file.
Definition: memory_mapping.hpp:108
#define MAP_ANONYMOUS
Definition: memory_mapping.hpp:521
int m_fd
File handle we got the mapping from.
Definition: memory_mapping.hpp:111
size_t size() const noexcept
Definition: memory_mapping.hpp:253
mapping_mode m_mapping_mode
Mapping mode.
Definition: memory_mapping.hpp:114
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
AnonymousMemoryMapping(size_t size)
Definition: memory_mapping.hpp:302
Definition: memory_mapping.hpp:479
TypedMemoryMapping(size_t size, MemoryMapping::mapping_mode mode, int fd, off_t offset=0)
Definition: memory_mapping.hpp:352
size_t size() const noexcept
Definition: memory_mapping.hpp:421
T * begin()
Definition: memory_mapping.hpp:447
Definition: memory_mapping.hpp:298
void make_invalid() noexcept
Definition: memory_mapping.hpp:513
MemoryMapping(size_t size, bool writable=true, int fd=-1, off_t offset=0)
DEPRECATED: For backwards compatibility.
Definition: memory_mapping.hpp:187
AnonymousTypedMemoryMapping(size_t size)
Definition: memory_mapping.hpp:483
int fd() const noexcept
Definition: memory_mapping.hpp:431
void resize(size_t new_size)
Definition: memory_mapping.hpp:582
MemoryMapping & operator=(const MemoryMapping &)=delete
You can not copy a MemoryMapping.
Definition: memory_mapping.hpp:326
TypedMemoryMapping & operator=(const TypedMemoryMapping &)=delete
You can not copy a MemoryMapping.
void unmap()
Definition: memory_mapping.hpp:390
bool writable() const noexcept
Definition: memory_mapping.hpp:269
void resize_file(int fd, size_t new_size)
Definition: file.hpp:94
const T * begin() const
Definition: memory_mapping.hpp:468
size_t m_size
The size of the mapping.
Definition: memory_mapping.hpp:105
const T * cend() const
Definition: memory_mapping.hpp:464
TypedMemoryMapping(size_t size)
Definition: memory_mapping.hpp:338
T * get_addr() const
Definition: memory_mapping.hpp:279
bool writable() const noexcept
Definition: memory_mapping.hpp:438