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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2020 Red Hat
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include "common/allocate_unique.h"
#include <string>
#include <vector>
#include <gtest/gtest.h>
namespace {
// allocation events recorded by logging_allocator
struct event {
size_t size;
bool allocated; // true for allocate(), false for deallocate()
};
using event_log = std::vector<event>;
template <typename T>
struct logging_allocator {
event_log *const log;
using value_type = T;
explicit logging_allocator(event_log *log) : log(log) {}
logging_allocator(const logging_allocator& other) : log(other.log) {}
template <typename U>
logging_allocator(const logging_allocator<U>& other) : log(other.log) {}
T* allocate(size_t n)
{
auto p = std::allocator<T>{}.allocate(n);
log->emplace_back(event{n * sizeof(T), true});
return p;
}
void deallocate(T* p, size_t n)
{
std::allocator<T>{}.deallocate(p, n);
if (p) {
log->emplace_back(event{n * sizeof(T), false});
}
}
};
} // anonymous namespace
namespace ceph {
TEST(AllocateUnique, Allocate)
{
event_log log;
auto alloc = logging_allocator<char>{&log};
{
auto p = allocate_unique<char>(alloc, 'a');
static_assert(std::is_same_v<decltype(p),
std::unique_ptr<char, deallocator<logging_allocator<char>>>>);
ASSERT_TRUE(p);
ASSERT_EQ(1, log.size());
EXPECT_EQ(1, log.front().size);
EXPECT_EQ(true, log.front().allocated);
}
ASSERT_EQ(2, log.size());
EXPECT_EQ(1, log.back().size);
EXPECT_EQ(false, log.back().allocated);
}
TEST(AllocateUnique, RebindAllocate)
{
event_log log;
auto alloc = logging_allocator<char>{&log};
{
auto p = allocate_unique<std::string>(alloc, "a");
static_assert(std::is_same_v<decltype(p),
std::unique_ptr<std::string,
deallocator<logging_allocator<std::string>>>>);
ASSERT_TRUE(p);
ASSERT_EQ(1, log.size());
EXPECT_EQ(sizeof(std::string), log.front().size);
EXPECT_EQ(true, log.front().allocated);
}
ASSERT_EQ(2, log.size());
EXPECT_EQ(sizeof(std::string), log.back().size);
EXPECT_EQ(false, log.back().allocated);
}
} // namespace ceph
|