-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathdefault_allocator.cpp
75 lines (59 loc) · 1.89 KB
/
default_allocator.cpp
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
// Copyright (C) 2015-2021 Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
// tests all possible default allocator classes
#include "default_allocator.hpp"
#include <doctest/doctest.h>
#include <vector>
#include "detail/align.hpp"
using namespace foonathan::memory;
// *very* simple test case to ensure proper alignment and might catch some segfaults
template <class Allocator>
void check_default_allocator(Allocator& alloc, std::size_t def_alignment = detail::max_alignment)
{
auto ptr = alloc.allocate_node(1, 1);
REQUIRE(detail::is_aligned(ptr, def_alignment));
alloc.deallocate_node(ptr, 1, 1);
for (std::size_t i = 0u; i != 10u; ++i)
{
auto node = alloc.allocate_node(i, 1);
REQUIRE(detail::is_aligned(node, def_alignment));
alloc.deallocate_node(node, i, 1);
}
std::vector<void*> nodes;
for (std::size_t i = 0u; i != 10u; ++i)
{
auto node = alloc.allocate_node(i, 1);
REQUIRE(detail::is_aligned(node, def_alignment));
nodes.push_back(node);
}
for (std::size_t i = 0u; i != 10u; ++i)
alloc.deallocate_node(nodes[i], i, 1);
}
TEST_CASE("heap_allocator")
{
heap_allocator alloc;
check_default_allocator(alloc);
}
TEST_CASE("new_allocator")
{
new_allocator alloc;
check_default_allocator(alloc);
}
TEST_CASE("malloc_allocator")
{
malloc_allocator alloc;
check_default_allocator(alloc);
}
TEST_CASE("static_allocator")
{
static_allocator_storage<1024> storage;
static_allocator alloc(storage);
// no need to test alignment issues here again, implemented by fixed_memory_stack
check_default_allocator(alloc, 1);
}
TEST_CASE("virtual_memory_allocator")
{
virtual_memory_allocator alloc;
check_default_allocator(alloc, virtual_memory_page_size);
}