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 {
133 using flag_type = int;
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>
358 m_mapping(sizeof(T) * size, mode, fd, sizeof(T) * offset) {
367 m_mapping(sizeof(T) * size,
370 sizeof(T) * offset) {
417 m_mapping.
resize(
sizeof(T) * new_size);
424 explicit operator bool() const noexcept {
433 std::size_t
size() const noexcept {
434 assert(m_mapping.
size() %
sizeof(T) == 0);
435 return m_mapping.
size() /
sizeof(T);
443 int fd() const noexcept {
444 return m_mapping.
fd();
490 template <
typename T>
504 void resize(std::size_t) =
delete;
518 #pragma GCC diagnostic push 519 #pragma GCC diagnostic ignored "-Wold-style-cast" 521 inline bool osmium::MemoryMapping::is_valid() const noexcept {
522 return m_addr != MAP_FAILED;
525 inline void osmium::MemoryMapping::make_invalid() noexcept {
529 #pragma GCC diagnostic pop 532 #ifndef MAP_ANONYMOUS 533 # define MAP_ANONYMOUS MAP_ANON 536 inline int osmium::MemoryMapping::get_protection() const noexcept {
540 return PROT_READ | PROT_WRITE;
543 inline int osmium::MemoryMapping::get_flags() const noexcept {
553 inline osmium::MemoryMapping::MemoryMapping(std::size_t
size,
mapping_mode mode,
int fd, off_t offset) :
561 throw std::system_error{errno, std::system_category(),
"mmap failed"};
565 inline osmium::MemoryMapping::MemoryMapping(
MemoryMapping&& other) noexcept :
571 other.make_invalid();
574 inline osmium::MemoryMapping& osmium::MemoryMapping::operator=(osmium::MemoryMapping&& other) noexcept {
581 other.make_invalid();
585 inline void osmium::MemoryMapping::unmap() {
588 throw std::system_error{errno, std::system_category(),
"munmap failed"};
594 inline void osmium::MemoryMapping::resize(std::size_t new_size) {
595 assert(new_size > 0 &&
"can not resize to zero size");
600 throw std::system_error{errno, std::system_category(),
"mremap failed"};
604 assert(
false &&
"can't resize anonymous mappings on non-linux systems");
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);
644 inline DWORD osmium::MemoryMapping::get_protection()
const noexcept {
647 return PAGE_READONLY;
649 return PAGE_WRITECOPY;
653 return PAGE_READWRITE;
656 inline DWORD osmium::MemoryMapping::get_flags()
const noexcept {
659 return FILE_MAP_READ;
661 return FILE_MAP_COPY;
665 return FILE_MAP_WRITE;
668 inline HANDLE osmium::MemoryMapping::get_handle()
const noexcept {
670 return INVALID_HANDLE_VALUE;
672 return reinterpret_cast<HANDLE
>(_get_osfhandle(
m_fd));
675 inline HANDLE osmium::MemoryMapping::create_file_mapping()
const noexcept {
677 _setmode(
m_fd, _O_BINARY);
679 return CreateFileMapping(get_handle(),
687 inline void* osmium::MemoryMapping::map_view_of_file()
const noexcept {
688 return MapViewOfFile(m_handle,
695 inline bool osmium::MemoryMapping::is_valid()
const noexcept {
699 inline void osmium::MemoryMapping::make_invalid() noexcept {
706 inline int last_error() noexcept {
707 return static_cast<int>(GetLastError());
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"};
728 inline osmium::MemoryMapping::MemoryMapping(
MemoryMapping&& other) noexcept :
733 m_handle(std::move(other.m_handle)),
735 other.make_invalid();
736 other.m_handle =
nullptr;
739 inline osmium::MemoryMapping& osmium::MemoryMapping::operator=(osmium::MemoryMapping&& other) noexcept {
745 m_handle = std::move(other.m_handle);
747 other.make_invalid();
748 other.m_handle =
nullptr;
752 inline void osmium::MemoryMapping::unmap() {
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"};
768 inline void osmium::MemoryMapping::resize(std::size_t new_size) {
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
#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
MemoryMapping m_mapping
Definition: memory_mapping.hpp:333
int flag_type
Definition: memory_mapping.hpp:133
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
int fd() const noexcept
Definition: memory_mapping.hpp:267
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
void resize(std::size_t new_size)
Definition: memory_mapping.hpp:594
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:191
T * begin()
Definition: memory_mapping.hpp:459
Definition: memory_mapping.hpp:303
TypedMemoryMapping(std::size_t size)
Definition: memory_mapping.hpp:343
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
const T * end() const
Definition: memory_mapping.hpp:484
flag_type get_flags() const noexcept
Definition: memory_mapping.hpp:543
void unmap()
Definition: memory_mapping.hpp:402
bool is_valid() const noexcept
Definition: memory_mapping.hpp:521
void make_invalid() noexcept
Definition: memory_mapping.hpp:525
MemoryMapping(std::size_t size, mapping_mode mode, int fd=-1, off_t offset=0)
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
flag_type get_protection() const noexcept
Definition: memory_mapping.hpp:536
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
void unmap()
Definition: memory_mapping.hpp:585
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