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

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

#include <doctest/doctest.h>

#include <foonathan/memory/detail/free_list.hpp>
#include <foonathan/memory/detail/small_free_list.hpp>
#include <foonathan/memory/static_allocator.hpp>

using namespace foonathan::memory;
using namespace detail;

TEST_CASE("detail::log2_access_policy")
{
    using ap = detail::log2_access_policy;
    REQUIRE(ap::index_from_size(1) == 0u);
    REQUIRE(ap::index_from_size(2) == 1u);
    REQUIRE(ap::index_from_size(3) == 2u);
    REQUIRE(ap::index_from_size(4) == 2u);
    REQUIRE(ap::index_from_size(5) == 3u);
    REQUIRE(ap::index_from_size(6) == 3u);
    REQUIRE(ap::index_from_size(8) == 3u);
    REQUIRE(ap::index_from_size(9) == 4u);

    REQUIRE(ap::size_from_index(0) == 1u);
    REQUIRE(ap::size_from_index(1) == 2u);
    REQUIRE(ap::size_from_index(2) == 4u);
    REQUIRE(ap::size_from_index(3) == 8u);
}

TEST_CASE("detail::free_list_array")
{
    static_allocator_storage<1024> memory;
    detail::fixed_memory_stack     stack(&memory);
    SUBCASE("power of two max size, small list")
    {
        using array =
            detail::free_list_array<detail::small_free_memory_list, detail::log2_access_policy>;
        array arr(stack, stack.top() + 1024, 16);
        REQUIRE(arr.max_node_size() == 16u);
        REQUIRE(arr.size() == 5u);

        REQUIRE(arr.get(1u).node_size() == 1u);
        REQUIRE(arr.get(2u).node_size() == 2u);
        REQUIRE(arr.get(3u).node_size() == 4u);
        REQUIRE(arr.get(4u).node_size() == 4u);
        REQUIRE(arr.get(5u).node_size() == 8u);
        REQUIRE(arr.get(9u).node_size() == 16u);
        REQUIRE(arr.get(16u).node_size() == 16u);
    }
    SUBCASE("non power of two max size, small list")
    {
        using array =
            detail::free_list_array<detail::small_free_memory_list, detail::log2_access_policy>;
        array arr(stack, stack.top() + 1024, 15);
        REQUIRE(arr.max_node_size() == 16u);
        REQUIRE(arr.size() == 5u);

        REQUIRE(arr.get(1u).node_size() == 1u);
        REQUIRE(arr.get(2u).node_size() == 2u);
        REQUIRE(arr.get(3u).node_size() == 4u);
        REQUIRE(arr.get(4u).node_size() == 4u);
        REQUIRE(arr.get(5u).node_size() == 8u);
        REQUIRE(arr.get(9u).node_size() == 16u);
        REQUIRE(arr.get(15u).node_size() == 16u);
    }
    SUBCASE("non power of two max size, normal list")
    {
        using array = detail::free_list_array<detail::free_memory_list, detail::log2_access_policy>;
        array arr(stack, stack.top() + 1024, 15);
        REQUIRE(arr.max_node_size() == 16u);
        REQUIRE(arr.size() <= 5u);

        REQUIRE(arr.get(1u).node_size() == detail::free_memory_list::min_element_size);
        REQUIRE(arr.get(2u).node_size() == detail::free_memory_list::min_element_size);
        REQUIRE(arr.get(9u).node_size() == 16u);
        REQUIRE(arr.get(15u).node_size() == 16u);
    }
}