added fast allocator implementations

This commit is contained in:
Dominik Tomičević
2015-06-22 14:31:26 +02:00
parent 686275ad30
commit a9a3fc02b0
13 changed files with 250 additions and 16 deletions

68
test/allocator.cpp Normal file
View File

@@ -0,0 +1,68 @@
#include "catch.hpp"
#include "utils/memory/allocator.hpp"
TEST_CASE("A block of integers can be allocated")
{
constexpr int N = 100;
fast_allocator<int> a;
int* xs = a.allocate(N);
for(int i = 0; i < N; ++i)
xs[i] = i;
// can we read them back?
for(int i = 0; i < N; ++i)
REQUIRE(xs[i] == i);
// we should be able to free the memory
a.deallocate(xs, N);
}
TEST_CASE("Allocator should work with structures")
{
struct TestObject
{
TestObject(int a, int b, int c, int d)
: a(a), b(b), c(c), d(d) {}
int a, b, c, d;
};
fast_allocator<TestObject> a;
SECTION("Allocate a single object")
{
auto* test = a.allocate(1);
*test = TestObject(1, 2, 3, 4);
REQUIRE(test->a == 1);
REQUIRE(test->b == 2);
REQUIRE(test->c == 3);
REQUIRE(test->d == 4);
a.deallocate(test, 1);
}
SECTION("Allocate a block of structures")
{
constexpr int N = 8;
auto* tests = a.allocate(N);
// structures should not overlap!
for(int i = 0; i < N; ++i)
tests[i] = TestObject(i, i, i, i);
for(int i = 0; i < N; ++i)
{
REQUIRE(tests[i].a == i);
REQUIRE(tests[i].b == i);
REQUIRE(tests[i].c == i);
REQUIRE(tests[i].d == i);
}
a.deallocate(tests, N);
}
}