File: memory_stack.cpp

package info (click to toggle)
foonathan-memory 0.7-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,748 kB
  • sloc: cpp: 12,014; xml: 139; sh: 49; makefile: 22
file content (150 lines) | stat: -rw-r--r-- 4,449 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
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
// Copyright (C) 2015-2020 Jonathan 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.

#include <foonathan/memory/memory_stack.hpp>

#include <catch.hpp>

#include <foonathan/memory/allocator_storage.hpp>
#include "test_allocator.hpp"

using namespace foonathan::memory;

TEST_CASE("memory_stack", "[stack]")
{
    test_allocator alloc;

    using stack_type = memory_stack<allocator_reference<test_allocator>>;
    stack_type stack(stack_type::min_block_size(100), alloc);
    REQUIRE(alloc.no_allocated() == 1u);
    REQUIRE(stack.capacity_left() == 100);
    auto capacity = stack.capacity_left();

    SECTION("empty unwind")
    {
        auto m = stack.top();
        stack.unwind(m);
        REQUIRE(capacity <= 100);
        REQUIRE(alloc.no_allocated() == 1u);
        REQUIRE(alloc.no_deallocated() == 0u);
    }
    SECTION("normal allocation/unwind")
    {
        stack.allocate(10, 1);
        REQUIRE(stack.capacity_left() == capacity - 10 - 2 * detail::debug_fence_size);

        auto m = stack.top();

        auto memory = stack.allocate(10, 16);
        REQUIRE(detail::is_aligned(memory, 16));

        stack.unwind(m);
        REQUIRE(stack.capacity_left() == capacity - 10 - 2 * detail::debug_fence_size);

        REQUIRE(stack.allocate(10, 16) == memory);
        REQUIRE(alloc.no_allocated() == 1u);
        REQUIRE(alloc.no_deallocated() == 0u);
    }
    SECTION("multiple block allocation/unwind")
    {
        // note: tests are mostly hoping not to get a segfault

        stack.allocate(10, 1);
        auto m = stack.top();

        auto old_next = stack.next_capacity();

        stack.allocate(100, 1);
        REQUIRE(stack.next_capacity() > old_next);
        REQUIRE(alloc.no_allocated() == 2u);
        REQUIRE(alloc.no_deallocated() == 0u);

        auto m2 = stack.top();
        REQUIRE(m < m2);
        stack.allocate(10, 1);
        stack.unwind(m2);
        stack.allocate(20, 1);

        stack.unwind(m);
        REQUIRE(alloc.no_allocated() == 2u);
        REQUIRE(alloc.no_deallocated() == 0u);

        stack.allocate(10, 1);

        stack.shrink_to_fit();
        REQUIRE(alloc.no_allocated() == 1u);
        REQUIRE(alloc.no_deallocated() == 1u);
    }
    SECTION("move")
    {
        auto other = detail::move(stack);
        auto m     = other.top();
        other.allocate(10, 1);
        REQUIRE(alloc.no_allocated() == 1u);

        stack.allocate(10, 1);
        REQUIRE(alloc.no_allocated() == 2u);

        stack = detail::move(other);
        REQUIRE(alloc.no_allocated() == 1u);
        stack.unwind(m);
    }
    SECTION("marker comparision")
    {
        auto m1 = stack.top();
        auto m2 = stack.top();
        REQUIRE(m1 == m2);

        stack.allocate(1, 1);
        auto m3 = stack.top();
        REQUIRE(m1 < m3);

        stack.unwind(m2);
        REQUIRE(stack.top() == m2);
    }
    SECTION("unwinder")
    {
        auto m = stack.top();
        {
            memory_stack_raii_unwind<decltype(stack)> unwind(stack);
            stack.allocate(10, 1);
            REQUIRE(unwind.will_unwind());
            REQUIRE(&unwind.get_stack() == &stack);
            REQUIRE(unwind.get_marker() == m);
        }
        REQUIRE(stack.top() == m);

        memory_stack_raii_unwind<decltype(stack)> unwind(stack);
        stack.allocate(10, 1);
        unwind.unwind();
        REQUIRE(stack.top() == m);
        REQUIRE(unwind.will_unwind());

        {
            memory_stack_raii_unwind<decltype(stack)> unwind2(stack);
            stack.allocate(10, 1);
            unwind2.release();
            REQUIRE(!unwind2.will_unwind());
        }
        REQUIRE(stack.top() > m);
        m = stack.top();

        unwind.release(); // need to release
        unwind = memory_stack_raii_unwind<decltype(stack)>(stack);
        REQUIRE(unwind.will_unwind());
        REQUIRE(unwind.get_marker() == m);
        REQUIRE(&unwind.get_stack() == &stack);
        auto unwind2 = detail::move(unwind);
        REQUIRE(unwind2.will_unwind());
        REQUIRE(&unwind2.get_stack() == &stack);
        REQUIRE(unwind2.get_marker() == m);
        REQUIRE(!unwind.will_unwind());
    }
    SECTION("overaligned")
    {
        auto align = 2 * detail::max_alignment;
        auto mem   = stack.allocate(align, align);
        REQUIRE(detail::is_aligned(mem, align));
    }
}