forked from foonathan/memory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_pool_collection.cpp
More file actions
98 lines (85 loc) · 3.1 KB
/
Copy pathmemory_pool_collection.cpp
File metadata and controls
98 lines (85 loc) · 3.1 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
// Copyright (C) 2015-2023 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("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("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);
}