1 #ifndef OSMIUM_UTIL_MEMORY_MAPPING_HPP 2 #define OSMIUM_UTIL_MEMORY_MAPPING_HPP 43 #include <system_error> 46 # include <sys/mman.h> 51 # include <sys/types.h> 56 inline namespace util {
148 HANDLE get_handle() const noexcept;
149 HANDLE create_file_mapping() const noexcept;
150 void* map_view_of_file() const noexcept;
219 }
catch (
const std::system_error&) {
243 void resize(std::size_t new_size);
249 explicit operator bool() const noexcept {
258 std::size_t
size() const noexcept {
267 int fd() const noexcept {
283 template <
typename T =
void>
286 return reinterpret_cast<T*
>(
m_addr);
288 throw std::runtime_error{
"invalid memory mapping"};
316 void resize(std::size_t) =
delete;
330 template <
typename T>
370 sizeof(T) * offset) {
424 explicit operator bool() const noexcept {
433 std::size_t
size() const noexcept {
443 int fd() const noexcept {
490 template <
typename T>
504 void resize(std::size_t) =
delete;
518 #pragma GCC diagnostic push 519 #pragma GCC diagnostic ignored "-Wold-style-cast" 522 return m_addr != MAP_FAILED;
529 #pragma GCC diagnostic pop 532 #ifndef MAP_ANONYMOUS 533 # define MAP_ANONYMOUS MAP_ANON 537 if (m_mapping_mode == mapping_mode::readonly) {
540 return PROT_READ | PROT_WRITE;
547 if (m_mapping_mode == mapping_mode::write_shared) {
554 m_size(check_size(size)),
557 m_mapping_mode(mode),
558 m_addr(::mmap(nullptr, m_size, get_protection(), get_flags(), m_fd, m_offset)) {
561 throw std::system_error{errno, std::system_category(),
"mmap failed"};
566 m_size(other.m_size),
567 m_offset(other.m_offset),
569 m_mapping_mode(other.m_mapping_mode),
570 m_addr(other.m_addr) {
571 other.make_invalid();
576 m_size = other.m_size;
577 m_offset = other.m_offset;
579 m_mapping_mode = other.m_mapping_mode;
580 m_addr = other.m_addr;
581 other.make_invalid();
587 if (::munmap(m_addr, m_size) != 0) {
588 throw std::system_error{errno, std::system_category(),
"munmap failed"};
595 assert(new_size > 0 &&
"can not resize to zero size");
598 m_addr = ::mremap(m_addr, m_size, new_size, MREMAP_MAYMOVE);
600 throw std::system_error{errno, std::system_category(),
"mremap failed"};
604 assert(
false &&
"can't resize anonymous mappings on non-linux systems");
610 m_addr = ::mmap(
nullptr, new_size, get_protection(), get_flags(), m_fd, m_offset);
612 throw std::system_error{errno, std::system_category(),
"mmap (remap) failed"};
630 inline namespace util {
632 inline DWORD dword_hi(uint64_t x) {
633 return static_cast<DWORD
>(x >> 32);
636 inline DWORD dword_lo(uint64_t x) {
637 return static_cast<DWORD
>(x & 0xffffffff);
645 switch (m_mapping_mode) {
646 case mapping_mode::readonly:
647 return PAGE_READONLY;
648 case mapping_mode::write_private:
649 return PAGE_WRITECOPY;
653 return PAGE_READWRITE;
657 switch (m_mapping_mode) {
658 case mapping_mode::readonly:
659 return FILE_MAP_READ;
660 case mapping_mode::write_private:
661 return FILE_MAP_COPY;
665 return FILE_MAP_WRITE;
668 inline HANDLE osmium::util::MemoryMapping::get_handle() const noexcept {
670 return INVALID_HANDLE_VALUE;
672 return reinterpret_cast<HANDLE
>(_get_osfhandle(m_fd));
675 inline HANDLE osmium::util::MemoryMapping::create_file_mapping() const noexcept {
677 _setmode(m_fd, _O_BINARY);
679 return CreateFileMapping(get_handle(),
682 osmium::dword_hi(static_cast<uint64_t>(m_size) + m_offset),
683 osmium::dword_lo(static_cast<uint64_t>(m_size) + m_offset),
687 inline void* osmium::util::MemoryMapping::map_view_of_file() const noexcept {
688 return MapViewOfFile(m_handle,
690 osmium::dword_hi(m_offset),
691 osmium::dword_lo(m_offset),
696 return m_addr !=
nullptr;
706 inline int last_error() noexcept {
707 return static_cast<int>(GetLastError());
711 m_size(check_size(size)),
714 m_mapping_mode(mode),
715 m_handle(create_file_mapping()),
719 throw std::system_error{last_error(), std::system_category(),
"CreateFileMapping failed"};
722 m_addr = map_view_of_file();
724 throw std::system_error{last_error(), std::system_category(),
"MapViewOfFile failed"};
729 m_size(other.m_size),
730 m_offset(other.m_offset),
732 m_mapping_mode(other.m_mapping_mode),
733 m_handle(std::move(other.m_handle)),
734 m_addr(other.m_addr) {
735 other.make_invalid();
736 other.m_handle =
nullptr;
741 m_size = other.m_size;
742 m_offset = other.m_offset;
744 m_mapping_mode = other.m_mapping_mode;
745 m_handle = std::move(other.m_handle);
746 m_addr = other.m_addr;
747 other.make_invalid();
748 other.m_handle =
nullptr;
754 if (!UnmapViewOfFile(m_addr)) {
755 throw std::system_error{last_error(), std::system_category(),
"UnmapViewOfFile failed"};
761 if (!CloseHandle(m_handle)) {
762 throw std::system_error{last_error(), std::system_category(),
"CloseHandle failed"};
774 m_handle = create_file_mapping();
776 throw std::system_error{last_error(), std::system_category(),
"CreateFileMapping failed"};
779 m_addr = map_view_of_file();
781 throw std::system_error{last_error(), std::system_category(),
"MapViewOfFile failed"};
787 #endif // OSMIUM_UTIL_MEMORY_MAPPING_HPP ~MemoryMapping() noexcept
Definition: memory_mapping.hpp:216
bool is_valid() const noexcept
Definition: memory_mapping.hpp:521
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
OSMIUM_DEPRECATED TypedMemoryMapping(std::size_t size, bool writable, int fd, off_t offset=0)
Definition: memory_mapping.hpp:366
flag_type get_protection() const noexcept
Definition: memory_mapping.hpp:536
MemoryMapping m_mapping
Definition: memory_mapping.hpp:333
int flag_type
Definition: memory_mapping.hpp:133
~TypedMemoryMapping() noexcept=default
OSMIUM_DEPRECATED MemoryMapping(std::size_t size, bool writable=true, int fd=-1, off_t offset=0)
Definition: memory_mapping.hpp:191
std::size_t file_size(int fd)
Definition: file.hpp:109
const T * begin() const
Definition: memory_mapping.hpp:480
flag_type get_flags() const noexcept
Definition: memory_mapping.hpp:543
int fd() const noexcept
Definition: memory_mapping.hpp:267
void unmap()
Definition: memory_mapping.hpp:585
Definition: memory_mapping.hpp:95
Definition: location.hpp:550
mapping_mode
Definition: memory_mapping.hpp:99
int resize_fd(int fd)
Definition: memory_mapping.hpp:153
std::size_t size() const noexcept
Definition: memory_mapping.hpp:433
T * end()
Definition: memory_mapping.hpp:468
void * m_addr
The address where the memory is mapped.
Definition: memory_mapping.hpp:124
off_t m_offset
Offset into the file.
Definition: memory_mapping.hpp:111
#define MAP_ANONYMOUS
Definition: memory_mapping.hpp:533
int m_fd
File handle we got the mapping from.
Definition: memory_mapping.hpp:114
mapping_mode m_mapping_mode
Mapping mode.
Definition: memory_mapping.hpp:117
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: memory_mapping.hpp:491
std::size_t get_pagesize()
Definition: file.hpp:193
T * begin()
Definition: memory_mapping.hpp:459
Definition: memory_mapping.hpp:303
TypedMemoryMapping(std::size_t size)
Definition: memory_mapping.hpp:343
void make_invalid() noexcept
Definition: memory_mapping.hpp:525
std::size_t m_size
The size of the mapping.
Definition: memory_mapping.hpp:108
const T * cbegin() const
Definition: memory_mapping.hpp:472
int fd() const noexcept
Definition: memory_mapping.hpp:443
MemoryMapping & operator=(const MemoryMapping &)=delete
You can not copy a MemoryMapping.
Definition: memory_mapping.hpp:331
TypedMemoryMapping & operator=(const TypedMemoryMapping &)=delete
You can not copy a TypedMemoryMapping.
const T * end() const
Definition: memory_mapping.hpp:484
void unmap()
Definition: memory_mapping.hpp:402
MemoryMapping(std::size_t size, mapping_mode mode, int fd=-1, off_t offset=0)
Definition: memory_mapping.hpp:553
bool writable() const noexcept
Definition: memory_mapping.hpp:274
AnonymousTypedMemoryMapping(std::size_t size)
Definition: memory_mapping.hpp:495
AnonymousMemoryMapping(std::size_t size)
Definition: memory_mapping.hpp:307
void resize_file(int fd, std::size_t new_size)
Definition: file.hpp:176
std::size_t size() const noexcept
Definition: memory_mapping.hpp:258
void resize(std::size_t new_size)
Definition: memory_mapping.hpp:594
static std::size_t check_size(std::size_t size)
Definition: memory_mapping.hpp:140
TypedMemoryMapping(std::size_t size, MemoryMapping::mapping_mode mode, int fd, off_t offset=0)
Definition: memory_mapping.hpp:357
T * get_addr() const
Definition: memory_mapping.hpp:284
const T * cend() const
Definition: memory_mapping.hpp:476
void resize(std::size_t new_size)
Definition: memory_mapping.hpp:416
bool writable() const noexcept
Definition: memory_mapping.hpp:450