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> 133 using flag_type = int;
142 throw std::runtime_error(
"Zero-sized mapping is not allowed.");
148 HANDLE get_handle()
const noexcept;
149 HANDLE osmium::util::MemoryMapping::create_file_mapping()
const noexcept;
150 void* osmium::util::MemoryMapping::map_view_of_file()
const noexcept;
219 }
catch (
const std::system_error&) {
243 void resize(
size_t new_size);
249 explicit operator bool() 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(
size_t) =
delete;
330 template <
typename T>
358 m_mapping(sizeof(T) * size, mode, fd, sizeof(T) * offset) {
414 m_mapping.
resize(
sizeof(T) * new_size);
421 explicit operator bool() const noexcept {
431 assert(m_mapping.
size() %
sizeof(T) == 0);
432 return m_mapping.
size() /
sizeof(T);
440 int fd() const noexcept {
441 return m_mapping.
fd();
487 template <
typename T>
501 void resize(
size_t) =
delete;
515 #pragma GCC diagnostic push 516 #pragma GCC diagnostic ignored "-Wold-style-cast" 519 return m_addr != MAP_FAILED;
526 #pragma GCC diagnostic pop 529 #ifndef MAP_ANONYMOUS 530 # define MAP_ANONYMOUS MAP_ANON 537 return PROT_READ | PROT_WRITE;
558 throw std::system_error(errno, std::system_category(),
"mmap failed");
568 other.make_invalid();
578 other.make_invalid();
585 throw std::system_error(errno, std::system_category(),
"munmap failed");
592 assert(new_size > 0 &&
"can not resize to zero size");
597 throw std::system_error(errno, std::system_category(),
"mremap failed");
601 assert(
false &&
"can't resize anonymous mappings on non-linux systems");
609 throw std::system_error(errno, std::system_category(),
"mmap (remap) failed");
629 inline DWORD dword_hi(uint64_t x) {
630 return static_cast<DWORD
>(x >> 32);
633 inline DWORD dword_lo(uint64_t x) {
634 return static_cast<DWORD
>(x & 0xffffffff);
644 return PAGE_READONLY;
646 return PAGE_WRITECOPY;
650 return PAGE_READWRITE;
656 return FILE_MAP_READ;
658 return FILE_MAP_COPY;
662 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 {
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");
714 m_handle(std::move(other.m_handle)),
716 other.make_invalid();
717 other.m_handle =
nullptr;
726 m_handle = std::move(other.m_handle);
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:216
bool is_valid() const noexcept
Definition: memory_mapping.hpp:518
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
flag_type get_protection() const noexcept
Definition: memory_mapping.hpp:533
MemoryMapping m_mapping
Definition: memory_mapping.hpp:333
int flag_type
Definition: memory_mapping.hpp:133
MemoryMapping(size_t size, mapping_mode mode, int fd=-1, off_t offset=0)
Definition: memory_mapping.hpp:550
size_t file_size(int fd)
Definition: file.hpp:70
const T * begin() const
Definition: memory_mapping.hpp:477
flag_type get_flags() const noexcept
Definition: memory_mapping.hpp:540
int fd() const noexcept
Definition: memory_mapping.hpp:267
void unmap()
Definition: memory_mapping.hpp:582
Definition: memory_mapping.hpp:95
mapping_mode
Definition: memory_mapping.hpp:99
void resize(size_t new_size)
Definition: memory_mapping.hpp:413
static size_t check_size(size_t size)
Definition: memory_mapping.hpp:140
int resize_fd(int fd)
Definition: memory_mapping.hpp:153
T * end()
Definition: memory_mapping.hpp:465
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:530
int m_fd
File handle we got the mapping from.
Definition: memory_mapping.hpp:114
size_t size() const noexcept
Definition: memory_mapping.hpp:258
mapping_mode m_mapping_mode
Mapping mode.
Definition: memory_mapping.hpp:117
Namespace for everything in the Osmium library.
Definition: assembler.hpp:63
AnonymousMemoryMapping(size_t size)
Definition: memory_mapping.hpp:307
Definition: memory_mapping.hpp:488
OSMIUM_DEPRECATED TypedMemoryMapping(size_t size, bool writable, int fd, off_t offset=0)
Definition: memory_mapping.hpp:366
TypedMemoryMapping(size_t size, MemoryMapping::mapping_mode mode, int fd, off_t offset=0)
Definition: memory_mapping.hpp:357
size_t size() const noexcept
Definition: memory_mapping.hpp:430
T * begin()
Definition: memory_mapping.hpp:456
Definition: memory_mapping.hpp:303
void make_invalid() noexcept
Definition: memory_mapping.hpp:522
const T * cbegin() const
Definition: memory_mapping.hpp:469
AnonymousTypedMemoryMapping(size_t size)
Definition: memory_mapping.hpp:492
int fd() const noexcept
Definition: memory_mapping.hpp:440
void resize(size_t new_size)
Definition: memory_mapping.hpp:591
MemoryMapping & operator=(const MemoryMapping &)=delete
You can not copy a MemoryMapping.
Definition: memory_mapping.hpp:331
const T * end() const
Definition: memory_mapping.hpp:481
void unmap()
Definition: memory_mapping.hpp:399
bool writable() const noexcept
Definition: memory_mapping.hpp:274
void resize_file(int fd, size_t new_size)
Definition: file.hpp:135
size_t m_size
The size of the mapping.
Definition: memory_mapping.hpp:108
T * get_addr() const
Definition: memory_mapping.hpp:284
TypedMemoryMapping(size_t size)
Definition: memory_mapping.hpp:343
OSMIUM_DEPRECATED MemoryMapping(size_t size, bool writable=true, int fd=-1, off_t offset=0)
Definition: memory_mapping.hpp:191
const T * cend() const
Definition: memory_mapping.hpp:473
bool writable() const noexcept
Definition: memory_mapping.hpp:447