1 #ifndef OSMIUM_UTIL_MEMORY_MAPPING_HPP 2 #define OSMIUM_UTIL_MEMORY_MAPPING_HPP 43 #include <system_error> 46 # include <sys/mman.h> 47 # include <sys/statvfs.h> 52 # include <sys/types.h> 57 inline namespace util {
134 using flag_type = int;
149 HANDLE get_handle()
const noexcept;
150 HANDLE create_file_mapping()
const noexcept;
151 void* map_view_of_file()
const noexcept;
161 const int result = ::fstatvfs(fd, &stat);
165 return stat.f_bsize * stat.f_bavail;
177 if (current_file_size < m_size + m_offset) {
179 if (available > 0 && current_file_size + available <= m_size) {
180 throw std::system_error{ENOSPC, std::system_category(),
"Could not resize file: Not enough space on filesystem"};
241 }
catch (
const std::system_error&) {
265 void resize(std::size_t new_size);
271 explicit operator bool() const noexcept {
280 std::size_t
size() const noexcept {
289 int fd() const noexcept {
305 template <
typename T =
void>
307 return reinterpret_cast<T*
>(
m_addr);
335 void resize(std::size_t) =
delete;
349 template <
typename T>
377 m_mapping(sizeof(T) * size, mode, fd, sizeof(T) * offset) {
386 m_mapping(sizeof(T) * size,
389 sizeof(T) * offset) {
436 m_mapping.
resize(
sizeof(T) * new_size);
443 explicit operator bool() const noexcept {
452 std::size_t
size() const noexcept {
453 assert(m_mapping.
size() %
sizeof(T) == 0);
454 return m_mapping.
size() /
sizeof(T);
462 int fd() const noexcept {
463 return m_mapping.
fd();
505 const T*
cend() const noexcept {
523 const T*
end() const noexcept {
529 template <
typename T>
543 void resize(std::size_t) =
delete;
557 #pragma GCC diagnostic push 558 #pragma GCC diagnostic ignored "-Wold-style-cast" 561 return m_addr != MAP_FAILED;
568 #pragma GCC diagnostic pop 571 #ifndef MAP_ANONYMOUS 572 # define MAP_ANONYMOUS MAP_ANON 579 return PROT_READ | PROT_WRITE;
600 throw std::system_error{errno, std::system_category(),
"mmap failed"};
610 other.make_invalid();
616 }
catch (
const std::system_error&) {
625 other.make_invalid();
632 throw std::system_error{errno, std::system_category(),
"munmap failed"};
639 assert(new_size > 0 &&
"can not resize to zero size");
644 throw std::system_error{errno, std::system_category(),
"mremap failed"};
648 assert(
false &&
"can't resize anonymous mappings on non-linux systems");
656 throw std::system_error{errno, std::system_category(),
"mmap (remap) failed"};
674 inline namespace util {
676 inline DWORD dword_hi(uint64_t x) {
677 return static_cast<DWORD
>(x >> 32);
680 inline DWORD dword_lo(uint64_t x) {
681 return static_cast<DWORD
>(x & 0xffffffff);
691 return PAGE_READONLY;
693 return PAGE_WRITECOPY;
697 return PAGE_READWRITE;
703 return FILE_MAP_READ;
705 return FILE_MAP_COPY;
709 return FILE_MAP_WRITE;
712 inline HANDLE osmium::util::MemoryMapping::get_handle()
const noexcept {
714 return INVALID_HANDLE_VALUE;
716 return reinterpret_cast<HANDLE
>(_get_osfhandle(
m_fd));
719 inline HANDLE osmium::util::MemoryMapping::create_file_mapping()
const noexcept {
721 _setmode(
m_fd, _O_BINARY);
723 return CreateFileMapping(get_handle(),
731 inline void* osmium::util::MemoryMapping::map_view_of_file()
const noexcept {
732 return MapViewOfFile(m_handle,
750 inline int last_error() noexcept {
751 return static_cast<int>(GetLastError());
759 m_handle(create_file_mapping()),
763 throw std::system_error{last_error(), std::system_category(),
"CreateFileMapping failed"};
766 m_addr = map_view_of_file();
768 throw std::system_error{last_error(), std::system_category(),
"MapViewOfFile failed"};
777 m_handle(std::move(other.m_handle)),
779 other.make_invalid();
780 other.m_handle =
nullptr;
789 m_handle = std::move(other.m_handle);
791 other.make_invalid();
792 other.m_handle =
nullptr;
798 if (!UnmapViewOfFile(
m_addr)) {
799 throw std::system_error{last_error(), std::system_category(),
"UnmapViewOfFile failed"};
805 if (!CloseHandle(m_handle)) {
806 throw std::system_error{last_error(), std::system_category(),
"CloseHandle failed"};
818 m_handle = create_file_mapping();
820 throw std::system_error{last_error(), std::system_category(),
"CreateFileMapping failed"};
823 m_addr = map_view_of_file();
825 throw std::system_error{last_error(), std::system_category(),
"MapViewOfFile failed"};
831 #endif // OSMIUM_UTIL_MEMORY_MAPPING_HPP const T * begin() const noexcept
Definition: memory_mapping.hpp:514
~MemoryMapping() noexcept
Definition: memory_mapping.hpp:238
bool is_valid() const noexcept
Definition: memory_mapping.hpp:560
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:51
OSMIUM_DEPRECATED TypedMemoryMapping(std::size_t size, bool writable, int fd, off_t offset=0)
Definition: memory_mapping.hpp:385
flag_type get_protection() const noexcept
Definition: memory_mapping.hpp:575
MemoryMapping m_mapping
Definition: memory_mapping.hpp:352
int flag_type
Definition: memory_mapping.hpp:134
OSMIUM_DEPRECATED MemoryMapping(std::size_t size, bool writable=true, int fd=-1, off_t offset=0)
Definition: memory_mapping.hpp:213
std::size_t file_size(int fd)
Definition: file.hpp:109
const T * cend() const noexcept
Definition: memory_mapping.hpp:505
flag_type get_flags() const noexcept
Definition: memory_mapping.hpp:582
T * get_addr() const noexcept
Definition: memory_mapping.hpp:306
int fd() const noexcept
Definition: memory_mapping.hpp:289
static std::size_t available_space(int fd)
Definition: memory_mapping.hpp:156
void unmap()
Definition: memory_mapping.hpp:629
Definition: memory_mapping.hpp:96
Definition: location.hpp:551
mapping_mode
Definition: memory_mapping.hpp:100
int resize_fd(int fd)
Definition: memory_mapping.hpp:169
std::size_t size() const noexcept
Definition: memory_mapping.hpp:452
void * m_addr
The address where the memory is mapped.
Definition: memory_mapping.hpp:125
off_t m_offset
Offset into the file.
Definition: memory_mapping.hpp:112
#define MAP_ANONYMOUS
Definition: memory_mapping.hpp:572
int m_fd
File handle we got the mapping from.
Definition: memory_mapping.hpp:115
mapping_mode m_mapping_mode
Mapping mode.
Definition: memory_mapping.hpp:118
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
const T * cbegin() const noexcept
Definition: memory_mapping.hpp:496
Definition: memory_mapping.hpp:530
std::size_t get_pagesize()
Definition: file.hpp:193
T * begin() noexcept
Definition: memory_mapping.hpp:478
Definition: memory_mapping.hpp:322
TypedMemoryMapping(std::size_t size)
Definition: memory_mapping.hpp:362
void make_invalid() noexcept
Definition: memory_mapping.hpp:564
std::size_t m_size
The size of the mapping.
Definition: memory_mapping.hpp:109
int fd() const noexcept
Definition: memory_mapping.hpp:462
MemoryMapping & operator=(const MemoryMapping &)=delete
You can not copy a MemoryMapping.
Definition: memory_mapping.hpp:350
void unmap()
Definition: memory_mapping.hpp:421
MemoryMapping(std::size_t size, mapping_mode mode, int fd=-1, off_t offset=0)
Definition: memory_mapping.hpp:592
bool writable() const noexcept
Definition: memory_mapping.hpp:296
AnonymousTypedMemoryMapping(std::size_t size)
Definition: memory_mapping.hpp:534
AnonymousMemoryMapping(std::size_t size)
Definition: memory_mapping.hpp:326
void resize_file(int fd, std::size_t new_size)
Definition: file.hpp:177
T * end() noexcept
Definition: memory_mapping.hpp:487
std::size_t size() const noexcept
Definition: memory_mapping.hpp:280
const T * end() const noexcept
Definition: memory_mapping.hpp:523
void resize(std::size_t new_size)
Definition: memory_mapping.hpp:638
static std::size_t check_size(std::size_t size)
Definition: memory_mapping.hpp:141
TypedMemoryMapping(std::size_t size, MemoryMapping::mapping_mode mode, int fd, off_t offset=0)
Definition: memory_mapping.hpp:376
void resize(std::size_t new_size)
Definition: memory_mapping.hpp:435
bool writable() const noexcept
Definition: memory_mapping.hpp:469