|
mio 1.1.0
|
This is a fork of the original repo, which is not actively maintained.
An easy to use header-only cross-platform C++11 memory mapping library with an MIT license.
mio has been created with the goal to be easily includable (i.e. no dependencies) in any C++ project that needs memory mapped file IO without the need to pull in Boost.
Please feel free to open an issue, I'll try to address any concerns as best I can.
Because memory mapping is the best thing since sliced bread!
More seriously, the primary motivation for writing this library instead of using Boost.Iostreams, was the lack of support for establishing a memory mapping with an already open file handle/descriptor. This is possible with mio.
Furthermore, Boost.Iostreams' solution requires that the user pick offsets exactly at page boundaries, which is cumbersome and error prone. mio, on the other hand, manages this internally, accepting any offset and finding the nearest page boundary.
Albeit a minor nitpick, Boost.Iostreams implements memory mapped file IO with a std::shared_ptr to provide shared semantics, even if not needed, and the overhead of the heap allocation may be unnecessary and/or unwanted. In mio, there are two classes to cover the two use-cases: one that is move-only (basically a zero-cost abstraction over the system specific mmapping functions), and the other that acts just like its Boost.Iostreams counterpart, with shared semantics.
Note : the file must exist before creating a mapping.
There are three ways to map a file into memory:
std::system_error on failure: or you can omit the offset and size_to_map arguments, in which case the entire file is mapped: map member function: to build your projects withNote: The constructors require exceptions to be enabled. If you prefer
-fno-exceptions, you can still use the other ways.Moreover, in each case, you can provide either some string type for the file's path, or you can use an existing, valid file handle.
However, mio does not check whether the provided file descriptor has the same access permissions as the desired mapping, so the mapping may fail. Such errors are reported via the std::error_code out parameter that is passed to the mapping function.
WINDOWS USERS: This library does support the use of wide character types for functions where character strings are expected (e.g. path parameters).
mio::basic_mmap is move-only, but if multiple copies to the same mapping are needed, use mio::basic_shared_mmap which has std::shared_ptr semantics and has the same interface as mio::basic_mmap.
It's possible to define the type of a byte (which has to be the same width as char), though aliases for the most common ones are provided by default:
But it may be useful to define your own types, say when using the new std::byte type in C++17:
Though generally not needed, since mio maps users requested offsets to page boundaries, you can query the underlying system's page allocation granularity by invoking mio::page_size(), which is located in mio/page.hpp.
Mio can be added to your project as a single header file simply by including \single_include\mio\mio.hpp. Single header files can be regenerated at any time by running the amalgamate.py script within \third_party.
As a header-only library, mio has no compiled components. Nevertheless, a CMake build system is provided to allow easy testing, installation, and subproject composition on many platforms and operating systems.
Mio is distributed with a small suite of tests and examples. When mio is configured as the highest level CMake project, this suite of executables is built by default. Mio's test executables are integrated with the CMake test driver program, CTest.
CMake supports a number of backends for compilation and linking.
To use a static configuration build tool, such as GNU Make or Ninja:
To use a dynamic configuration build tool, such as Visual Studio or Xcode:
Of course the build and test steps can also be executed via the all and test targets, respectively, from within the IDE after opening the project file generated during the configuration step.
Mio's testing is also configured to operate as a client to the CDash software quality dashboard application. Please see the Kitware documentation for more information on this mode of operation.
Mio's build system provides an installation target and support for downstream consumption via CMake's find_package intrinsic function. CMake allows installation to an arbitrary location, which may be specified by defining CMAKE_INSTALL_PREFIX at configure time. In the absense of a user specification, CMake will install mio to conventional location based on the platform operating system.
To use a static configuration build tool, such as GNU Make or Ninja:
To use a dynamic configuration build tool, such as Visual Studio or Xcode:
Note that the last command of the installation sequence may require administrator privileges (e.g. sudo) if the installation root directory lies outside your home directory.
This installation
include/mio subdirectory of the installation rootshare/cmake/mio subdirectory of the installation rootThis latter step allows downstream CMake projects to consume mio via find_package, e.g.
WINDOWS USERS: The mio::mio target #defines WIN32_LEAN_AND_MEAN and NOMINMAX. The former ensures the imported surface area of the Win API is minimal, and the latter disables Windows' min and max macros so they don't intefere with std::min and std::max. Because mio is a header only library, these defintions will leak into downstream CMake builds. If their presence is causing problems with your build then you can use the alternative mio::mio_full_winapi target, which adds none of these defintions.
If mio was installed to a non-conventional location, it may be necessary for downstream projects to specify the mio installation root directory via either
CMAKE_PREFIX_PATH configuration option,CMAKE_PREFIX_PATH environment variable, ormio_DIR environment variable.Please see the Kitware documentation for more information.
In addition, mio supports packaged relocatable installations via CPack. Following configuration, from the build directory, invoke cpack as follows to generate a packaged installation:
The list of supported generators varies from platform to platform. See the output of cpack --help for a complete list of supported generators on your platform.
To use mio as a subproject, copy the mio repository to your project's dependencies/externals folder. If your project is version controlled using git, a git submodule or git subtree can be used to syncronize with the updstream repository. The use and relative advantages of these git facilities is beyond the scope of this document, but in brief, each may be established as follows:
Given a mio subdirectory in a project, simply add the following lines to your project's to add mio include directories to your target's include path.
Note that, as a subproject, mio's tests and examples will not be built and CPack integration is deferred to the host project.