Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-arena.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
33
38# include <cstdlib>
39# include <gtest/gtest.h>
40
41# include <ah-arena.H>
42# include <ah-random.H>
43# include <tpl_dynSetTree.H>
44
45using namespace std;
46using namespace Aleph;
47using namespace testing;
48
49struct StaticArenaFixture : public Test
50{
51 static constexpr size_t sz = 1021;
52 unique_ptr<char[]> buffer;
54
56 : buffer(new char[sz]), arena(buffer.get(), sz) {}
57};
58
60{
61 // Allocating more than available should fail
62 void * ptr = arena.alloc(sz + 1);
63 ASSERT_EQ(ptr, nullptr);
64}
65
66TEST_F(StaticArenaFixture, free_under_lifo_order)
67{
68 void * ptr = arena.alloc(10);
69 ASSERT_NE(ptr, nullptr);
70
71 arena.dealloc(ptr, 10);
72 ASSERT_EQ(arena.allocated_size(), 0);
73}
74
75TEST_F(StaticArenaFixture, one_alloc_next_fail)
76{
77 // Allocate all available space
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);
83
84 // Now any allocation should fail
85 void * ptr1 = arena.alloc(1);
86 ASSERT_EQ(ptr1, nullptr);
87}
88
89// Default seed for random tests (can be overridden via environment)
90static unsigned long get_seed()
91{
92 const char* env_seed = std::getenv("ALEPH_TEST_SEED");
93 return env_seed ? std::stoul(env_seed) : 0;
94}
95
97{
100 size_t size = 0;
101 while (true)
102 {
103 size = 1 + r.uniform_int(sz - 2);
104 const char * addr = static_cast<const char *>(arena.alloc(size));
105 if (addr == nullptr)
106 break;
107 }
108
109 cout << "Available size = " << arena.available_size() << endl
110 << "Last requested size = " << size << endl;
111
112 ASSERT_EQ(arena.alloc(arena.available_size() + 1), nullptr);
113}
114
115struct Foo
116{
117 string * data_ptr = nullptr;
118 int i = -1;
119
120 Foo(const char * data, int i)
121 : data_ptr(new string(data)), i(i)
122 {
123 cout << "Foo Constructor" << endl;
124 }
125
126 Foo() {}
127
129 {
130 if (data_ptr != nullptr)
131 delete data_ptr;
132 cout << "Foo Destructor" << endl;
133 }
134
135 bool operator < (const Foo rhs) const
136 {
137 return i < rhs.i;
138 }
139};
140
142{
143 DynList<Foo*> chunks;
144 for (int i = 0; true; ++i)
145 {
146 Foo * ptr = allocate<Foo>(arena, "hello", i);
147 if (ptr == nullptr)
148 break;
149
150 chunks.append(ptr);
151 cout << "Allocated " << i << " node" << endl;
152 }
153
154 ASSERT_EQ(chunks.size() * sizeof(Foo), arena.allocated_size());
155
156 chunks.reverse().for_each([this] (Foo * ptr)
157 {
158 cout << "Freeing " << ptr->i << endl;
159 dealloc<Foo>(arena, ptr);
160 });
161
162 ASSERT_EQ(arena.allocated_size(), 0);
163 ASSERT_EQ(arena.available_size(), sz);
164}
165
166TEST(Tree, tree)
167{
168 constexpr size_t n = 1024;
169 char buf[n];
170 DynSetTree<int> tree(buf, n);
171
172 for (int i = 0; true; ++i)
173 {
174 try
175 {
176 int * ptr = tree.insert(i);
177 if (ptr == nullptr)
178 break;
179
180 cout << "Allocated " << i << " node" << endl;
181 }
182 catch (bad_alloc & e)
183 {
184 cout << "Arena limit reached" << endl
185 << "Allocated =" << tree.arena_allocated_size() << endl
186 << "Available = " << tree.arena_available_size() << endl;
187 break;
188 }
189 }
190
191 cout << "Inserted " << tree.size() << " entries" << endl;
192}
193
194int main(int argc, char **argv)
195{
196 ::testing::InitGoogleTest(&argc, argv);
197 return RUN_ALL_TESTS();
198}
Memory arena for fast bulk allocations.
TEST_F(StaticArenaFixture, simple_fail)
Definition ah-arena.cc:59
static unsigned long get_seed()
Definition ah-arena.cc:90
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
int main()
Arena allocator for fast bump-pointer allocation.
Definition ah-arena.H:122
void * alloc(const size_t size) noexcept
Allocate raw memory from the arena.
Definition ah-arena.H:271
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
DynList & reverse() noexcept
Definition htlist.H:1728
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.
Definition ah-random.H:147
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).
Definition ah-random.H:302
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
#define TEST(name)
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
STL namespace.
string * data_ptr
Definition ah-arena.cc:117
int i
Foo()
Definition ah-arena.cc:126
bool operator<(const Foo &r)
~Foo()
Definition ah-arena.cc:128
Foo(const char *data, int i)
Definition ah-arena.cc:120
unique_ptr< char[]> buffer
Definition ah-arena.cc:52
AhArenaAllocator arena
Definition ah-arena.cc:53
static constexpr size_t sz
Definition ah-arena.cc:51
Dnode< int > Test
Definition testDnode.C:42
Gsl_Compatible_Rng r
Dynamic set implementations based on balanced binary search trees.