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>
285 return reinterpret_cast<T*
>(
m_addr);
313 void resize(std::size_t) =
delete;
327 template <
typename T>
355 m_mapping(sizeof(T) * size, mode, fd, sizeof(T) * offset) {
364 m_mapping(sizeof(T) * size,
367 sizeof(T) * offset) {
414 m_mapping.
resize(
sizeof(T) * new_size);
421 explicit operator bool() const noexcept {
430 std::size_t
size() 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();
483 const T*
cend() const noexcept {
501 const T*
end() const noexcept {
507 template <
typename T>
521 void resize(std::size_t) =
delete;
535 #pragma GCC diagnostic push 536 #pragma GCC diagnostic ignored "-Wold-style-cast" 539 return m_addr != MAP_FAILED;
546 #pragma GCC diagnostic pop 549 #ifndef MAP_ANONYMOUS 550 # define MAP_ANONYMOUS MAP_ANON 557 return PROT_READ | PROT_WRITE;
578 throw std::system_error{errno, std::system_category(),
"mmap failed"};
588 other.make_invalid();
594 }
catch (
const std::system_error&) {
603 other.make_invalid();
610 throw std::system_error{errno, std::system_category(),
"munmap failed"};
617 assert(new_size > 0 &&
"can not resize to zero size");
622 throw std::system_error{errno, std::system_category(),
"mremap failed"};
626 assert(
false &&
"can't resize anonymous mappings on non-linux systems");
634 throw std::system_error{errno, std::system_category(),
"mmap (remap) failed"};
652 inline namespace util {
654 inline DWORD dword_hi(uint64_t x) {
655 return static_cast<DWORD
>(x >> 32);
658 inline DWORD dword_lo(uint64_t x) {
659 return static_cast<DWORD
>(x & 0xffffffff);
669 return PAGE_READONLY;
671 return PAGE_WRITECOPY;
675 return PAGE_READWRITE;
681 return FILE_MAP_READ;
683 return FILE_MAP_COPY;
687 return FILE_MAP_WRITE;
690 inline HANDLE osmium::util::MemoryMapping::get_handle()
const noexcept {
692 return INVALID_HANDLE_VALUE;
694 return reinterpret_cast<HANDLE
>(_get_osfhandle(
m_fd));
697 inline HANDLE osmium::util::MemoryMapping::create_file_mapping()
const noexcept {
699 _setmode(
m_fd, _O_BINARY);
701 return CreateFileMapping(get_handle(),
709 inline void* osmium::util::MemoryMapping::map_view_of_file()
const noexcept {
710 return MapViewOfFile(m_handle,
728 inline int last_error() noexcept {
729 return static_cast<int>(GetLastError());
737 m_handle(create_file_mapping()),
741 throw std::system_error{last_error(), std::system_category(),
"CreateFileMapping failed"};
744 m_addr = map_view_of_file();
746 throw std::system_error{last_error(), std::system_category(),
"MapViewOfFile failed"};
755 m_handle(std::move(other.m_handle)),
757 other.make_invalid();
758 other.m_handle =
nullptr;
767 m_handle = std::move(other.m_handle);
769 other.make_invalid();
770 other.m_handle =
nullptr;
776 if (!UnmapViewOfFile(
m_addr)) {
777 throw std::system_error{last_error(), std::system_category(),
"UnmapViewOfFile failed"};
783 if (!CloseHandle(m_handle)) {
784 throw std::system_error{last_error(), std::system_category(),
"CloseHandle failed"};
796 m_handle = create_file_mapping();
798 throw std::system_error{last_error(), std::system_category(),
"CreateFileMapping failed"};
801 m_addr = map_view_of_file();
803 throw std::system_error{last_error(), std::system_category(),
"MapViewOfFile failed"};
809 #endif // OSMIUM_UTIL_MEMORY_MAPPING_HPP const T * begin() const noexcept
Definition: memory_mapping.hpp:492
~MemoryMapping() noexcept
Definition: memory_mapping.hpp:216
bool is_valid() const noexcept
Definition: memory_mapping.hpp:538
#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:363
flag_type get_protection() const noexcept
Definition: memory_mapping.hpp:553
MemoryMapping m_mapping
Definition: memory_mapping.hpp:330
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 * cend() const noexcept
Definition: memory_mapping.hpp:483
flag_type get_flags() const noexcept
Definition: memory_mapping.hpp:560
T * get_addr() const noexcept
Definition: memory_mapping.hpp:284
int fd() const noexcept
Definition: memory_mapping.hpp:267
void unmap()
Definition: memory_mapping.hpp:607
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:430
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:550
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
const T * cbegin() const noexcept
Definition: memory_mapping.hpp:474
Definition: memory_mapping.hpp:508
std::size_t get_pagesize()
Definition: file.hpp:194
T * begin() noexcept
Definition: memory_mapping.hpp:456
Definition: memory_mapping.hpp:300
TypedMemoryMapping(std::size_t size)
Definition: memory_mapping.hpp:340
void make_invalid() noexcept
Definition: memory_mapping.hpp:542
std::size_t m_size
The size of the mapping.
Definition: memory_mapping.hpp:108
int fd() const noexcept
Definition: memory_mapping.hpp:440
MemoryMapping & operator=(const MemoryMapping &)=delete
You can not copy a MemoryMapping.
Definition: memory_mapping.hpp:328
void unmap()
Definition: memory_mapping.hpp:399
MemoryMapping(std::size_t size, mapping_mode mode, int fd=-1, off_t offset=0)
Definition: memory_mapping.hpp:570
bool writable() const noexcept
Definition: memory_mapping.hpp:274
AnonymousTypedMemoryMapping(std::size_t size)
Definition: memory_mapping.hpp:512
AnonymousMemoryMapping(std::size_t size)
Definition: memory_mapping.hpp:304
void resize_file(int fd, std::size_t new_size)
Definition: file.hpp:177
T * end() noexcept
Definition: memory_mapping.hpp:465
std::size_t size() const noexcept
Definition: memory_mapping.hpp:258
const T * end() const noexcept
Definition: memory_mapping.hpp:501
void resize(std::size_t new_size)
Definition: memory_mapping.hpp:616
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:354
void resize(std::size_t new_size)
Definition: memory_mapping.hpp:413
bool writable() const noexcept
Definition: memory_mapping.hpp:447