1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
#include "kernel/memory/bitmap_allocator.hpp"
#include "kapi/memory.hpp"
#include "catch2/matchers/catch_matchers.hpp"
#include "catch2/matchers/catch_matchers_range_equals.hpp"
#include <catch2/catch_test_macros.hpp>
#include <cstdint>
#include <limits>
#include <ranges>
#include <span>
#include <vector>
constexpr auto all_bits_set = std::numeric_limits<std::uint64_t>::max();
constexpr auto available_frames = 1024uz;
SCENARIO("Bitmap allocator construction and initialization", "[memory][bitmap_allocator]")
{
GIVEN("A storage region")
{
auto storage = std::vector(available_frames / 64, 0uz);
WHEN("constructing the allocator with 0 frames")
{
auto allocator = kernel::memory::bitmap_frame_allocator{std::span(storage), 0};
THEN("the storage region is not modified")
{
REQUIRE_THAT(storage, Catch::Matchers::RangeEquals(std::vector(16, 0uz)));
}
}
WHEN("constructing with 1 frame")
{
auto allocator = kernel::memory::bitmap_frame_allocator{std::span(storage), 1};
THEN("the first word of the storage region is set to all ones")
{
REQUIRE_THAT(std::views::take(storage, 1), Catch::Matchers::RangeEquals(std::vector(1, all_bits_set)));
}
THEN("the rest of the storage region is not modified")
{
REQUIRE_THAT(std::views::drop(storage, 1), Catch::Matchers::RangeEquals(std::vector(15, 0uz)));
}
}
WHEN("constructing with 64 frames")
{
auto allocator = kernel::memory::bitmap_frame_allocator{std::span(storage), 64};
THEN("the first word of the storage region is set to all ones")
{
REQUIRE_THAT(std::views::take(storage, 1), Catch::Matchers::RangeEquals(std::vector(1, all_bits_set)));
}
THEN("the rest of the storage region is not modified")
{
REQUIRE_THAT(std::views::drop(storage, 1), Catch::Matchers::RangeEquals(std::vector(15, 0uz)));
}
}
WHEN("constructing with all available frames")
{
auto allocator = kernel::memory::bitmap_frame_allocator{std::span(storage), available_frames};
THEN("the storage region is filled with all ones")
{
REQUIRE_THAT(storage, Catch::Matchers::RangeEquals(std::vector(16, all_bits_set)));
}
}
WHEN("constructing with half the available frames")
{
auto allocator = kernel::memory::bitmap_frame_allocator{std::span(storage), available_frames / 2};
THEN("the first half of the storage region is filled with all ones")
{
REQUIRE_THAT(std::views::take(storage, (available_frames / 2) / 64),
Catch::Matchers::RangeEquals(std::vector((available_frames / 2) / 64, all_bits_set)));
}
THEN("the second half of the storage region is filled with all zeros")
{
REQUIRE_THAT(std::views::drop(storage, (available_frames / 2) / 64),
Catch::Matchers::RangeEquals(std::vector((available_frames / 2) / 64, 0uz)));
}
}
}
}
SCENARIO("Bitmap allocator frame allocation", "[memory][bitmap_allocator]")
{
GIVEN("A storage region")
{
auto storage = std::vector(available_frames / 64, 0uz);
AND_GIVEN("an allocator constructed with all available frames but no free ones")
{
auto allocator = kernel::memory::bitmap_frame_allocator{std::span(storage), available_frames};
WHEN("allocating 1 frame")
{
auto result = allocator.allocate_many(1);
THEN("the result is empty")
{
REQUIRE_FALSE(result.has_value());
}
}
}
AND_GIVEN("an allocator constructed with all available frames but only one free one")
{
auto allocator = kernel::memory::bitmap_frame_allocator{std::span(storage), available_frames};
allocator.release_many({kapi::memory::frame{0}, 1});
WHEN("allocating 1 frame")
{
auto result = allocator.allocate_many(1);
THEN("the result is not empty")
{
REQUIRE(result.has_value());
}
THEN("the result contains 1 frame")
{
REQUIRE(result->second == 1);
}
}
WHEN("allocating more frames than are free")
{
auto result = allocator.allocate_many(2);
THEN("the result is empty")
{
REQUIRE_FALSE(result.has_value());
}
AND_WHEN("allocating a single frame")
{
auto result = allocator.allocate_many(1);
THEN("the result is not empty")
{
REQUIRE(result.has_value());
}
}
WHEN("allocating 0 frames")
{
auto result = allocator.allocate_many(0);
THEN("the result is empty")
{
REQUIRE_FALSE(result.has_value());
}
}
}
}
AND_GIVEN("an allocator with many single frame holes")
{
auto allocator = kernel::memory::bitmap_frame_allocator{std::span(storage), available_frames};
for (auto i = 0uz; i < available_frames; i += 2)
{
allocator.release_many({kapi::memory::frame{i}, 1});
}
WHEN("allocating 1 frame")
{
auto result = allocator.allocate_many(1);
THEN("the result is not empty")
{
REQUIRE(result.has_value());
}
THEN("the result contains 1 frame")
{
REQUIRE(result->second == 1);
}
}
WHEN("allocating 2 frames")
{
auto result = allocator.allocate_many(2);
THEN("the result is empty")
{
REQUIRE_FALSE(result.has_value());
}
}
}
AND_GIVEN("and allocator with all frames marked as free")
{
auto allocator = kernel::memory::bitmap_frame_allocator{std::span(storage), available_frames};
allocator.release_many({kapi::memory::frame{0}, available_frames});
WHEN("allocating 1 frame")
{
auto result = allocator.allocate_many(1);
THEN("the result is not empty")
{
REQUIRE(result.has_value());
}
THEN("the result contains 1 frame")
{
REQUIRE(result->second == 1);
}
}
WHEN("allocating multiple frames")
{
auto result = allocator.allocate_many(20);
THEN("the result is not empty")
{
REQUIRE(result.has_value());
}
THEN("the result contains 20 frames")
{
REQUIRE(result->second == 20);
}
}
WHEN("marking all frames as used")
{
for (auto i = 0uz; i < available_frames; i++)
{
allocator.mark_used(kapi::memory::frame{i});
}
THEN("the allocator has no free frames")
{
REQUIRE_FALSE(allocator.allocate_many(1).has_value());
}
}
}
AND_GIVEN("an allocator with a contiguous block of free frames")
{
auto allocator = kernel::memory::bitmap_frame_allocator{std::span(storage), available_frames};
allocator.release_many({kapi::memory::frame{0}, available_frames});
for (auto i = 0uz; i < available_frames / 2; i++)
{
allocator.mark_used(kapi::memory::frame{i});
}
WHEN("allocating a single frame")
{
auto result = allocator.allocate_many(1);
THEN("the result is not empty")
{
REQUIRE(result.has_value());
}
THEN("the result contains 1 frame")
{
REQUIRE(result->second == 1);
}
}
WHEN("allocating multiple frames")
{
auto result = allocator.allocate_many(20);
THEN("the result is not empty")
{
REQUIRE(result.has_value());
}
THEN("the result contains 20 frames")
{
REQUIRE(result->second == 20);
}
}
}
}
}
|