19 #ifndef ALLOCATOR_FACTORY_HPP
20 #define ALLOCATOR_FACTORY_HPP
25 #include <boost/filesystem.hpp>
26 #include "allocator.hpp"
29 namespace fsys = boost::filesystem;
35 virtual std::unique_ptr<baseallocator> get(
const std::string& name)
const = 0;
36 virtual std::unique_ptr<baseallocator> get(
size_t nb)
const = 0;
38 virtual std::string to_string()
const = 0;
39 inline virtual bool isPersistent()
const {
return false; }
40 inline virtual fsys::path getDirname()
const {
41 throw std::range_error(
"no file mapping");
46 inline std::unique_ptr<baseallocator> get(
const std::string&)
const {
47 return std::make_unique<memallocator>();
49 inline std::unique_ptr<baseallocator> get(
size_t)
const {
50 return std::make_unique<memallocator>();
52 inline std::string to_string()
const {
53 return "malloc-based"s;
58 inline std::unique_ptr<baseallocator> get(
const std::string&)
const {
59 return std::make_unique<flexallocator>();
61 inline std::unique_ptr<baseallocator> get(
size_t)
const {
62 return std::make_unique<flexallocator>();
64 inline std::string to_string()
const {
70 MmapAllocFactory(
const fsys::path& dirname_p,
bool expectExists) : dirname(dirname_p) {
72 if (stat(dirname.c_str(), &st) == 0) {
74 throw std::range_error(dirname.c_str() +
" already exists"s);
79 throw std::range_error(dirname.c_str() +
" does not exist"s);
82 if (mkdir(dirname.c_str(), 0700) == -1) {
83 throw std::system_error(std::error_code(errno, std::system_category()),
84 "cannot create "s + dirname.c_str());
90 inline std::unique_ptr<baseallocator> get(
const std::string& name)
const {
91 return std::make_unique<mmapallocator>(dirname / fsys::path(name));
93 inline std::unique_ptr<baseallocator> get(
size_t nb)
const {
94 return std::make_unique<mmapallocator>(dirname / fsys::path(std::to_string(nb)));
97 inline std::string to_string()
const {
98 return "mmap file = "s + dirname.c_str();
101 inline virtual fsys::path getDirname()
const {
return dirname; }
103 inline virtual bool isPersistent()
const {
return true; }
106 const fsys::path dirname;