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;
147 HANDLE osmium::util::MemoryMapping::create_file_mapping()
const noexcept;
148 void* osmium::util::MemoryMapping::map_view_of_file()
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>
356 m_mapping(sizeof(T) * size, mode, 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 535 return PROT_READ | PROT_WRITE;
556 throw std::system_error(errno, std::system_category(),
"mmap failed");
566 other.make_invalid();
576 other.make_invalid();
583 throw std::system_error(errno, std::system_category(),
"munmap failed");
590 assert(new_size > 0 &&
"can not resize to zero size");
595 throw std::system_error(errno, std::system_category(),
"mremap failed");
599 assert(
false &&
"can't resize anonymous mappings on non-linux systems");
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);
642 return PAGE_READONLY;
644 return PAGE_WRITECOPY;
646 return PAGE_READWRITE;
653 return FILE_MAP_READ;
655 return FILE_MAP_COPY;
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 {
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");
710 m_handle(std::move(other.m_handle)),
712 other.make_invalid();
713 other.m_handle =
nullptr;
722 m_handle = std::move(other.m_handle);
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
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
size_t size() const noexcept
Definition: memory_mapping.hpp:256
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
size_t file_size(int fd)
Definition: file.hpp:69
bool writable() const noexcept
Definition: memory_mapping.hpp:445
flag_type get_flags() const noexcept
Definition: memory_mapping.hpp:538
void unmap()
Definition: memory_mapping.hpp:580
Definition: memory_mapping.hpp:94
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
bool writable() const noexcept
Definition: memory_mapping.hpp:272
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
int fd() const noexcept
Definition: memory_mapping.hpp:438
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
int fd() const noexcept
Definition: memory_mapping.hpp:265
T * begin()
Definition: memory_mapping.hpp:454
Definition: memory_mapping.hpp:301
flag_type get_protection() const noexcept
Definition: memory_mapping.hpp:531
void make_invalid() noexcept
Definition: memory_mapping.hpp:520
AnonymousTypedMemoryMapping(size_t size)
Definition: memory_mapping.hpp:490
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
size_t size() const noexcept
Definition: memory_mapping.hpp:428
void unmap()
Definition: memory_mapping.hpp:397
void resize_file(int fd, size_t new_size)
Definition: file.hpp:96
const T * begin() const
Definition: memory_mapping.hpp:475
bool is_valid() const noexcept
Definition: memory_mapping.hpp:516
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