Aleph-w 3.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 <gsl/gsl_rng.h>
40# include <gtest/gtest.h>
41
42# include <ah-arena.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
57};
58
60{
61 // Allocating more than available should fail
62 void * ptr = arena.alloc(sz + 1);
63 ASSERT_EQ(ptr, nullptr);
64}
65
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
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 gsl_rng_set(r, get_seed());
101 size_t size = 0;
102 while (true)
103 {
104 size = 1 + gsl_rng_uniform_int(r, sz - 2);
105 const char * addr = static_cast<const char *>(arena.alloc(size));
106 if (addr == nullptr)
107 break;
108 }
109
110 cout << "Available size = " << arena.available_size() << endl
111 << "Last requested size = " << size << endl;
112
113 ASSERT_EQ(arena.alloc(arena.available_size() + 1), nullptr);
114
115 gsl_rng_free(r);
116}
117
118struct Foo
119{
120 string * data_ptr = nullptr;
121 int i = -1;
122
123 Foo(const char * data, int i)
124 : data_ptr(new string(data)), i(i)
125 {
126 cout << "Foo Constructor" << endl;
127 }
128
129 Foo() {}
130
132 {
133 if (data_ptr != nullptr)
134 delete data_ptr;
135 cout << "Foo Destructor" << endl;
136 }
137
138 bool operator < (const Foo rhs) const
139 {
140 return i < rhs.i;
141 }
142};
143
145{
147 for (int i = 0; true; ++i)
148 {
149 Foo * ptr = allocate<Foo>(arena, "hello", i);
150 if (ptr == nullptr)
151 break;
152
153 chunks.append(ptr);
154 cout << "Allocated " << i << " node" << endl;
155 }
156
157 ASSERT_EQ(chunks.size() * sizeof(Foo), arena.allocated_size());
158
159 chunks.reverse().for_each([this] (Foo * ptr)
160 {
161 cout << "Freeing " << ptr->i << endl;
162 dealloc<Foo>(arena, ptr);
163 });
164
165 ASSERT_EQ(arena.allocated_size(), 0);
166 ASSERT_EQ(arena.available_size(), sz);
167}
168
169TEST(Tree, tree)
170{
172 gsl_rng_set(r, get_seed());
173
174 constexpr size_t n = 1024;
175 char buf[n];
176 DynSetTree<int> tree(buf, n);
177
178 for (int i = 0; true; ++i)
179 {
180 try
181 {
182 int * ptr = tree.insert(i);
183 if (ptr == nullptr)
184 break;
185
186 cout << "Allocated " << i << " node" << endl;
187 }
188 catch (bad_alloc & e)
189 {
190 cout << "Arena limit reached" << endl
191 << "Allocated =" << tree.arena_allocated_size() << endl
192 << "Available = " << tree.arena_available_size() << endl;
193 break;
194 }
195 }
196
197 cout << "Inserted " << tree.size() << " entries" << endl;
198
199 gsl_rng_free(r);
200}
201
202int main(int argc, char **argv)
203{
204 ::testing::InitGoogleTest(&argc, argv);
205 return RUN_ALL_TESTS();
206}
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
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
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
T & append(const T &item)
Append a new item by copy.
Definition htlist.H:1562
DynList & reverse() noexcept
Definition htlist.H:1763
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)
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:685
#define TEST(name)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
size_t size(Node *root) noexcept
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
string * data_ptr
Definition ah-arena.cc:120
int i
bool operator<(const Foo rhs) const
Definition ah-arena.cc:138
Foo()
Definition ah-arena.cc:129
~Foo()
Definition ah-arena.cc:131
Foo(const char *data, int i)
Definition ah-arena.cc:123
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
Dynamic set implementations based on balanced binary search trees.