#include "mmap_allocator.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct mmap_file
{
std::string filename;
std::mutex mutex;
boost::interprocess::file_mapping mapping;
boost::interprocess::mapped_region region;
boost::interprocess::managed_external_buffer buffer;
mmap_file(std::string const &filename, std::size_t offset = 0);
~mmap_file();
void remove();
};
using mmap_file_ptr = std::shared_ptr;
struct mmap_dir_t
{
static constexpr std::size_t increase = 1024000000;
static constexpr std::size_t alignment = 32;
std::string mmap_dir_filename;
bool mmap_dir_created = false;
public:
~mmap_dir_t();
bool is_open();
void open_mmap_file(std::string const &filename, size_t file_size = increase);
void resize_mmap_file(size_t add_size);
size_t mmap_file_size = 0;
std::vector files;
};
thread_local mmap_file_ptr mmap_file_thread_ptr;
struct mmap_shm
{
std::mutex mutex;
std::vector region;
boost::interprocess::managed_external_buffer buffer;
static size_t mmap_file_size;
mmap_shm(size_t size);
static void open(size_t add_size);
static void close();
};
using mmap_shm_ptr = std::shared_ptr;
using allocator_t = boost::interprocess::allocator;
static mmap_dir_t mmap_dir;
std::vector< mmap_shm_ptr > mmap_shm_regions;
size_t mmap_shm::mmap_file_size = 0;
thread_local mmap_shm_ptr mmap_shm_thread_region_ptr;
std::mutex mmap_allocator_mutex;
mmap_file::mmap_file(std::string const &filename, std::size_t offset)
: filename(filename)
, mapping(filename.c_str(), boost::interprocess::read_write)
, region(mapping, boost::interprocess::read_write)
, buffer(boost::interprocess::create_only, reinterpret_cast(region.get_address()) + offset, region.get_size() - offset)
{ }
mmap_file::~mmap_file()
{
mapping = boost::interprocess::file_mapping();
region = boost::interprocess::mapped_region();
buffer = boost::interprocess::managed_external_buffer();
remove();
}
void mmap_file::remove()
{
if(!filename.empty()) {
try {
boost::filesystem::remove(filename.c_str());
} catch(boost::filesystem::filesystem_error &e) {
std::cout