Skip to content

Instantly share code, notes, and snippets.

@arrieta
Created November 23, 2018 21:21
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save arrieta/f5aa2a47da8df9b7a135e499cea6659b to your computer and use it in GitHub Desktop.
Simple Memory Mapping in C++
// -*- coding:utf-8; mode:c++; mode:auto-fill; fill-column:80; -*-
/// @file nzl/memory_map.hpp
/// @brief A mapped memory region.
/// @author J. Arrieta <juan.arrieta@nablazerolabs.com>
/// @date November 23, 2018
///
/// Copyright 2018 Nabla Zero Labs
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to
/// deal in the Software without restriction, including without limitation the
/// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
/// sell copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
/// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
/// IN THE SOFTWARE.
#pragma once
// C++ Standard Library
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <system_error>
// C POSIX Library
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
namespace nzl {
/// @brief A mapped memory region.
class memory_map {
public:
/// @brief Create a memory_map of a regular file.
/// @param path Path to a file.
/// @throw std::system_error if the memory_map cannot be created.
explicit memory_map(const char* path) {
auto fp = ::open(path, O_RDONLY);
if (fp == -1) {
throw std::system_error(errno, std::system_category(), path);
}
struct stat sb;
if (::fstat(fp, &sb) == -1) {
::close(fp);
throw std::system_error(errno, std::system_category(), path);
}
m_size = sb.st_size;
m_data = static_cast<std::byte*>(
::mmap(nullptr, m_size, PROT_READ, MAP_SHARED, fp, 0));
::close(fp); // closing file descriptor does not unmap region
if (m_data == MAP_FAILED) {
throw std::system_error(errno, std::system_category(), path);
}
}
/// @brief Create a memory_map of a regular file.
/// @param path Path to a file.
/// @throw std::system_error if the memory_map cannot be created.
explicit memory_map(const std::string& path) : memory_map{path.c_str()} {}
/// @brief Move-construct a memory_map.
/// @param other Existing memory_map used to move-construct.
memory_map(memory_map&& other) noexcept
: m_data{other.m_data}, m_size{other.m_size} {
other.m_data = nullptr;
other.m_size = 0;
}
/// @brief Move-assign a memory_map.
/// @param other Existing memory_map used to move-assign.
memory_map& operator=(memory_map&& other) noexcept {
m_data = other.m_data;
m_size = other.m_size;
other.m_data = nullptr;
other.m_size = 0;
return *this;
}
/// @brief Destroy a memory_map.
~memory_map() noexcept {
if (m_data != nullptr) {
::munmap(data(), size());
m_data = nullptr;
m_size = 0;
}
}
/// @brief Read-only access to the beginning of the mapped region.
const std::byte* data() const noexcept { return m_data; }
/// @brief Read/write access to the beginning of the mapped region.
std::byte* data() noexcept { return m_data; }
/// @brief Size in bytes of the mapped region.
std::size_t size() const noexcept { return m_size; }
/// @brief Swap this memory_map and other memory_map.
/// @param other Existing memory_map to swap with this memory_map.
void swap(memory_map& other) noexcept {
using std::swap;
swap(m_data, other.m_data);
swap(m_size, other.m_size);
}
private:
std::byte* m_data{nullptr};
std::size_t m_size{0};
};
/// @brief Swap two @link memory_map memory_maps@endlink.
/// @param lhs First memory_map to swap.
/// @param rhs Second memory_map to swap.
inline void swap(memory_map& lhs, memory_map& rhs) noexcept {
return lhs.swap(rhs);
}
} // namespace nzl
@arrieta
Copy link
Author

arrieta commented Nov 23, 2018

Example:

#include <algorithm>
#include <iostream>

#include "memory_map.hpp"

int main(int argc, char* argv[]) {
  std::for_each(argv + 1, argv + argc, [](auto path) {
    try {
      nzl::memory_map mm(path);
      std::cout << mm.size() << " " << path << "\n";
    } catch (const std::exception& e) {
      std::cerr << "[fatal] " << e.what() << "\n";
    }
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment