38#include <gtest/gtest.h>
46namespace fs = std::filesystem;
99 arena.
init(test_file);
111 char * ptr = arena.
reserve(100);
113 std::memcpy(ptr,
"test data", 10);
136 std::memcpy(ptr,
"hello", 6);
159 std::memcpy(ptr,
"data", 5);
177 tmp = std::move(arena);
178 arena = std::move(
tmp);
193 char * ptr = arena.
reserve(100);
205 for (
int i = 0; i < 10; ++i)
207 char * ptr = arena.
reserve(50);
219 char * ptr = arena.
reserve(1000);
231 const char *
message =
"Hello, MapArena!";
232 size_t len = std::strlen(
message) + 1;
234 char * ptr = arena.
reserve(len);
237 std::memcpy(ptr,
message, len);
291 const char * data =
"ABCDEFGHIJ";
292 char * ptr = arena.
reserve(10);
294 std::memcpy(ptr, data, 10);
299 for (
auto it = arena.
begin(); it != arena.
end(); ++it, ++i)
309 std::memcpy(ptr,
"test", 5);
320 char * ptr = arena.
reserve(26);
321 for (
int i = 0; i < 26; ++i)
329 EXPECT_EQ(result,
"abcdefghijklmnopqrstuvwxyz");
377 const char *
message =
"Persistent data!";
378 size_t len = std::strlen(
message) + 1;
383 char * ptr = arena.
reserve(len);
385 std::memcpy(ptr,
message, len);
403 char * ptr = arena.
reserve(100);
405 std::memset(ptr,
'A', 100);
441 arena.
init(test_file);
479 for (
int i = 0; i < 10; ++i)
482 ASSERT_NE(ptr,
nullptr) <<
"Failed at iteration " << i;
501 std::ostringstream
oss;
536 constexpr int N = 10000;
537 for (
int i = 0; i <
N; ++i)
540 ASSERT_NE(ptr,
nullptr) <<
"Failed at allocation " << i;
552 for (
int i = 0; i < 100; ++i)
554 size_t sz = (i % 2 == 0) ? 16 : 256;
555 char * ptr = arena.
reserve(sz);
570 static_assert(std::is_same_v<MapArena::iterator, char*>);
571 static_assert(std::is_same_v<MapArena::const_iterator, const char*>);
572 static_assert(std::is_same_v<MapArena::size_type, size_t>);
590 ::testing::InitGoogleTest(&
argc,
argv);
Memory-mapped file arena allocator.
TEST_F(MapArenaTest, DefaultConstruction)
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).
char * base() const noexcept
Get the base address of the mapped region.
void init_and_erase(const std::string &file_path_name)
Initialize and erase any existing data.
void init(const std::string &file_path_name, void *addr=nullptr)
Initialize the arena with a backing file.
int file_descriptor() const noexcept
Get the file descriptor.
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.
iterator end() noexcept
Get iterator past the last allocated byte.
bool is_initialized() const noexcept
Check if the arena has been initialized.
static constexpr size_t initial_rgn_size
Initial region size in bytes (4 KB).
size_type avail() const noexcept
Get the available memory in the current mapping.
void * mapped_addr() const noexcept
Get the mapped memory address.
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.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
Main namespace for Aleph-w library functions.
void message(const char *file, int line, const char *format,...)
Print an informational message with file and line info.
DynList< T > maps(const C &c, Op op)
Classic map operation.