-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug_stateless_allocator.cpp
96 lines (75 loc) · 2.1 KB
/
debug_stateless_allocator.cpp
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
#include <bits/stdc++.h>
template<typename T>
class myallocator
{
public:
using value_type = T;
using pointer = T *;
using size_type = std::size_t;
using is_always_equal = std::true_type;
public:
myallocator()
{
std::cout << this << " constructor, sizeof(T): "
<< sizeof(T) << '\n';
}
template <typename U>
myallocator(const myallocator<U> &other) noexcept
{
(void) other;
}
myallocator(myallocator &&other) noexcept
{
(void) other;
std::cout << this << " move constructor, sizeof(T): "<< sizeof(T) << '\n';
}
myallocator &operator=(myallocator &&other) noexcept
{
(void) other;
std::cout << this << " move assignment, sizeof(T): "<< sizeof(T) << '\n';
return *this;
}
myallocator(const myallocator &other) noexcept
{
(void) other;
std::cout << this << " copy constructor, sizeof(T): "<< sizeof(T) << '\n';
}
myallocator &operator=(const myallocator &other) noexcept
{
(void) other;
std::cout << this << " copy assignment, sizeof(T): "<< sizeof(T) << '\n';
return *this;
}
pointer allocate(size_type n)
{
if (auto ptr = static_cast<pointer>(malloc(sizeof(T) * n)))
{
std::cout << this << " Alloc [" << n << "]: " << ptr << '\n';
return ptr;
}
throw std::bad_alloc();
}
void deallocate(pointer ptr, size_type n)
{
(void) n;
std::cout << this << " Dealloc [" << n << "]: " << ptr << '\n';
free(ptr);
}
};
template <typename T1, typename T2>
bool operator==(const myallocator<T1> &, const myallocator<T2> &)
{ return true; }
template <typename T1, typename T2>
bool operator!=(const myallocator<T1> &, const myallocator<T2> &)
{ return false; }
int main()
{
std::list<int, myallocator<int>> mylist1;
std::list<int, myallocator<int>> mylist2;
mylist1.emplace_back(42);
mylist1.emplace_back(42);
std::vector<int, myallocator<int>> tv;
tv.emplace_back(1);
tv.emplace_back(2);
return 0;
}