39# include <gtest/gtest.h>
47using namespace testing;
51 static constexpr size_t sz = 1021;
62 void * ptr = arena.
alloc(sz + 1);
63 ASSERT_EQ(ptr,
nullptr);
68 void * ptr = arena.alloc(10);
69 ASSERT_NE(ptr,
nullptr);
71 arena.dealloc(ptr, 10);
72 ASSERT_EQ(arena.allocated_size(), 0);
78 void * ptr = arena.alloc(sz);
79 ASSERT_NE(ptr,
nullptr);
80 ASSERT_EQ(ptr, arena.base_addr());
81 ASSERT_EQ((
void*) ((
char*) ptr + sz), arena.end_addr());
82 ASSERT_EQ(arena.available_size(), 0);
85 void * ptr1 = arena.alloc(1);
86 ASSERT_EQ(ptr1,
nullptr);
92 const char* env_seed = std::getenv(
"ALEPH_TEST_SEED");
93 return env_seed ? std::stoul(env_seed) : 0;
104 const char * addr =
static_cast<const char *
>(arena.alloc(
size));
109 cout <<
"Available size = " << arena.available_size() << endl
110 <<
"Last requested size = " <<
size << endl;
112 ASSERT_EQ(arena.alloc(arena.available_size() + 1),
nullptr);
123 cout <<
"Foo Constructor" << endl;
132 cout <<
"Foo Destructor" << endl;
144 for (
int i = 0;
true; ++i)
146 Foo * ptr = allocate<Foo>(arena,
"hello", i);
151 cout <<
"Allocated " << i <<
" node" << endl;
154 ASSERT_EQ(chunks.
size() *
sizeof(
Foo), arena.allocated_size());
158 cout <<
"Freeing " << ptr->
i << endl;
159 dealloc<Foo>(arena, ptr);
162 ASSERT_EQ(arena.allocated_size(), 0);
163 ASSERT_EQ(arena.available_size(), sz);
168 constexpr size_t n = 1024;
172 for (
int i = 0;
true; ++i)
176 int * ptr = tree.
insert(i);
180 cout <<
"Allocated " << i <<
" node" << endl;
182 catch (bad_alloc & e)
184 cout <<
"Arena limit reached" << endl
191 cout <<
"Inserted " << tree.
size() <<
" entries" << endl;
196 ::testing::InitGoogleTest(&argc, argv);
197 return RUN_ALL_TESTS();
Memory arena for fast bulk allocations.
TEST_F(StaticArenaFixture, simple_fail)
static unsigned long get_seed()
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
Arena allocator for fast bump-pointer allocation.
void * alloc(const size_t size) noexcept
Allocate raw memory from the arena.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
size_t size() const noexcept
Return the number of items of the list, in constant time.
DynList & reverse() noexcept
Dynamic set backed by balanced binary search trees with automatic memory management.
const size_t & size() const
Returns the cardinality of the set.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
size_t arena_allocated_size() const noexcept
Returns the allocated size from the arena (0 if not using arena)
size_t arena_available_size() const noexcept
Returns the available size in the arena (0 if not using arena)
A random number generator with the sequences of GSL's gsl_rng_mt19937.
unsigned long uniform_int(const unsigned long n)
An integer in [0, n), each with the same probability, as gsl_rng_uniform_int(r, n).
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
QuadTree - Hierarchical spatial index for 2D points.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
bool operator<(const Foo &r)
Foo(const char *data, int i)
unique_ptr< char[]> buffer
static constexpr size_t sz
Dynamic set implementations based on balanced binary search trees.