-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathmemory_pool_collection.cpp
More file actions
133 lines (116 loc) · 4.43 KB
/
memory_pool_collection.cpp
File metadata and controls
133 lines (116 loc) · 4.43 KB
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
// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors
// SPDX-License-Identifier: Zlib
#include "memory_pool_collection.hpp"
#include <algorithm>
#include <doctest/doctest.h>
#include <random>
#include <vector>
#include "allocator_storage.hpp"
#include "test_allocator.hpp"
using namespace foonathan::memory;
using namespace detail;
TEST_CASE("memory_pool_collection")
{
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, 4000, alloc);
REQUIRE(pool.max_node_size() == max_size);
REQUIRE(pool.capacity_left() <= 4000u);
REQUIRE(pool.next_capacity() >= 4000u);
REQUIRE(alloc.no_allocated() == 1u);
for (auto i = 0u; i != max_size; ++i)
REQUIRE(pool.pool_capacity_left(i) == 0u);
SUBCASE("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() <= 4000u);
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);
}
SUBCASE("normal alloc/dealloc with traits")
{
std::vector<void*> a, b;
for (auto i = 0u; i != 5u; ++i)
{
a.push_back(allocator_traits<pools>::allocate_node(pool, 1, 8u));
b.push_back(allocator_traits<pools>::allocate_node(pool, 5, 8u));
}
REQUIRE(alloc.no_allocated() == 1u);
REQUIRE(pool.capacity_left() <= 4000u);
std::shuffle(a.begin(), a.end(), std::mt19937{});
std::shuffle(b.begin(), b.end(), std::mt19937{});
for (auto ptr : a)
allocator_traits<pools>::deallocate_node(pool, ptr, 1, 8);
for (auto ptr : b)
allocator_traits<pools>::deallocate_node(pool, ptr, 5, 8u);
}
SUBCASE("single array alloc")
{
auto memory = pool.allocate_array(4, 4);
pool.deallocate_array(memory, 4, 4);
}
SUBCASE("array alloc/dealloc")
{
std::vector<void*> a, b;
for (auto i = 0u; i != 5u; ++i)
{
a.push_back(pool.allocate_array(4, 4));
b.push_back(pool.try_allocate_array(5, 5));
REQUIRE(b.back());
}
REQUIRE(alloc.no_allocated() == 1u);
REQUIRE(pool.capacity_left() <= 4000u);
std::shuffle(a.begin(), a.end(), std::mt19937{});
std::shuffle(b.begin(), b.end(), std::mt19937{});
for (auto ptr : a)
REQUIRE(pool.try_deallocate_array(ptr, 4, 4));
for (auto ptr : b)
pool.deallocate_array(ptr, 5, 5);
}
SUBCASE("array alloc/dealloc with traits")
{
std::vector<void*> a;
for (auto i = 0u; i != 5u; ++i)
{
a.push_back(allocator_traits<pools>::allocate_array(pool, 5, 5, 8u));
REQUIRE(a.back());
}
REQUIRE(alloc.no_allocated() == 1u);
REQUIRE(pool.capacity_left() <= 4000u);
std::shuffle(a.begin(), a.end(), std::mt19937{});
for (auto ptr : a)
allocator_traits<pools>::deallocate_array(pool, ptr, 5, 5, 8u);
}
SUBCASE("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);
}