91namespace fs = std::filesystem;
96constexpr std::uint32_t
kMagic = 0x414C5048U;
104 std::uint32_t version;
107static_assert(
offsetof(FileHeader, end) == 0);
109FileHeader read_header(
const MapArena & arena)
114 std::memcpy(&header, arena.
begin(),
sizeof(header));
120 std::memcpy(arena.
begin(), &header,
sizeof(header));
126 header.end >=
sizeof(FileHeader);
141 throw std::runtime_error(
"existing file with unknown format; delete it and retry");
149 throw std::runtime_error(
"MapArena: reserve() failed while initializing the header");
151 arena.
commit(
sizeof(FileHeader));
161 if (payload.size() > std::numeric_limits<std::uint32_t>::max())
162 throw std::runtime_error(
"payload too large");
164 const auto len =
static_cast<std::uint32_t
>(payload.size());
170 throw std::runtime_error(
"MapArena: reserve() failed (len)");
172 arena.
commit(
sizeof(len));
180 throw std::runtime_error(
"MapArena: reserve() failed (payload)");
186 auto header = read_header(arena);
188 throw std::runtime_error(
"invalid header while appending");
189 header.end = arena.
size();
200 const auto header = read_header(arena);
204 const char *
cursor = arena.
begin() +
sizeof(FileHeader);
205 const char *
const end_ptr = arena.
begin() + header.end;
209 if (
static_cast<size_t>(
end_ptr -
cursor) <
sizeof(std::uint32_t))
210 throw std::runtime_error(
"truncated file: missing length field");
212 std::uint32_t len = 0;
213 std::memcpy(&len,
cursor,
sizeof(len));
217 throw std::runtime_error(
"truncated file: incomplete payload");
230 for (
size_t index = 0; index <
recs.
size(); ++index)
231 cout <<
" [" << index <<
"] " <<
recs[index] <<
"\n";
238 const string arena_file =
"/tmp/aleph_map_arena_persist_demo.dat";
241 cout <<
"Aleph-w MapArena: persistence example\n";
Memory-mapped file arena allocator.
size_t size() const noexcept
Count the number of elements of the list.
Memory-mapped file arena allocator.
size_type size() const noexcept
Get the total committed (allocated) size.
size_type capacity() const noexcept
Get the current capacity (mapped region size).
void commit(const size_type sz) noexcept
Commit a previous reservation.
char * reserve(const size_type sz)
Reserve memory for allocation.
bool empty() const noexcept
Check if the arena is empty.
bool is_initialized() const noexcept
Check if the arena has been initialized.
iterator begin() noexcept
Get iterator to the beginning of allocated memory.
void sync() noexcept
Ensure data is persisted to disk.
iterator end() noexcept
Return an STL-compatible end iterator.
Main namespace for Aleph-w library functions.
DynList< T > maps(const C &c, Op op)
Classic map operation.