Libosmium
2.20.0
Fast and flexible C++ library for working with OpenStreetMap data
|
#include <memory_mapping.hpp>
Public Types | |
enum class | mapping_mode { readonly = 0 , write_private = 1 , write_shared = 2 } |
Public Member Functions | |
MemoryMapping (std::size_t size, mapping_mode mode, int fd=-1, off_t offset=0) | |
MemoryMapping (const MemoryMapping &)=delete | |
You can not copy construct a MemoryMapping. More... | |
MemoryMapping & | operator= (const MemoryMapping &)=delete |
You can not copy a MemoryMapping. More... | |
MemoryMapping (MemoryMapping &&other) noexcept | |
MemoryMapping & | operator= (MemoryMapping &&other) noexcept |
~MemoryMapping () noexcept | |
void | unmap () |
void | resize (std::size_t new_size) |
operator bool () const noexcept | |
std::size_t | size () const noexcept |
int | fd () const noexcept |
bool | writable () const noexcept |
template<typename T = void> | |
T * | get_addr () const noexcept |
Private Types | |
using | flag_type = int |
Private Member Functions | |
bool | is_valid () const noexcept |
void | make_invalid () noexcept |
flag_type | get_protection () const noexcept |
flag_type | get_flags () const noexcept |
int | resize_fd (int fd) const |
Static Private Member Functions | |
static std::size_t | check_size (std::size_t size) |
static std::size_t | available_space (int fd) |
Private Attributes | |
std::size_t | m_size |
The size of the mapping. More... | |
off_t | m_offset |
Offset into the file. More... | |
int | m_fd |
File handle we got the mapping from. More... | |
mapping_mode | m_mapping_mode |
Mapping mode. More... | |
void * | m_addr |
The address where the memory is mapped. More... | |
Class for wrapping memory mapping system calls.
Usage for anonymous mapping:
Or for file-backed mapping:
If the file backing a file-backed mapping is not large enough, it will be resized. This works, of course, only for writable files, so for read-only files you have to make sure they are large enough for any mapping you want.
If you ask for a zero-sized mapping, a mapping of the systems page size will be created instead. For file-backed mapping this will only work if the file is writable.
There are different implementations for Unix and Windows systems. On Unix systems this wraps the mmap(), munmap(), and the mremap() system calls. On Windows it wraps the CreateFileMapping(), CloseHandle(), MapViewOfFile(), and UnmapViewOfFile() functions.
On Windows the file will be set to binary mode before the memory mapping.
|
private |
|
strong |
|
inline |
Create memory mapping of given size.
If fd is not set (or fd == -1), an anonymous mapping will be created, otherwise a mapping based on the file descriptor will be created.
size | Size of the mapping in bytes |
mode | Mapping mode: readonly, or writable (shared or private) |
fd | Open file descriptor of a file we want to map |
offset | Offset into the file where the mapping should start |
std::system_error | if the mapping fails |
|
delete |
You can not copy construct a MemoryMapping.
|
inlinenoexcept |
Move construct a mapping from another one. The other mapping will be marked as invalid.
|
inlinenoexcept |
|
inlinestaticprivate |
|
inlinestaticprivate |
|
inlinenoexcept |
The file descriptor this mapping was created from.
|
inlinenoexcept |
Get the address of the mapping as any pointer type you like.
|
inlineprivatenoexcept |
|
inlineprivatenoexcept |
|
inlineprivatenoexcept |
|
inlineprivatenoexcept |
|
inlineexplicitnoexcept |
In a boolean context a MemoryMapping is true when it is a valid existing mapping.
|
delete |
You can not copy a MemoryMapping.
|
inlinenoexcept |
Move a mapping. The other mapping will be marked as invalid.
|
inline |
Resize a mapping to the given new size.
On Linux systems this will use the mremap() function. On other systems it will unmap and remap the memory. This can only be done for file-based mappings, not anonymous mappings!
new_size | Number of bytes to resize to (must be > 0). |
std::system_error | if the remapping fails. |
|
inlineprivate |
|
inlinenoexcept |
The number of bytes mapped. This is the same size you created the mapping with. The actual mapping will probably be larger because the system will round it to the page size.
|
inline |
Unmap a mapping. If the mapping is not valid, it will do nothing.
std::system_error | if the unmapping fails |
|
inlinenoexcept |
Was this mapping created as a writable mapping?
|
private |
The address where the memory is mapped.
|
private |
File handle we got the mapping from.
|
private |
Mapping mode.
|
private |
Offset into the file.
|
private |
The size of the mapping.