File: default_allocator.cpp

package info (click to toggle)
foonathan-memory 0.7.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,644 kB
  • sloc: cpp: 12,425; xml: 139; sh: 48; makefile: 25
file content (87 lines) | stat: -rw-r--r-- 2,320 bytes parent folder | download
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
// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors
// SPDX-License-Identifier: Zlib

// tests all possible default allocator classes

#include <foonathan/memory/default_allocator.hpp>

#include <doctest/doctest.h>
#include <vector>

#include <foonathan/memory/detail/align.hpp>
#include <foonathan/memory/memory_arena.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 = alignof(void*))
{
    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, get_virtual_memory_page_size());
}

TEST_CASE("virtual_block_allocator")
{
    auto const            page_size = get_virtual_memory_page_size();
    constexpr std::size_t no_blocks{8u};

    virtual_block_allocator alloc{page_size, no_blocks};
    auto                    block = alloc.allocate_block();
    REQUIRE(block.memory != nullptr);
    REQUIRE(block.size == page_size);
    alloc.deallocate_block(block);
}