66# define ALEPH_HAS_MAP_ARENA 1
68# define ALEPH_HAS_MAP_ARENA 0
71#if ALEPH_HAS_MAP_ARENA
84namespace fs = std::filesystem;
158 static constexpr size_t initial_rgn_size = 4 * 1024;
162 char * rgn_ptr =
nullptr;
164 size_t rgn_size = initial_rgn_size;
174 bool remap(
const size_t sz)
176 assert(sz > avail());
179 size_t new_size = 2 * rgn_size;
180 while (new_size - end_ < sz)
183 void * ptr = mremap(rgn_ptr, rgn_size, new_size, MREMAP_MAYMOVE);
184 if (ptr == MAP_FAILED)
187 const auto status = ftruncate(fd,
static_cast<off_t
>(new_size));
189 <<
"cannot truncate the file to " << new_size <<
" error = " << errno;
191 rgn_ptr =
static_cast<char*
>(ptr);
200 using iterator =
char *;
203 using const_iterator =
const char *;
206 using size_type = size_t;
218 void init(
const std::string & file_path_name,
void * addr =
nullptr)
221 if (fs::exists(fs::path(file_path_name)))
223 fd = open(file_path_name.c_str(), O_RDWR,
static_cast<mode_t
>(0600));
227 const auto status = ::read(fd, &end_,
sizeof(end_));
229 <<
"Cannot read file length of " << file_path_name;
232 const auto file_size = lseek(fd, 0, SEEK_END);
234 <<
"Cannot determine file length of " << file_path_name;
235 rgn_size =
static_cast<size_t>(file_size);
239 fd = open(file_path_name.c_str(), O_RDWR | O_CREAT,
static_cast<mode_t
>(0600));
241 const auto status = ftruncate(fd,
static_cast<off_t
>(rgn_size));
243 <<
"cannot truncate the file to " << rgn_size <<
" error = " << errno;
246 void *ptr = mmap(addr, rgn_size, PROT_READ | PROT_WRITE,
247 MAP_SHARED | MAP_POPULATE, fd, 0);
249 <<
"Cannot mmap. Error = " << errno;
251 rgn_ptr =
static_cast<char*
>(ptr);
262 void init_and_erase(
const std::string & file_path_name)
264 fs::remove(file_path_name);
265 init(file_path_name);
278 explicit MapArena(
const std::string & file_path_name)
280 init(file_path_name);
287 MapArena()
noexcept =
default;
290 MapArena(
const MapArena &) =
delete;
293 MapArena & operator=(
const MapArena &) =
delete;
301 MapArena(MapArena && other) noexcept
302 : rgn_ptr(other.rgn_ptr), end_(other.end_),
303 rgn_size(other.rgn_size), fd(other.fd)
305 other.rgn_ptr =
nullptr;
307 other.rgn_size = initial_rgn_size;
316 MapArena & operator=(MapArena && other)
noexcept
322 munmap(rgn_ptr, rgn_size);
327 rgn_ptr = other.rgn_ptr;
329 rgn_size = other.rgn_size;
333 other.rgn_ptr =
nullptr;
335 other.rgn_size = initial_rgn_size;
346 munmap(rgn_ptr, rgn_size);
355 [[nodiscard]] iterator begin()
noexcept {
return rgn_ptr; }
361 [[nodiscard]] const_iterator begin()
const noexcept {
return rgn_ptr; }
367 [[nodiscard]] iterator end()
noexcept {
return rgn_ptr + end_; }
373 [[nodiscard]] const_iterator end()
const noexcept {
return rgn_ptr + end_; }
382 [[nodiscard]]
char * base()
const noexcept {
return rgn_ptr; }
390 [[nodiscard]] size_type avail()
const noexcept
392 return rgn_size - end_;
409 [[nodiscard]]
char * reserve(
const size_type sz)
411 if (sz > avail() && !remap(sz))
414 return rgn_ptr + end_;
429 void commit(
const size_type sz)
noexcept
441 msync(rgn_ptr, rgn_size, MS_ASYNC);
448 [[nodiscard]] size_type size()
const noexcept
457 [[nodiscard]] size_type capacity()
const noexcept
466 [[nodiscard]]
bool empty()
const noexcept
475 [[nodiscard]]
bool is_initialized()
const noexcept
477 return rgn_ptr !=
nullptr;
484 [[nodiscard]]
void * mapped_addr()
const noexcept
493 [[nodiscard]]
int file_descriptor()
const noexcept
504 friend std::ostream &
operator<<(std::ostream & o,
const MapArena & s)
506 return o <<
"MapArena:\n"
507 <<
" rgn_ptr = " <<
static_cast<void*
>(s.rgn_ptr) <<
'\n'
508 <<
" end_ = " << s.end_ <<
'\n'
509 <<
" rgn_size = " << s.rgn_size <<
'\n'
510 <<
" size = " << s.size() <<
'\n'
511 <<
" capacity = " << s.capacity() <<
'\n'
512 <<
" avail = " << s.avail();
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
ostream & operator<<(ostream &os, const Task &t)
Main namespace for Aleph-w library functions.