File: memory_stack.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 (85 lines) | stat: -rw-r--r-- 2,802 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
// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors
// SPDX-License-Identifier: Zlib

#include <foonathan/memory/detail/memory_stack.hpp>

#include <doctest/doctest.h>

#include <foonathan/memory/detail/align.hpp>
#include <foonathan/memory/detail/debug_helpers.hpp>
#include <foonathan/memory/detail/utility.hpp>
#include <foonathan/memory/static_allocator.hpp>

using namespace foonathan::memory;
using namespace detail;

TEST_CASE("detail::fixed_memory_stack")
{
    fixed_memory_stack stack;
    REQUIRE(stack.top() == nullptr);

    SUBCASE("allocate")
    {
        static_allocator_storage<1024> memory;
        stack    = fixed_memory_stack{&memory};
        auto end = stack.top() + 1024;

        REQUIRE(stack.top() == reinterpret_cast<char*>(&memory));

        SUBCASE("alignment for allocate")
        {
            auto ptr = stack.allocate(end, 13, 1u);
            REQUIRE(ptr);
            REQUIRE(is_aligned(ptr, 1u));

            ptr = stack.allocate(end, 10, 2u);
            REQUIRE(ptr);
            REQUIRE(is_aligned(ptr, 2u));

            ptr = stack.allocate(end, 10, max_alignment);
            REQUIRE(ptr);
            REQUIRE(is_aligned(ptr, max_alignment));

            ptr = stack.allocate(end, 10, 2 * max_alignment);
            REQUIRE(ptr);
            REQUIRE(is_aligned(ptr, 2 * max_alignment));
        }
        SUBCASE("allocate/unwind")
        {
            REQUIRE(stack.allocate(end, 10u, 1u));
            auto diff = std::size_t(stack.top() - reinterpret_cast<char*>(&memory));
            REQUIRE(diff == 2 * debug_fence_size + 10u);

            REQUIRE(stack.allocate(end, 16u, 1u));
            auto diff2 = std::size_t(stack.top() - reinterpret_cast<char*>(&memory));
            REQUIRE(diff2 == 2 * debug_fence_size + 16u + diff);

            stack.unwind(reinterpret_cast<char*>(&memory) + diff);
            REQUIRE(stack.top() == reinterpret_cast<char*>(&memory) + diff);

            auto top = stack.top();
            REQUIRE(!stack.allocate(end, 1024, 1));
            REQUIRE(stack.top() == top);
        }
    }
    SUBCASE("move")
    {
        static_allocator_storage<1024> memory;
        auto                           end = reinterpret_cast<char*>(&memory) + 1024;

        fixed_memory_stack other(reinterpret_cast<char*>(&memory));
        REQUIRE(other.top() == reinterpret_cast<char*>(&memory));

        stack = detail::move(other);
        REQUIRE(stack.top() == reinterpret_cast<char*>(&memory));

        REQUIRE(!other.allocate(end, 10, 1));
        REQUIRE(stack.allocate(end, 10, 1));
        auto top = stack.top();

        other = detail::move(stack);
        REQUIRE(other.top() == top);
        REQUIRE(!stack.allocate(end, 10, 1));
        REQUIRE(other.allocate(end, 10, 1));
    }
}