Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah_arena_exhaustive_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
56#include <gtest/gtest.h>
57
58#include <ah-arena.H>
59
60#include <algorithm>
61#include <chrono>
62#include <cstdint>
63#include <limits>
64#include <random>
65#include <stdexcept>
66#include <string>
67#include <type_traits>
68#include <vector>
69
70using namespace Aleph;
71
72// ============================================================================
73// Helper Classes and Utilities
74// ============================================================================
75
76namespace
77{
78// Exception-throwing object for testing exception safety
79class ThrowingObject
80{
81 static bool should_throw_;
82 static int construct_count_;
83 static int destruct_count_;
84
85public:
86 int value;
87
88 explicit ThrowingObject(int v) : value(v)
89 {
90 ++construct_count_;
91 if (should_throw_)
92 throw std::runtime_error("Intentional construction failure");
93 }
94
95 ~ThrowingObject()
96 {
97 ++destruct_count_;
98 }
99
100 static void reset_counters()
101 {
102 construct_count_ = 0;
103 destruct_count_ = 0;
104 should_throw_ = false;
105 }
106
107 static void set_throw(bool t) { should_throw_ = t; }
108 static int constructions() { return construct_count_; }
109 static int destructions() { return destruct_count_; }
110};
111
112bool ThrowingObject::should_throw_ = false;
113int ThrowingObject::construct_count_ = 0;
114int ThrowingObject::destruct_count_ = 0;
115
116// Polymorphic base class
117class Base
118{
119public:
120 virtual ~Base() = default;
121 virtual int get_value() const = 0;
122 virtual std::string get_type() const = 0;
123};
124
125// Derived class 1
126class Derived1 : public Base
127{
128 int value_;
129public:
130 explicit Derived1(int v) : value_(v) {}
131 int get_value() const override { return value_; }
132 std::string get_type() const override { return "Derived1"; }
133};
134
135// Derived class 2
136class Derived2 : public Base
137{
138 int value_;
139public:
140 explicit Derived2(int v) : value_(v) {}
141 int get_value() const override { return value_ * 2; }
142 std::string get_type() const override { return "Derived2"; }
143};
144
145// Large object for alignment testing
146struct alignas(64) CacheLineAligned
147{
148 char data[64];
149 int marker = 0xDEADBEEF;
150
151 CacheLineAligned() = default;
152 explicit CacheLineAligned(int m) : marker(m) {}
153};
154
155// Small object for fragmentation testing
156struct TinyObject
157{
158 char byte;
159 explicit TinyObject(char b = 0) : byte(b) {}
160};
161
162// Medium object
163struct MediumObject
164{
165 char data[128];
166 int id;
167 explicit MediumObject(int i = 0) : id(i) { std::fill(data, data + 128, static_cast<char>(i)); }
168};
169
170// Large object
171struct LargeObject
172{
173 char data[4096];
174 int id;
175 explicit LargeObject(int i = 0) : id(i) { std::fill(data, data + 4096, static_cast<char>(i)); }
176};
177
178// RAII wrapper for tracking allocations
179template <typename T>
180class AllocationTracker
181{
182 AhArenaAllocator& arena_;
183 T* ptr_;
184
185public:
186 AllocationTracker(AhArenaAllocator& arena, T* p) : arena_(arena), ptr_(p) {}
187
188 ~AllocationTracker()
189 {
190 if (ptr_)
191 deallocate(arena_, ptr_);
192 }
193
194 AllocationTracker(const AllocationTracker&) = delete;
195 AllocationTracker& operator=(const AllocationTracker&) = delete;
196
197 AllocationTracker(AllocationTracker&& other) noexcept
198 : arena_(other.arena_), ptr_(other.ptr_)
199 {
200 other.ptr_ = nullptr;
201 }
202
203 T* get() const { return ptr_; }
204 T* operator->() const { return ptr_; }
205 T& operator*() const { return *ptr_; }
206};
207
208} // anonymous namespace
209
210// ============================================================================
211// Stress Tests - Massive Allocations
212// ============================================================================
213
214TEST(ArenaStress, ThousandSmallAllocations)
215{
216 const size_t NUM_ALLOCS = 10000;
217 const size_t ALLOC_SIZE = 8;
218
219 AhArenaAllocator arena(NUM_ALLOCS * ALLOC_SIZE);
220
221 std::vector<void*> ptrs;
222 ptrs.reserve(NUM_ALLOCS);
223
224 for (size_t i = 0; i < NUM_ALLOCS; ++i)
225 {
226 void* ptr = arena.alloc(ALLOC_SIZE);
227 ASSERT_NE(ptr, nullptr) << "Allocation " << i << " failed";
228 ptrs.push_back(ptr);
229 }
230
231 EXPECT_EQ(arena.allocated_size(), NUM_ALLOCS * ALLOC_SIZE);
232 EXPECT_TRUE(arena.full());
233
234 // Verify all pointers are distinct
235 std::sort(ptrs.begin(), ptrs.end());
236 auto last = std::unique(ptrs.begin(), ptrs.end());
237 EXPECT_EQ(last, ptrs.end()) << "Found duplicate pointers";
238}
239
240TEST(ArenaStress, MixedSizeAllocations)
241{
242 const size_t ARENA_SIZE = 1024 * 1024; // 1 MB
243 AhArenaAllocator arena(ARENA_SIZE);
244
245 std::mt19937 rng(42);
246 std::uniform_int_distribution<size_t> size_dist(1, 1024);
247
248 std::vector<void*> ptrs;
249 size_t total_allocated = 0;
250
251 while (true)
252 {
253 size_t size = size_dist(rng);
254 void* ptr = arena.alloc(size);
255
256 if (ptr == nullptr)
257 break;
258
259 ptrs.push_back(ptr);
260 total_allocated += size;
261 }
262
263 EXPECT_GT(ptrs.size(), 100u) << "Should have allocated many objects";
264 EXPECT_LE(total_allocated, ARENA_SIZE);
265 EXPECT_GE(arena.allocated_size(), total_allocated * 0.9); // Allow some slack
266}
267
268TEST(ArenaStress, ThousandTypedAllocations)
269{
270 const size_t NUM_OBJECTS = 5000;
271 AhArenaAllocator arena(NUM_OBJECTS * sizeof(MediumObject));
272
273 std::vector<MediumObject*> objects;
274 objects.reserve(NUM_OBJECTS);
275
276 for (size_t i = 0; i < NUM_OBJECTS; ++i)
277 {
278 MediumObject* obj = allocate<MediumObject>(arena, static_cast<int>(i));
279 ASSERT_NE(obj, nullptr) << "Object " << i << " allocation failed";
280 EXPECT_EQ(obj->id, static_cast<int>(i));
281 objects.push_back(obj);
282 }
283
284 // Verify all objects are correctly initialized
285 for (size_t i = 0; i < objects.size(); ++i)
286 {
287 EXPECT_EQ(objects[i]->id, static_cast<int>(i));
288 EXPECT_EQ(objects[i]->data[0], static_cast<char>(i));
289 }
290
291 // LIFO deallocation
292 for (auto it = objects.rbegin(); it != objects.rend(); ++it)
293 deallocate(arena, *it);
294
295 EXPECT_TRUE(arena.empty());
296}
297
298// ============================================================================
299// Fragmentation and Usage Patterns
300// ============================================================================
301
302TEST(ArenaFragmentation, AlternatingSmallLarge)
303{
304 const size_t ARENA_SIZE = 1024 * 100;
305 AhArenaAllocator arena(ARENA_SIZE);
306
307 std::vector<void*> small_ptrs;
308 std::vector<void*> large_ptrs;
309
310 // Allocate alternating small and large
311 for (int i = 0; i < 50; ++i)
312 {
313 void* small = arena.alloc(16);
314 void* large = arena.alloc(1024);
315
316 if (small) small_ptrs.push_back(small);
317 if (large) large_ptrs.push_back(large);
318 }
319
320 size_t allocated = arena.allocated_size();
321 EXPECT_GT(allocated, 50u * (16 + 1024) * 0.8); // At least 80% efficiency
322}
323
324TEST(ArenaFragmentation, WorstCasePattern)
325{
326 // Allocate largest chunks first, then try to fit small ones
327 const size_t ARENA_SIZE = 10000;
328 AhArenaAllocator arena(ARENA_SIZE);
329
330 std::vector<void*> ptrs;
331
332 // Fill with large allocations
333 while (true)
334 {
335 void* ptr = arena.alloc(1000);
336 if (!ptr) break;
337 ptrs.push_back(ptr);
338 }
339
340 size_t before = arena.allocated_size();
341
342 // Try to allocate small (should fail or succeed depending on remaining)
343 void* small = arena.alloc(10);
344 (void)small; // May or may not succeed
345
346 EXPECT_GE(arena.allocated_size(), before);
347}
348
349TEST(ArenaFragmentation, RepeatedResetAndReuse)
350{
351 const size_t ARENA_SIZE = 50000;
352 AhArenaAllocator arena(ARENA_SIZE);
353
354 for (int cycle = 0; cycle < 100; ++cycle)
355 {
356 // Allocate varying amounts
357 std::mt19937 rng(cycle);
358 std::uniform_int_distribution<size_t> size_dist(10, 500);
359
360 size_t total = 0;
361 while (total < ARENA_SIZE * 0.9)
362 {
363 size_t size = size_dist(rng);
364 void* ptr = arena.alloc(size);
365 if (!ptr) break;
366 total += size;
367 }
368
369 EXPECT_GT(arena.allocated_size(), ARENA_SIZE * 0.5);
370
371 arena.reset();
372 EXPECT_TRUE(arena.empty());
373 }
374
375 // Arena should still be usable after 100 cycles
376 void* ptr = arena.alloc(1000);
377 EXPECT_NE(ptr, nullptr);
378}
379
380// ============================================================================
381// Exception Safety Tests
382// ============================================================================
383
384TEST(ArenaExceptionSafety, ConstructorThrows)
385{
386 ThrowingObject::reset_counters();
387 ThrowingObject::set_throw(true);
388
389 AhArenaAllocator arena(4096);
390
391 ThrowingObject* obj = nullptr;
392 EXPECT_THROW(
393 obj = allocate<ThrowingObject>(arena, 42),
394 std::runtime_error
395 );
396
397 EXPECT_EQ(obj, nullptr);
398 EXPECT_EQ(ThrowingObject::constructions(), 1); // Constructor was called
399 EXPECT_EQ(ThrowingObject::destructions(), 0); // But object not created
400
401 // Arena should still be usable
402 ThrowingObject::set_throw(false);
403 obj = allocate<ThrowingObject>(arena, 100);
404 EXPECT_NE(obj, nullptr);
405 EXPECT_EQ(obj->value, 100);
406}
407
408TEST(ArenaExceptionSafety, PartialAllocationRecovery)
409{
410 ThrowingObject::reset_counters();
411
412 AhArenaAllocator arena(4096);
413
414 // Allocate some objects successfully
415 std::vector<ThrowingObject*> objs;
416 for (int i = 0; i < 5; ++i)
417 {
418 ThrowingObject* obj = allocate<ThrowingObject>(arena, i);
419 ASSERT_NE(obj, nullptr);
420 objs.push_back(obj);
421 }
422
423 EXPECT_EQ(ThrowingObject::constructions(), 5);
424
425 // Now cause one to throw
426 ThrowingObject::set_throw(true);
427 EXPECT_THROW(
428 allocate<ThrowingObject>(arena, 999),
429 std::runtime_error
430 );
431
432 // Previous allocations should still be valid
433 for (size_t i = 0; i < objs.size(); ++i)
434 EXPECT_EQ(objs[i]->value, static_cast<int>(i));
435
436 // Cleanup
437 ThrowingObject::set_throw(false);
438 for (auto it = objs.rbegin(); it != objs.rend(); ++it)
439 deallocate(arena, *it);
440}
441
442// ============================================================================
443// Polymorphic and Complex Objects
444// ============================================================================
445
446TEST(ArenaPolymorphism, VirtualDispatch)
447{
448 AhArenaAllocator arena(4096);
449
450 std::vector<Base*> objects;
451
452 for (int i = 0; i < 10; ++i)
453 {
454 if (i % 2 == 0)
455 objects.push_back(allocate<Derived1>(arena, i));
456 else
457 objects.push_back(allocate<Derived2>(arena, i));
458 }
459
460 // Verify polymorphic behavior
461 for (size_t i = 0; i < objects.size(); ++i)
462 {
463 if (i % 2 == 0)
464 {
465 EXPECT_EQ(objects[i]->get_type(), "Derived1");
466 EXPECT_EQ(objects[i]->get_value(), static_cast<int>(i));
467 }
468 else
469 {
470 EXPECT_EQ(objects[i]->get_type(), "Derived2");
471 EXPECT_EQ(objects[i]->get_value(), static_cast<int>(i) * 2);
472 }
473 }
474
475 // Cleanup (must call virtual destructors)
476 for (auto it = objects.rbegin(); it != objects.rend(); ++it)
477 {
478 if ((*it)->get_type() == "Derived1")
479 deallocate<Derived1>(arena, static_cast<Derived1*>(*it));
480 else
481 deallocate<Derived2>(arena, static_cast<Derived2*>(*it));
482 }
483}
484
485TEST(ArenaComplexObjects, StringMembers)
486{
487 struct ComplexObject
488 {
489 std::string name;
490 std::vector<int> numbers;
491 int id;
492
493 ComplexObject(std::string n, std::vector<int> nums, int i)
494 : name(std::move(n)), numbers(std::move(nums)), id(i)
495 {}
496 };
497
498 AhArenaAllocator arena(100000);
499
500 std::vector<ComplexObject*> objects;
501
502 for (int i = 0; i < 100; ++i)
503 {
504 std::string name = "Object_" + std::to_string(i);
505 std::vector<int> nums(i % 10 + 1, i);
506
507 ComplexObject* obj = allocate<ComplexObject>(arena, name, nums, i);
508 ASSERT_NE(obj, nullptr);
509 objects.push_back(obj);
510 }
511
512 // Verify
513 for (size_t i = 0; i < objects.size(); ++i)
514 {
515 EXPECT_EQ(objects[i]->name, "Object_" + std::to_string(i));
516 EXPECT_EQ(objects[i]->id, static_cast<int>(i));
517 EXPECT_EQ(objects[i]->numbers.size(), i % 10 + 1);
518 }
519
520 // Cleanup
521 for (auto it = objects.rbegin(); it != objects.rend(); ++it)
522 deallocate(arena, *it);
523}
524
525// ============================================================================
526// Alignment Tests - Advanced
527// ============================================================================
528
529TEST(ArenaAlignmentAdvanced, CacheLineAlignment)
530{
531 AhArenaAllocator arena(10000);
532
533 std::vector<CacheLineAligned*> objects;
534
535 for (int i = 0; i < 50; ++i)
536 {
537 CacheLineAligned* obj = allocate<CacheLineAligned>(arena, i);
538 ASSERT_NE(obj, nullptr);
539 EXPECT_EQ(reinterpret_cast<uintptr_t>(obj) % 64, 0u)
540 << "Object " << i << " not 64-byte aligned";
541 EXPECT_EQ(obj->marker, i);
542 objects.push_back(obj);
543 }
544
545 // Cleanup
546 for (auto it = objects.rbegin(); it != objects.rend(); ++it)
547 deallocate(arena, *it);
548}
549
550TEST(ArenaAlignmentAdvanced, MixedAlignments)
551{
552 AhArenaAllocator arena(10000);
553
554 struct Align4 { alignas(4) char data[4]; };
555 struct Align8 { alignas(8) char data[8]; };
556 struct Align16 { alignas(16) char data[16]; };
557 struct Align32 { alignas(32) char data[32]; };
558
559 // Allocate in mixed order
560 auto a4 = allocate<Align4>(arena);
561 auto a32 = allocate<Align32>(arena);
562 auto a8 = allocate<Align8>(arena);
563 auto a16 = allocate<Align16>(arena);
564
565 ASSERT_NE(a4, nullptr);
566 ASSERT_NE(a8, nullptr);
567 ASSERT_NE(a16, nullptr);
568 ASSERT_NE(a32, nullptr);
569
570 EXPECT_EQ(reinterpret_cast<uintptr_t>(a4) % 4, 0u);
571 EXPECT_EQ(reinterpret_cast<uintptr_t>(a8) % 8, 0u);
572 EXPECT_EQ(reinterpret_cast<uintptr_t>(a16) % 16, 0u);
573 EXPECT_EQ(reinterpret_cast<uintptr_t>(a32) % 32, 0u);
574}
575
576TEST(ArenaAlignmentAdvanced, VeryLargeAlignment)
577{
578 AhArenaAllocator arena(100000);
579
580 // 256-byte alignment
581 void* ptr = arena.alloc_aligned(100, 256);
582 ASSERT_NE(ptr, nullptr);
583 EXPECT_EQ(reinterpret_cast<uintptr_t>(ptr) % 256, 0u);
584
585 // 512-byte alignment
586 ptr = arena.alloc_aligned(100, 512);
587 ASSERT_NE(ptr, nullptr);
588 EXPECT_EQ(reinterpret_cast<uintptr_t>(ptr) % 512, 0u);
589
590 // 1024-byte alignment
591 ptr = arena.alloc_aligned(100, 1024);
592 ASSERT_NE(ptr, nullptr);
593 EXPECT_EQ(reinterpret_cast<uintptr_t>(ptr) % 1024, 0u);
594}
595
596// ============================================================================
597// Boundary Conditions
598// ============================================================================
599
600TEST(ArenaBoundary, AllocationAtExactCapacity)
601{
602 const size_t SIZE = 1000;
603 AhArenaAllocator arena(SIZE);
604
605 // Fill exactly to capacity with multiple allocations
606 void* p1 = arena.alloc(400);
607 void* p2 = arena.alloc(300);
608 void* p3 = arena.alloc(300);
609
610 ASSERT_NE(p1, nullptr);
611 ASSERT_NE(p2, nullptr);
612 ASSERT_NE(p3, nullptr);
613
614 EXPECT_TRUE(arena.full());
615 EXPECT_EQ(arena.allocated_size(), SIZE);
616 EXPECT_EQ(arena.available_size(), 0u);
617
618 // No more allocations possible
619 void* p4 = arena.alloc(1);
620 EXPECT_EQ(p4, nullptr);
621}
622
623TEST(ArenaBoundary, SingleByteAllocations)
624{
625 const size_t SIZE = 100;
626 AhArenaAllocator arena(SIZE);
627
628 std::vector<void*> ptrs;
629
630 for (size_t i = 0; i < SIZE; ++i)
631 {
632 void* ptr = arena.alloc(1);
633 ASSERT_NE(ptr, nullptr) << "Byte " << i << " failed";
634 ptrs.push_back(ptr);
635 }
636
637 EXPECT_TRUE(arena.full());
638
639 // Verify all pointers are consecutive
640 for (size_t i = 1; i < ptrs.size(); ++i)
641 {
642 EXPECT_EQ(
643 static_cast<char*>(ptrs[i]),
644 static_cast<char*>(ptrs[i-1]) + 1
645 );
646 }
647}
648
649TEST(ArenaBoundary, AlignmentPaddingAtBoundary)
650{
651 const size_t SIZE = 100;
652 AhArenaAllocator arena(SIZE);
653
654 // Allocate most of arena
655 void* p1 = arena.alloc(90);
656 ASSERT_NE(p1, nullptr);
657
658 // Try aligned allocation that requires padding
659 // This might fail due to padding requirements
660 void* p2 = arena.alloc_aligned(5, 16);
661
662 if (p2 != nullptr)
663 {
664 EXPECT_EQ(reinterpret_cast<uintptr_t>(p2) % 16, 0u);
665 }
666 else
667 {
668 // Failure is acceptable if not enough space for alignment
669 EXPECT_LT(arena.available_size(), 21u); // 5 + up to 15 padding
670 }
671}
672
673// ============================================================================
674// LIFO Pattern Tests - Complex
675// ============================================================================
676
677TEST(ArenaLIFO, DeepNesting)
678{
679 // This test allocates 10+20+...+1000 = 50500 bytes.
680 // Use a comfortably larger arena to avoid spurious failures.
681 AhArenaAllocator arena(60000);
682
683 struct Level
684 {
685 void* ptr;
686 size_t requested_size;
687 size_t actual_size; // includes any allocator overhead (e.g. alignment padding)
688 };
689
690 std::vector<Level> stack;
691 stack.reserve(100);
692
693 // Push many levels and record the *actual* consumed bytes so we can
694 // deallocate accurately even if future allocator versions introduce padding.
695 for (int i = 0; i < 100; ++i)
696 {
697 const size_t before = arena.allocated_size();
698 const size_t size = static_cast<size_t>(i + 1) * 10;
699 void* ptr = arena.alloc(size);
700 ASSERT_NE(ptr, nullptr);
701 const size_t after = arena.allocated_size();
702 stack.push_back({ptr, size, after - before});
703 }
704
705 const size_t max_allocated = arena.allocated_size();
706
707 // Pop in LIFO order
708 while (!stack.empty())
709 {
710 Level level = stack.back();
711 stack.pop_back();
712
713 const size_t before = arena.allocated_size();
714 arena.dealloc(level.ptr, level.actual_size);
715 const size_t after = arena.allocated_size();
716
717 EXPECT_EQ(before - after, level.actual_size);
718 }
719
720 EXPECT_TRUE(arena.empty());
721 EXPECT_GT(max_allocated, 45000u);
722}
723
724TEST(ArenaLIFO, InterleavedTypedAllocations)
725{
726 AhArenaAllocator arena(100000);
727
728 std::vector<TinyObject*> tiny_stack;
729 std::vector<MediumObject*> medium_stack;
730
731 // Interleaved pushes
732 for (int i = 0; i < 50; ++i)
733 {
734 tiny_stack.push_back(allocate<TinyObject>(arena, static_cast<char>(i)));
735 medium_stack.push_back(allocate<MediumObject>(arena, i));
736 }
737
738 // Interleaved pops (LIFO per type)
739 while (!medium_stack.empty() || !tiny_stack.empty())
740 {
741 if (!medium_stack.empty())
742 {
743 deallocate(arena, medium_stack.back());
744 medium_stack.pop_back();
745 }
746
747 if (!tiny_stack.empty())
748 {
749 deallocate(arena, tiny_stack.back());
750 tiny_stack.pop_back();
751 }
752 }
753
754 // Interleaving different-sized objects can leave the arena non-empty because
755 // deallocation is only effective for a strict LIFO pattern on the raw blocks.
756 // The supported bulk mechanism is reset().
757 arena.reset();
758 EXPECT_TRUE(arena.empty());
759}
760
761// ============================================================================
762// Performance and Scaling Tests
763// ============================================================================
764
765TEST(ArenaPerformance, LargeScaleAllocation)
766{
767 const size_t NUM_OBJECTS = 50000;
768 AhArenaAllocator arena(NUM_OBJECTS * sizeof(MediumObject));
769
770 auto start = std::chrono::high_resolution_clock::now();
771
772 std::vector<MediumObject*> objects;
773 objects.reserve(NUM_OBJECTS);
774
775 for (size_t i = 0; i < NUM_OBJECTS; ++i)
776 objects.push_back(allocate<MediumObject>(arena, static_cast<int>(i)));
777
778 auto end = std::chrono::high_resolution_clock::now();
779 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
780
781 // Should be fast (bump allocation is O(1))
782 EXPECT_LT(duration.count(), 1000) << "Allocation too slow: " << duration.count() << "ms";
783
784 // Verify all allocated
785 for (size_t i = 0; i < objects.size(); ++i)
786 {
787 ASSERT_NE(objects[i], nullptr);
788 EXPECT_EQ(objects[i]->id, static_cast<int>(i));
789 }
790
791 std::cout << " [Allocated " << NUM_OBJECTS << " objects in "
792 << duration.count() << "ms]" << std::endl;
793}
794
795// The least time of some runs of a function, in nanoseconds: a run that
796// the system interrupts, or that pays the first touch of its memory, does
797// not decide
798template <class Run>
799static long long least_nanoseconds(Run && run, const int runs = 7)
800{
801 long long least = std::numeric_limits<long long>::max();
802 for (int i = 0; i < runs; ++i)
803 {
804 const auto start = std::chrono::steady_clock::now();
805 run();
806 const auto end = std::chrono::steady_clock::now();
807 least = std::min<long long>(
808 least, std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count());
809 }
810 return least;
811}
812
813// What the arena takes to give its blocks, next to malloc() and free(): a
814// bump of a pointer, typically 10 to 100 times as fast. Each side is timed
815// several times and the least times are printed. The arena and the arrays
816// of pointers are made before the clock starts. What is timed, on the side
817// of malloc(), is its calls and those of free(); on the side of the arena,
818// its reset() and the calls of alloc(); and on both, the emptying of the
819// array of pointers, which is reserved already, and the appends to it: the
820// speedup that is printed compares those two. It asserts none: the times
821// of a runner do not decide a job, and in a Debug build the arena is not
822// optimized while the malloc() of the system is.
823TEST(ArenaPerformance, CompareWithMalloc)
824{
825 const size_t NUM_ALLOCS = 10000;
826 const size_t ALLOC_SIZE = 64;
827
828 std::vector<void*> malloc_ptrs;
829 malloc_ptrs.reserve(NUM_ALLOCS);
830 const long long malloc_ns = least_nanoseconds([&]
831 {
832 malloc_ptrs.clear();
833 for (size_t i = 0; i < NUM_ALLOCS; ++i)
834 malloc_ptrs.push_back(std::malloc(ALLOC_SIZE));
835 for (void* ptr : malloc_ptrs)
836 std::free(ptr);
837 });
838
839 AhArenaAllocator arena(NUM_ALLOCS * ALLOC_SIZE);
840 std::vector<void*> arena_ptrs;
841 arena_ptrs.reserve(NUM_ALLOCS);
842 const long long arena_ns = least_nanoseconds([&]
843 {
844 arena.reset();
845 arena_ptrs.clear();
846 for (size_t i = 0; i < NUM_ALLOCS; ++i)
847 arena_ptrs.push_back(arena.alloc(ALLOC_SIZE));
848 });
849
850 ASSERT_EQ(arena_ptrs.size(), NUM_ALLOCS);
851 for (const void* ptr : arena_ptrs)
852 ASSERT_NE(ptr, nullptr);
853
854 std::cout << " [malloc and free: " << malloc_ns / 1000.0 << "μs, "
855 << "arena: " << arena_ns / 1000.0 << "μs, the least of 7 runs; "
856 << "speedup: " << double(malloc_ns) / double(arena_ns > 0 ? arena_ns : 1) << "x]"
857 << std::endl;
858}
859
860// ============================================================================
861// Statistical Validation
862// ============================================================================
863
864TEST(ArenaStatistical, RandomizedStressTest)
865{
866 const size_t NUM_TRIALS = 100;
867 const size_t ARENA_SIZE = 100000;
868
869 std::mt19937 rng(12345);
870 std::uniform_int_distribution<size_t> size_dist(1, 1000);
871 std::bernoulli_distribution dealloc_dist(0.3); // 30% chance to dealloc
872
873 size_t total_allocs = 0;
874 size_t total_deallocs = 0;
875 size_t failed_allocs = 0;
876
877 for (size_t trial = 0; trial < NUM_TRIALS; ++trial)
878 {
879 AhArenaAllocator arena(ARENA_SIZE);
880 std::vector<std::pair<void*, size_t>> allocations;
881
882 for (int op = 0; op < 1000; ++op)
883 {
884 // Randomly allocate or deallocate
885 if (allocations.empty() || !dealloc_dist(rng))
886 {
887 // Allocate
888 size_t size = size_dist(rng);
889 void* ptr = arena.alloc(size);
890
891 if (ptr != nullptr)
892 {
893 allocations.push_back({ptr, size});
894 ++total_allocs;
895 }
896 else
897 {
898 ++failed_allocs;
899 }
900 }
901 else
902 {
903 // Deallocate (LIFO only works)
904 auto [ptr, size] = allocations.back();
905 allocations.pop_back();
906 arena.dealloc(ptr, size);
907 ++total_deallocs;
908 }
909 }
910 }
911
912 std::cout << " [Trials: " << NUM_TRIALS
913 << ", Allocs: " << total_allocs
914 << ", Deallocs: " << total_deallocs
915 << ", Failed: " << failed_allocs << "]" << std::endl;
916
917 EXPECT_GT(total_allocs, NUM_TRIALS * 100);
918 EXPECT_GT(total_deallocs, 0u);
919}
920
921// ============================================================================
922// Edge Cases and Corner Cases
923// ============================================================================
924
925TEST(ArenaEdgeCases, MaxSizeAllocation)
926{
927 const size_t MAX = std::numeric_limits<size_t>::max();
928
929 AhArenaAllocator arena(1024);
930
931 // Try to allocate SIZE_MAX (should fail gracefully)
932 void* ptr = arena.alloc(MAX);
933 EXPECT_EQ(ptr, nullptr);
934
935 // Arena should still be usable
936 ptr = arena.alloc(100);
937 EXPECT_NE(ptr, nullptr);
938}
939
940TEST(ArenaEdgeCases, AlignmentNotPowerOfTwo)
941{
942 AhArenaAllocator arena(1024);
943
944 // Non-power-of-2 alignments (undefined behavior in C++, but test robustness)
945 // The implementation uses bitwise operations that assume power-of-2
946 // This test verifies it doesn't crash, even if result is undefined
947
948 void* ptr1 = arena.alloc_aligned(100, 3); // Not power of 2
949 void* ptr2 = arena.alloc_aligned(100, 7); // Not power of 2
950
951 // Implementation-defined behavior, but should not crash
952 // May return nullptr or incorrectly aligned pointer
953 (void)ptr1;
954 (void)ptr2;
955
956 SUCCEED(); // Didn't crash
957}
958
959TEST(ArenaEdgeCases, ConstBufferUsage)
960{
961 const char buffer[1024] = {}; // Const buffer
962
963 AhArenaAllocator arena(buffer, sizeof(buffer));
964
965 EXPECT_TRUE(arena.is_valid());
966 EXPECT_FALSE(arena.owns_memory());
967
968 // Should be able to allocate (const_cast used internally)
969 void* ptr = arena.alloc(100);
970 EXPECT_NE(ptr, nullptr);
971}
972
973TEST(ArenaEdgeCases, ZeroCapacityArena)
974{
975 char buffer[1] = {}; // Minimal buffer
976 AhArenaAllocator arena(buffer, 0); // Zero capacity
977
978 EXPECT_TRUE(arena.is_valid());
979 EXPECT_EQ(arena.capacity(), 0u);
980 EXPECT_TRUE(arena.empty());
981 EXPECT_TRUE(arena.full());
982
983 void* ptr = arena.alloc(1);
984 EXPECT_EQ(ptr, nullptr);
985}
986
987// ============================================================================
988// Integration Tests
989// ============================================================================
990
991TEST(ArenaIntegration, RealWorldUsagePattern)
992{
993 // Simulate a parser that builds an AST
994 struct ASTNode
995 {
996 std::string token;
997 std::vector<ASTNode*> children;
998 int line_number;
999
1000 ASTNode(std::string t, int line)
1001 : token(std::move(t)), line_number(line)
1002 {}
1003 };
1004
1005 const size_t ESTIMATED_NODES = 1000;
1006 AhArenaAllocator arena(ESTIMATED_NODES * sizeof(ASTNode));
1007
1008 std::vector<ASTNode*> all_nodes;
1009
1010 // Build tree
1011 for (int i = 0; i < 500; ++i)
1012 {
1013 ASTNode* node = allocate<ASTNode>(arena, "token_" + std::to_string(i), i);
1014 ASSERT_NE(node, nullptr);
1015 all_nodes.push_back(node);
1016
1017 // Some nodes have children
1018 if (i > 0 && i % 10 == 0)
1019 {
1020 for (int j = 0; j < 3 && !all_nodes.empty(); ++j)
1021 node->children.push_back(all_nodes[all_nodes.size() - j - 1]);
1022 }
1023 }
1024
1025 // Verify tree structure
1026 int nodes_with_children = 0;
1027 for (ASTNode* node : all_nodes)
1028 {
1029 EXPECT_FALSE(node->token.empty());
1030 EXPECT_GE(node->line_number, 0);
1031 if (!node->children.empty())
1032 ++nodes_with_children;
1033 }
1034
1035 EXPECT_GT(nodes_with_children, 0);
1036
1037 // Cleanup (LIFO)
1038 for (auto it = all_nodes.rbegin(); it != all_nodes.rend(); ++it)
1039 deallocate(arena, *it);
1040
1041 EXPECT_TRUE(arena.empty());
1042}
1043
1044int main(int argc, char** argv)
1045{
1046 ::testing::InitGoogleTest(&argc, argv);
1047 return RUN_ALL_TESTS();
1048}
Memory arena for fast bulk allocations.
static long long least_nanoseconds(Run &&run, const int runs=7)
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Arena allocator for fast bump-pointer allocation.
Definition ah-arena.H:122
bool is_valid() const noexcept
Returns true if the arena is valid (has memory).
Definition ah-arena.H:383
void * alloc(const size_t size) noexcept
Allocate raw memory from the arena.
Definition ah-arena.H:271
void * alloc_aligned(const size_t size, const size_t alignment) noexcept
Allocate aligned memory from the arena.
Definition ah-arena.H:294
void reset() noexcept
Reset arena, making all memory available again.
Definition ah-arena.H:256
void dealloc(const void *addr, const size_t size) noexcept
Deallocate memory (only effective for LIFO pattern).
Definition ah-arena.H:326
bool owns_memory() const noexcept
Returns true if the arena owns its memory.
Definition ah-arena.H:389
size_t capacity() const noexcept
Get total arena capacity.
Definition ah-arena.H:407
size_t allocated_size() const noexcept
Get total bytes currently allocated.
Definition ah-arena.H:395
bool full() const noexcept
Returns true if the arena is full (no space left).
Definition ah-arena.H:419
bool empty() const noexcept
Returns true if the arena is empty (no allocations).
Definition ah-arena.H:413
size_t available_size() const noexcept
Get remaining bytes available.
Definition ah-arena.H:401
#define TEST(name)
static mt19937 rng
Main namespace for Aleph-w library functions.
@ Run
Execute the lowered HIR module with Interpreter_Runtime.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
Matrix< Trow, Tcol, NumType > operator*(const NumType &scalar, const Matrix< Trow, Tcol, NumType > &m)
Scalar-matrix multiplication (scalar * matrix).
Definition al-matrix.H:1228
void deallocate(AhArenaAllocator &arena, T *ptr) noexcept
Destruct and deallocate an object from an arena.
Definition ah-arena.H:494
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)