Libosmium  2.7.1
Fast and flexible C++ library for working with OpenStreetMap data
memory_mapping.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_UTIL_MEMORY_MAPPING_HPP
2 #define OSMIUM_UTIL_MEMORY_MAPPING_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2016 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <cassert>
37 #include <cerrno>
38 #include <stdexcept>
39 #include <system_error>
40 
42 #include <osmium/util/file.hpp>
43 
44 #ifndef _WIN32
45 # include <sys/mman.h>
46 #else
47 # include <fcntl.h>
48 # include <io.h>
49 # include <windows.h>
50 # include <sys/types.h>
51 #endif
52 
53 namespace osmium {
54 
55  namespace util {
56 
94  class MemoryMapping {
95 
96 public:
97  enum class mapping_mode {
98  readonly = 0,
99  write_private = 1,
100  write_shared = 2
101  };
102 
103 private:
104 
106  size_t m_size;
107 
109  off_t m_offset;
110 
112  int m_fd;
113 
116 
117 #ifdef _WIN32
118  HANDLE m_handle;
119 #endif
120 
122  void* m_addr;
123 
124  bool is_valid() const noexcept;
125 
126  void make_invalid() noexcept;
127 
128 #ifdef _WIN32
129  using flag_type = DWORD;
130 #else
131  using flag_type = int;
132 #endif
133 
134  flag_type get_protection() const noexcept;
135 
136  flag_type get_flags() const noexcept;
137 
138  static size_t check_size(size_t size) {
139  if (size == 0) {
140  throw std::runtime_error("Zero-sized mapping is not allowed.");
141  }
142  return size;
143  }
144 
145 #ifdef _WIN32
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;
149 #endif
150 
151  int resize_fd(int fd) {
152  // Anonymous mapping doesn't need resizing.
153  if (fd == -1) {
154  return -1;
155  }
156 
157  // Make sure the file backing this mapping is large enough.
158  if (osmium::util::file_size(fd) < m_size + m_offset) {
159  osmium::util::resize_file(fd, m_size + m_offset);
160  }
161  return fd;
162  }
163 
164  public:
165 
182  MemoryMapping(size_t size, mapping_mode mode, int fd=-1, off_t offset=0);
183 
189  OSMIUM_DEPRECATED MemoryMapping(size_t size, bool writable=true, int fd=-1, off_t offset=0) :
190  MemoryMapping(size, writable ? mapping_mode::write_shared : mapping_mode::readonly, fd, offset) {
191  }
192 
194  MemoryMapping(const MemoryMapping&) = delete;
195 
197  MemoryMapping& operator=(const MemoryMapping&) = delete;
198 
203  MemoryMapping(MemoryMapping&& other);
204 
209 
214  ~MemoryMapping() noexcept {
215  try {
216  unmap();
217  } catch (std::system_error&) {
218  // Ignore any exceptions because destructor must not throw.
219  }
220  }
221 
228  void unmap();
229 
241  void resize(size_t new_size);
242 
247  explicit operator bool() const noexcept {
248  return is_valid();
249  }
250 
256  size_t size() const noexcept {
257  return m_size;
258  }
259 
265  int fd() const noexcept {
266  return m_fd;
267  }
268 
272  bool writable() const noexcept {
273  return m_mapping_mode != mapping_mode::readonly;
274  }
275 
281  template <typename T = void>
282  T* get_addr() const {
283  if (is_valid()) {
284  return reinterpret_cast<T*>(m_addr);
285  }
286  throw std::runtime_error("invalid memory mapping");
287  }
288 
289  }; // class MemoryMapping
290 
302 
303  public:
304 
307  }
308 
309 #ifndef __linux__
310 
314  void resize(size_t) = delete;
315 #endif
316 
317  }; // class AnonymousMemoryMapping
318 
328  template <typename T>
330 
332 
333  public:
334 
342  m_mapping(sizeof(T) * size, MemoryMapping::mapping_mode::write_private) {
343  }
344 
355  TypedMemoryMapping(size_t size, MemoryMapping::mapping_mode mode, int fd, off_t offset = 0) :
356  m_mapping(sizeof(T) * size, mode, fd, sizeof(T) * offset) {
357  }
358 
364  OSMIUM_DEPRECATED TypedMemoryMapping(size_t size, bool writable, int fd, off_t offset = 0) :
365  m_mapping(sizeof(T) * size, writable ? MemoryMapping::mapping_mode::write_shared : MemoryMapping::mapping_mode::readonly, fd, sizeof(T) * offset) {
366  }
367 
369  TypedMemoryMapping(const TypedMemoryMapping&) = delete;
370 
373 
378  TypedMemoryMapping(TypedMemoryMapping&& other) = default;
379 
383  TypedMemoryMapping& operator=(TypedMemoryMapping&& other) = default;
384 
389  ~TypedMemoryMapping() noexcept = default;
390 
397  void unmap() {
398  m_mapping.unmap();
399  }
400 
411  void resize(size_t new_size) {
412  m_mapping.resize(sizeof(T) * new_size);
413  }
414 
419  explicit operator bool() const noexcept {
420  return !!m_mapping;
421  }
422 
428  size_t size() const noexcept {
429  assert(m_mapping.size() % sizeof(T) == 0);
430  return m_mapping.size() / sizeof(T);
431  }
432 
438  int fd() const noexcept {
439  return m_mapping.fd();
440  }
441 
445  bool writable() const noexcept {
446  return m_mapping.writable();
447  }
448 
454  T* begin() {
455  return m_mapping.get_addr<T>();
456  }
457 
463  T* end() {
464  return m_mapping.get_addr<T>() + size();
465  }
466 
467  const T* cbegin() const {
468  return m_mapping.get_addr<T>();
469  }
470 
471  const T* cend() const {
472  return m_mapping.get_addr<T>() + size();
473  }
474 
475  const T* begin() const {
476  return m_mapping.get_addr<T>();
477  }
478 
479  const T* end() const {
480  return m_mapping.get_addr<T>() + size();
481  }
482 
483  }; // class TypedMemoryMapping
484 
485  template <typename T>
487 
488  public:
489 
491  TypedMemoryMapping<T>(size) {
492  }
493 
494 #ifndef __linux__
495 
499  void resize(size_t) = delete;
500 #endif
501 
502  }; // class AnonymousTypedMemoryMapping
503 
504  } // namespace util
505 
506 } // namespace osmium
507 
508 #ifndef _WIN32
509 
510 // =========== Unix implementation =============
511 
512 // MAP_FAILED is often a macro containing an old style cast
513 #pragma GCC diagnostic push
514 #pragma GCC diagnostic ignored "-Wold-style-cast"
515 
516 inline bool osmium::util::MemoryMapping::is_valid() const noexcept {
517  return m_addr != MAP_FAILED;
518 }
519 
521  m_addr = MAP_FAILED;
522 }
523 
524 #pragma GCC diagnostic pop
525 
526 // for BSD systems
527 #ifndef MAP_ANONYMOUS
528 # define MAP_ANONYMOUS MAP_ANON
529 #endif
530 
531 inline int osmium::util::MemoryMapping::get_protection() const noexcept {
533  return PROT_READ;
534  }
535  return PROT_READ | PROT_WRITE;
536 }
537 
538 inline int osmium::util::MemoryMapping::get_flags() const noexcept {
539  if (m_fd == -1) {
540  return MAP_PRIVATE | MAP_ANONYMOUS;
541  }
543  return MAP_SHARED;
544  }
545  return MAP_PRIVATE;
546 }
547 
548 inline osmium::util::MemoryMapping::MemoryMapping(size_t size, mapping_mode mode, int fd, off_t offset) :
549  m_size(check_size(size)),
550  m_offset(offset),
551  m_fd(resize_fd(fd)),
552  m_mapping_mode(mode),
553  m_addr(::mmap(nullptr, m_size, get_protection(), get_flags(), m_fd, m_offset)) {
554  assert(!(fd == -1 && mode == mapping_mode::readonly));
555  if (!is_valid()) {
556  throw std::system_error(errno, std::system_category(), "mmap failed");
557  }
558 }
559 
561  m_size(other.m_size),
562  m_offset(other.m_offset),
563  m_fd(other.m_fd),
565  m_addr(other.m_addr) {
566  other.make_invalid();
567 }
568 
570  unmap();
571  m_size = other.m_size;
572  m_offset = other.m_offset;
573  m_fd = other.m_fd;
574  m_mapping_mode = other.m_mapping_mode;
575  m_addr = other.m_addr;
576  other.make_invalid();
577  return *this;
578 }
579 
581  if (is_valid()) {
582  if (::munmap(m_addr, m_size) != 0) {
583  throw std::system_error(errno, std::system_category(), "munmap failed");
584  }
585  make_invalid();
586  }
587 }
588 
589 inline void osmium::util::MemoryMapping::resize(size_t new_size) {
590  assert(new_size > 0 && "can not resize to zero size");
591  if (m_fd == -1) { // anonymous mapping
592 #ifdef __linux__
593  m_addr = ::mremap(m_addr, m_size, new_size, MREMAP_MAYMOVE);
594  if (!is_valid()) {
595  throw std::system_error(errno, std::system_category(), "mremap failed");
596  }
597  m_size = new_size;
598 #else
599  assert(false && "can't resize anonymous mappings on non-linux systems");
600 #endif
601  } else { // file-based mapping
602  unmap();
603  m_size = new_size;
604  resize_fd(m_fd);
605  m_addr = ::mmap(nullptr, new_size, get_protection(), get_flags(), m_fd, m_offset);
606  if (!is_valid()) {
607  throw std::system_error(errno, std::system_category(), "mmap (remap) failed");
608  }
609  }
610 }
611 
612 #else
613 
614 // =========== Windows implementation =============
615 
616 /* References:
617  * CreateFileMapping: http://msdn.microsoft.com/en-us/library/aa366537(VS.85).aspx
618  * CloseHandle: http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx
619  * MapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366761(VS.85).aspx
620  * UnmapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366882(VS.85).aspx
621  */
622 
623 namespace osmium {
624 
625  namespace util {
626 
627  inline DWORD dword_hi(uint64_t x) {
628  return static_cast<DWORD>(x >> 32);
629  }
630 
631  inline DWORD dword_lo(uint64_t x) {
632  return static_cast<DWORD>(x & 0xffffffff);
633  }
634 
635  } // namespace util
636 
637 } // namespace osmium
638 
639 inline DWORD osmium::util::MemoryMapping::get_protection() const noexcept {
640  switch (m_mapping_mode) {
642  return PAGE_READONLY;
644  return PAGE_WRITECOPY;
646  return PAGE_READWRITE;
647  }
648 }
649 
650 inline DWORD osmium::util::MemoryMapping::get_flags() const noexcept {
651  switch (m_mapping_mode) {
653  return FILE_MAP_READ;
655  return FILE_MAP_COPY;
657  return FILE_MAP_WRITE;
658  }
659 }
660 
661 inline HANDLE osmium::util::MemoryMapping::get_handle() const noexcept {
662  if (m_fd == -1) {
663  return INVALID_HANDLE_VALUE;
664  }
665  return reinterpret_cast<HANDLE>(_get_osfhandle(m_fd));
666 }
667 
668 inline HANDLE osmium::util::MemoryMapping::create_file_mapping() const noexcept {
669  if (m_fd != -1) {
670  _setmode(m_fd, _O_BINARY);
671  }
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);
673 }
674 
675 inline void* osmium::util::MemoryMapping::map_view_of_file() const noexcept {
676  return MapViewOfFile(m_handle, get_flags(), osmium::util::dword_hi(m_offset), osmium::util::dword_lo(m_offset), m_size);
677 }
678 
679 inline bool osmium::util::MemoryMapping::is_valid() const noexcept {
680  return m_addr != nullptr;
681 }
682 
683 inline void osmium::util::MemoryMapping::make_invalid() noexcept {
684  m_addr = nullptr;
685 }
686 
687 inline osmium::util::MemoryMapping::MemoryMapping(size_t size, MemoryMapping::mapping_mode mode, int fd, off_t offset) :
688  m_size(check_size(size)),
689  m_offset(offset),
690  m_fd(resize_fd(fd)),
691  m_mapping_mode(mode),
692  m_handle(create_file_mapping()),
693  m_addr(nullptr) {
694 
695  if (!m_handle) {
696  throw std::system_error(GetLastError(), std::system_category(), "CreateFileMapping failed");
697  }
698 
699  m_addr = map_view_of_file();
700  if (!is_valid()) {
701  throw std::system_error(GetLastError(), std::system_category(), "MapViewOfFile failed");
702  }
703 }
704 
706  m_size(other.m_size),
707  m_offset(other.m_offset),
708  m_fd(other.m_fd),
709  m_mapping_mode(other.m_mapping_mode),
710  m_handle(std::move(other.m_handle)),
711  m_addr(other.m_addr) {
712  other.make_invalid();
713  other.m_handle = nullptr;
714 }
715 
717  unmap();
718  m_size = other.m_size;
719  m_offset = other.m_offset;
720  m_fd = other.m_fd;
721  m_mapping_mode = other.m_mapping_mode;
722  m_handle = std::move(other.m_handle);
723  m_addr = other.m_addr;
724  other.make_invalid();
725  other.m_handle = nullptr;
726  return *this;
727 }
728 
730  if (is_valid()) {
731  if (! UnmapViewOfFile(m_addr)) {
732  throw std::system_error(GetLastError(), std::system_category(), "UnmapViewOfFile failed");
733  }
734  make_invalid();
735  }
736 
737  if (m_handle) {
738  if (! CloseHandle(m_handle)) {
739  throw std::system_error(GetLastError(), std::system_category(), "CloseHandle failed");
740  }
741  m_handle = nullptr;
742  }
743 }
744 
745 inline void osmium::util::MemoryMapping::resize(size_t new_size) {
746  unmap();
747 
748  m_size = new_size;
749  resize_fd(m_fd);
750 
751  m_handle = create_file_mapping();
752  if (!m_handle) {
753  throw std::system_error(GetLastError(), std::system_category(), "CreateFileMapping failed");
754  }
755 
756  m_addr = map_view_of_file();
757  if (!is_valid()) {
758  throw std::system_error(GetLastError(), std::system_category(), "MapViewOfFile failed");
759  }
760 }
761 
762 #endif
763 
764 #endif // OSMIUM_UTIL_MEMORY_MAPPING_HPP
~MemoryMapping() noexcept
Definition: memory_mapping.hpp:214
const T * end() const
Definition: memory_mapping.hpp:479
bool is_valid() const noexcept
Definition: memory_mapping.hpp:516
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
flag_type get_protection() const noexcept
Definition: memory_mapping.hpp:531
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
flag_type get_flags() const noexcept
Definition: memory_mapping.hpp:538
int fd() const noexcept
Definition: memory_mapping.hpp:265
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
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
size_t size() const noexcept
Definition: memory_mapping.hpp:256
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
size_t size() const noexcept
Definition: memory_mapping.hpp:428
T * begin()
Definition: memory_mapping.hpp:454
Definition: memory_mapping.hpp:301
void make_invalid() noexcept
Definition: memory_mapping.hpp:520
AnonymousTypedMemoryMapping(size_t size)
Definition: memory_mapping.hpp:490
int fd() const noexcept
Definition: memory_mapping.hpp:438
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
void unmap()
Definition: memory_mapping.hpp:397
bool writable() const noexcept
Definition: memory_mapping.hpp:272
void resize_file(int fd, size_t new_size)
Definition: file.hpp:96
const T * begin() const
Definition: memory_mapping.hpp:475
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
bool writable() const noexcept
Definition: memory_mapping.hpp:445