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> 150 HANDLE get_handle()
const noexcept;
151 HANDLE osmium::util::MemoryMapping::create_file_mapping()
const noexcept;
152 void* osmium::util::MemoryMapping::map_view_of_file()
const noexcept;
221 }
catch (std::system_error&) {
245 void resize(
size_t new_size);
251 explicit operator bool() const noexcept {
269 int fd() const noexcept {
285 template <
typename T =
void>
288 return reinterpret_cast<T*
>(
m_addr);
290 throw std::runtime_error(
"invalid memory mapping");
318 void resize(
size_t) =
delete;
332 template <
typename T>
360 m_mapping(sizeof(T) * size, mode, fd, sizeof(T) * offset) {
416 m_mapping.
resize(
sizeof(T) * new_size);
423 explicit operator bool() const noexcept {
433 assert(m_mapping.
size() %
sizeof(T) == 0);
434 return m_mapping.
size() /
sizeof(T);
442 int fd() const noexcept {
443 return m_mapping.
fd();
489 template <
typename T>
503 void resize(
size_t) =
delete;
517 #pragma GCC diagnostic push 518 #pragma GCC diagnostic ignored "-Wold-style-cast" 521 return m_addr != MAP_FAILED;
528 #pragma GCC diagnostic pop 531 #ifndef MAP_ANONYMOUS 532 # define MAP_ANONYMOUS MAP_ANON 539 return PROT_READ | PROT_WRITE;
560 throw std::system_error(errno, std::system_category(),
"mmap failed");
570 other.make_invalid();
580 other.make_invalid();
587 throw std::system_error(errno, std::system_category(),
"munmap failed");
594 assert(new_size > 0 &&
"can not resize to zero size");
599 throw std::system_error(errno, std::system_category(),
"mremap failed");
603 assert(
false &&
"can't resize anonymous mappings on non-linux systems");
611 throw std::system_error(errno, std::system_category(),
"mmap (remap) failed");
631 inline DWORD dword_hi(uint64_t x) {
632 return static_cast<DWORD
>(x >> 32);
635 inline DWORD dword_lo(uint64_t x) {
636 return static_cast<DWORD
>(x & 0xffffffff);
646 return PAGE_READONLY;
648 return PAGE_WRITECOPY;
650 return PAGE_READWRITE;
657 return FILE_MAP_READ;
659 return FILE_MAP_COPY;
661 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:218
const T * end() const
Definition: memory_mapping.hpp:483
bool is_valid() const noexcept
Definition: memory_mapping.hpp:520
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
flag_type get_protection() const noexcept
Definition: memory_mapping.hpp:535
MemoryMapping m_mapping
Definition: memory_mapping.hpp:335
MemoryMapping(size_t size, mapping_mode mode, int fd=-1, off_t offset=0)
Definition: memory_mapping.hpp:552
size_t file_size(int fd)
Definition: file.hpp:69
flag_type get_flags() const noexcept
Definition: memory_mapping.hpp:542
int fd() const noexcept
Definition: memory_mapping.hpp:269
static size_t initial_size(size_t size)
Definition: memory_mapping.hpp:142
void unmap()
Definition: memory_mapping.hpp:584
Definition: memory_mapping.hpp:94
mapping_mode
Definition: memory_mapping.hpp:97
void resize(size_t new_size)
Definition: memory_mapping.hpp:415
int flag_type
Definition: memory_mapping.hpp:131
int resize_fd(int fd)
Definition: memory_mapping.hpp:155
T * end()
Definition: memory_mapping.hpp:467
size_t get_pagesize()
Definition: file.hpp:105
void * m_addr
The address where the memory is mapped.
Definition: memory_mapping.hpp:122
const T * cbegin() const
Definition: memory_mapping.hpp:471
off_t m_offset
Offset into the file.
Definition: memory_mapping.hpp:109
#define MAP_ANONYMOUS
Definition: memory_mapping.hpp:532
int m_fd
File handle we got the mapping from.
Definition: memory_mapping.hpp:112
size_t size() const noexcept
Definition: memory_mapping.hpp:260
mapping_mode m_mapping_mode
Mapping mode.
Definition: memory_mapping.hpp:115
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
AnonymousMemoryMapping(size_t size)
Definition: memory_mapping.hpp:309
Definition: memory_mapping.hpp:490
OSMIUM_DEPRECATED TypedMemoryMapping(size_t size, bool writable, int fd, off_t offset=0)
Definition: memory_mapping.hpp:368
TypedMemoryMapping(size_t size, MemoryMapping::mapping_mode mode, int fd, off_t offset=0)
Definition: memory_mapping.hpp:359
size_t size() const noexcept
Definition: memory_mapping.hpp:432
T * begin()
Definition: memory_mapping.hpp:458
Definition: memory_mapping.hpp:305
void make_invalid() noexcept
Definition: memory_mapping.hpp:524
AnonymousTypedMemoryMapping(size_t size)
Definition: memory_mapping.hpp:494
int fd() const noexcept
Definition: memory_mapping.hpp:442
void resize(size_t new_size)
Definition: memory_mapping.hpp:593
MemoryMapping & operator=(const MemoryMapping &)=delete
You can not copy a MemoryMapping.
Definition: memory_mapping.hpp:333
void unmap()
Definition: memory_mapping.hpp:401
bool writable() const noexcept
Definition: memory_mapping.hpp:276
void resize_file(int fd, size_t new_size)
Definition: file.hpp:96
const T * begin() const
Definition: memory_mapping.hpp:479
size_t m_size
The size of the mapping.
Definition: memory_mapping.hpp:106
const T * cend() const
Definition: memory_mapping.hpp:475
TypedMemoryMapping(size_t size)
Definition: memory_mapping.hpp:345
OSMIUM_DEPRECATED MemoryMapping(size_t size, bool writable=true, int fd=-1, off_t offset=0)
Definition: memory_mapping.hpp:193
T * get_addr() const
Definition: memory_mapping.hpp:286
bool writable() const noexcept
Definition: memory_mapping.hpp:449