forked from foonathan/memory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_pool.cpp
More file actions
66 lines (55 loc) · 2.28 KB
/
Copy pathmemory_pool.cpp
File metadata and controls
66 lines (55 loc) · 2.28 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
// Copyright (C) 2015-2016 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 "memory_pool.hpp"
#include <algorithm>
#include <catch.hpp>
#include <random>
#include <vector>
#include "allocator_storage.hpp"
#include "test_allocator.hpp"
using namespace foonathan::memory;
// don't test actual node allocationg, but the connection between arena and the implementation
// so only test for memory_pool<node_pool>
TEST_CASE("memory_pool", "[pool]")
{
using pool_type = memory_pool<node_pool, allocator_reference<test_allocator>>;
test_allocator alloc;
{
pool_type pool(4, 100, alloc);
REQUIRE(pool.node_size() >= 4u);
REQUIRE(pool.capacity_left() <= 100u);
REQUIRE(pool.next_capacity() >= 100u);
REQUIRE(alloc.no_allocated() == 1u);
SECTION("normal alloc/dealloc")
{
std::vector<void*> ptrs;
auto capacity = pool.capacity_left();
for (std::size_t i = 0u; i != capacity / pool.node_size(); ++i)
ptrs.push_back(pool.allocate_node());
REQUIRE(pool.capacity_left() == 0u);
REQUIRE(alloc.no_allocated() == 1u);
std::shuffle(ptrs.begin(), ptrs.end(), std::mt19937{});
for (auto ptr : ptrs)
pool.deallocate_node(ptr);
REQUIRE(pool.capacity_left() == capacity);
}
SECTION("multiple block alloc/dealloc")
{
std::vector<void*> ptrs;
auto capacity = pool.capacity_left();
for (std::size_t i = 0u; i != capacity / pool.node_size(); ++i)
ptrs.push_back(pool.allocate_node());
REQUIRE(pool.capacity_left() == 0u);
ptrs.push_back(pool.allocate_node());
REQUIRE(pool.capacity_left() >= capacity - pool.node_size());
REQUIRE(alloc.no_allocated() == 2u);
std::shuffle(ptrs.begin(), ptrs.end(), std::mt19937{});
for (auto ptr : ptrs)
pool.deallocate_node(ptr);
REQUIRE(pool.capacity_left() >= capacity);
REQUIRE(alloc.no_allocated() == 2u);
}
}
REQUIRE(alloc.no_allocated() == 0u);
}