176using namespace Aleph;
177namespace fs = std::filesystem;
186 cout <<
"+" << string(70,
'-') <<
"+" << endl;
187 cout <<
"| " << left << setw(68) << title <<
" |" << endl;
188 cout <<
"+" << string(70,
'-') <<
"+" << endl;
193 cout <<
"\n " << subtitle << endl;
194 cout <<
" " << string(subtitle.length(),
'-') << endl;
199 cout <<
" " << label <<
":" << endl;
200 cout <<
" Size (committed): " << arena.size() <<
" bytes" << endl;
201 cout <<
" Capacity (mapped): " << arena.capacity() <<
" bytes" << endl;
202 cout <<
" Available: " << arena.avail() <<
" bytes" << endl;
203 cout <<
" Is empty: " << (arena.empty() ?
"yes" :
"no") << endl;
214 const string arena_file =
"/tmp/aleph_arena_basic.dat";
217 fs::remove(arena_file);
219 MapArena arena(arena_file);
227 const char*
message =
"Hola desde Colombia!";
228 size_t msg_len = strlen(
message) + 1;
230 char* ptr = arena.reserve(msg_len);
233 arena.commit(msg_len);
234 cout <<
" Stored: \"" <<
message <<
"\" (" << msg_len <<
" bytes)" << endl;
238 const char* cities[] = {
"Bogota",
"Medellin",
"Cali",
"Barranquilla"};
239 for (
const char* city : cities) {
240 size_t len = strlen(city) + 1;
241 char* p = arena.reserve(len);
243 memcpy(p, city, len);
252 cout <<
" Stored strings:" << endl;
253 char* read_ptr = arena.begin();
254 while (read_ptr < arena.end()) {
255 cout <<
" -> \"" << read_ptr <<
"\"" << endl;
256 read_ptr += strlen(read_ptr) + 1;
261 cout <<
"\n Data synced to disk" << endl;
264 fs::remove(arena_file);
282 const string arena_file =
"/tmp/aleph_arena_struct.dat";
283 fs::remove(arena_file);
285 MapArena arena(arena_file);
291 {
"Bogota", 8281, 1775.98, 2640},
292 {
"Medellin", 2569, 380.64, 1495},
293 {
"Cali", 2228, 564.33, 1018},
294 {
"Barranquilla", 1274, 154.00, 18},
295 {
"Cartagena", 1047, 609.10, 2}
298 size_t count =
sizeof(cities) /
sizeof(cities[0]);
301 char* ptr = arena.reserve(
sizeof(
size_t));
302 memcpy(ptr, &
count,
sizeof(
size_t));
303 arena.commit(
sizeof(
size_t));
306 for (
const auto& city : cities) {
317 char* read_ptr = arena.begin();
319 memcpy(&stored_count, read_ptr,
sizeof(
size_t));
320 read_ptr +=
sizeof(size_t);
322 cout <<
"\n Retrieved " << stored_count <<
" city records:" << endl;
323 cout <<
" " << string(60,
'-') << endl;
324 cout <<
" " << left << setw(15) <<
"City"
325 << right << setw(10) <<
"Pop (k)"
326 << setw(12) <<
"Area (km2)"
327 << setw(12) <<
"Alt (m)" << endl;
328 cout <<
" " << string(60,
'-') << endl;
330 for (
size_t i = 0; i < stored_count; ++i) {
335 cout <<
" " << left << setw(15) << rec.
name
337 << setw(12) << fixed << setprecision(2) << rec.
area
338 << setw(12) << rec.
altitude << endl;
341 fs::remove(arena_file);
350 print_header(
"Example 3: Arena Growth (Automatic Remapping)");
352 const string arena_file =
"/tmp/aleph_arena_growth.dat";
353 fs::remove(arena_file);
355 MapArena arena(arena_file);
357 cout <<
"\n Initial capacity: " << arena.capacity() <<
" bytes" << endl;
358 cout <<
" (Initial region size: " << MapArena::initial_rgn_size <<
" bytes)" << endl;
363 size_t prev_capacity = arena.capacity();
364 int growth_count = 0;
367 const size_t chunk_size = 1000;
369 for (
int i = 0; i < 20; ++i) {
370 char* ptr = arena.reserve(chunk_size);
373 memset(ptr,
'A' + (i % 26), chunk_size);
374 arena.commit(chunk_size);
376 if (arena.capacity() > prev_capacity) {
378 cout <<
" Growth #" << growth_count
379 <<
": " << prev_capacity <<
" -> " << arena.capacity()
380 <<
" bytes (after " << arena.size() <<
" allocated)" << endl;
381 prev_capacity = arena.capacity();
390 cout <<
"\n Verifying data integrity..." << endl;
391 char* ptr = arena.begin();
393 for (
int i = 0; i < 20 && valid; ++i) {
395 for (
size_t j = 0; j < chunk_size && valid; ++j) {
398 cout <<
" ERROR: Data corruption at chunk " << i << endl;
404 cout <<
" All data verified successfully!" << endl;
406 fs::remove(arena_file);
417 const string arena_file =
"/tmp/aleph_arena_log.dat";
418 fs::remove(arena_file);
420 MapArena arena(arena_file);
425 const char* log_entries[] = {
426 "[2024-01-15 08:00:00] Sistema iniciado en Bogota",
427 "[2024-01-15 08:00:01] Conexion con servidor Medellin",
428 "[2024-01-15 08:00:02] Usuario: Juan Perez",
429 "[2024-01-15 08:00:05] Transaccion #1001: $150,000 COP",
430 "[2024-01-15 08:00:07] Sincronizacion con Cali",
431 "[2024-01-15 08:00:10] Backup iniciado",
432 "[2024-01-15 08:00:15] Backup completado",
433 "[2024-01-15 08:00:20] Alerta: Memoria al 75%"
436 size_t num_entries =
sizeof(log_entries) /
sizeof(log_entries[0]);
438 for (
const char* entry : log_entries) {
439 size_t len = strlen(entry) + 1;
440 char* ptr = arena.reserve(len);
442 memcpy(ptr, entry, len);
447 cout <<
" Written " << num_entries <<
" log entries" << endl;
452 cout <<
"\n Log contents:" << endl;
453 cout <<
" " << string(55,
'-') << endl;
455 char* ptr = arena.begin();
457 while (ptr < arena.end()) {
458 cout <<
" " << ptr << endl;
459 ptr += strlen(ptr) + 1;
463 cout <<
" " << string(55,
'-') << endl;
464 cout <<
" Total entries: " <<
count << endl;
466 fs::remove(arena_file);
477 const string arena_file =
"/tmp/aleph_arena_move.dat";
478 fs::remove(arena_file);
482 MapArena arena1(arena_file);
484 const char* data =
"Datos importantes de Colombia";
485 char* ptr = arena1.reserve(strlen(data) + 1);
486 memcpy(ptr, data, strlen(data) + 1);
487 arena1.commit(strlen(data) + 1);
489 cout <<
" arena1 size: " << arena1.size() << endl;
490 cout <<
" arena1 is_initialized: " << arena1.is_initialized() << endl;
494 MapArena arena2(std::move(arena1));
496 cout <<
" After move construction:" << endl;
497 cout <<
" arena1 is_initialized: " << arena1.is_initialized() << endl;
498 cout <<
" arena2 is_initialized: " << arena2.is_initialized() << endl;
499 cout <<
" arena2 size: " << arena2.size() << endl;
500 cout <<
" arena2 data: \"" << arena2.begin() <<
"\"" << endl;
505 cout <<
" arena3 (before): is_initialized = " << arena3.is_initialized() << endl;
507 arena3 = std::move(arena2);
509 cout <<
" After move assignment:" << endl;
510 cout <<
" arena2 is_initialized: " << arena2.is_initialized() << endl;
511 cout <<
" arena3 is_initialized: " << arena3.is_initialized() << endl;
512 cout <<
" arena3 data: \"" << arena3.begin() <<
"\"" << endl;
514 fs::remove(arena_file);
523 print_header(
"Example 6: Memory Statistics and Efficiency");
525 const string arena_file =
"/tmp/aleph_arena_stats.dat";
526 fs::remove(arena_file);
528 MapArena arena(arena_file);
533 int allocations[] = {10, 50, 100, 500, 1000, 2000};
535 cout <<
"\n Allocation pattern analysis:" << endl;
536 cout <<
" " << string(50,
'-') << endl;
537 cout <<
" " << setw(10) <<
"Alloc Size"
538 << setw(12) <<
"Committed"
539 << setw(12) <<
"Capacity"
540 << setw(15) <<
"Utilization" << endl;
541 cout <<
" " << string(50,
'-') << endl;
543 for (
int size : allocations) {
544 char* ptr = arena.reserve(
size);
546 memset(ptr,
'X',
size);
549 double utilization = 100.0 * arena.size() / arena.capacity();
551 cout <<
" " << setw(10) <<
size
552 << setw(12) << arena.size()
553 << setw(12) << arena.capacity()
554 << setw(14) << fixed << setprecision(1) << utilization <<
"%" << endl;
559 cout << arena << endl;
561 fs::remove(arena_file);
571 cout <<
"========================================================================" << endl;
572 cout <<
" ALEPH-W MAP ARENA EXAMPLE" << endl;
573 cout <<
" Memory-Mapped File Arena Allocator" << endl;
574 cout <<
"========================================================================" << endl;
584 cout <<
"========================================================================" << endl;
585 cout <<
" Example completed successfully!" << endl;
586 cout <<
"========================================================================" << endl;
Memory-mapped file arena allocator.
Minimal std::expected-style result type for C++20.
void print_arena_status(const MapArena &arena, const string &label)
void demo_basic_operations()
void demo_structured_data()
void print_subheader(const string &subtitle)
void demo_move_semantics()
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.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.