File: memory_pool_collection.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 (74 lines) | stat: -rw-r--r-- 2,417 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
// 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_pool_collection.hpp>

#include <algorithm>
#include <catch.hpp>
#include <random>
#include <vector>

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

using namespace foonathan::memory;
using namespace detail;

TEST_CASE("memory_pool_collection", "[pool]")
{
    using pools =
        memory_pool_collection<node_pool, identity_buckets, allocator_reference<test_allocator>>;
    test_allocator alloc;
    {
        const auto max_size = 16u;
        pools      pool(max_size, 2000, alloc);
        REQUIRE(pool.max_node_size() == max_size);
        REQUIRE(pool.capacity_left() <= 2000u);
        REQUIRE(pool.next_capacity() >= 2000u);
        REQUIRE(alloc.no_allocated() == 1u);

        for (auto i = 0u; i != max_size; ++i)
            REQUIRE(pool.pool_capacity_left(i) == 0u);

        SECTION("normal alloc/dealloc")
        {
            std::vector<void*> a, b;
            for (auto i = 0u; i != 5u; ++i)
            {
                a.push_back(pool.allocate_node(1));
                b.push_back(pool.try_allocate_node(5));
                REQUIRE(b.back());
            }
            REQUIRE(alloc.no_allocated() == 1u);
            REQUIRE(pool.capacity_left() <= 2000u);

            std::shuffle(a.begin(), a.end(), std::mt19937{});
            std::shuffle(b.begin(), b.end(), std::mt19937{});

            for (auto ptr : a)
                REQUIRE(pool.try_deallocate_node(ptr, 1));
            for (auto ptr : b)
                pool.deallocate_node(ptr, 5);
        }
        SECTION("multiple block alloc/dealloc")
        {
            std::vector<void*> a, b;
            for (auto i = 0u; i != 1000u; ++i)
            {
                a.push_back(pool.allocate_node(1));
                b.push_back(pool.allocate_node(5));
            }
            REQUIRE(alloc.no_allocated() > 1u);

            std::shuffle(a.begin(), a.end(), std::mt19937{});
            std::shuffle(b.begin(), b.end(), std::mt19937{});

            for (auto ptr : a)
                pool.deallocate_node(ptr, 1);
            for (auto ptr : b)
                pool.deallocate_node(ptr, 5);
        }
    }
    REQUIRE(alloc.no_allocated() == 0u);
}