-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_vector_allocator.hpp
More file actions
161 lines (142 loc) · 4.4 KB
/
Copy pathmy_vector_allocator.hpp
File metadata and controls
161 lines (142 loc) · 4.4 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#pragma once
#ifndef my_vector_allocator_hpp
#define my_vector_allocator_hpp
#include <my_vector_help.hpp>
/**/
#include <iostream>
#include <limits>
#include <memory>
/**/
my_vector_namespace_begin
template <size_t _size_of_Ty>
inline constexpr size_t _get_size(const size_t n)
{
constexpr bool _overflow_posibility = _size_of_Ty > 1;
if Constexpr (_overflow_posibility)
{
constexpr size_t _max_n = std::numeric_limits<size_t>::max() / _size_of_Ty;
if (_max_n < n)
{
throw std::bad_array_new_length();
}
}
return n * _size_of_Ty;
}
template <typename _Ty>
inline constexpr _Ty *_do_allocate(size_t n)
{
size_t _do_allocate_lenth = _get_size<sizeof(_Ty)>(n);
if (show_allocate_and_deallocate_log)
{
_Ty *_adress = nullptr;
std::cout << "do allocate called: " << std::endl;
std::cout << " allocate number: " << n << std::endl
<< " number size: " << sizeof(_Ty) << std::endl;
if (_do_allocate_lenth == 0)
{
std::cout << " allocate adress: " << _adress << std::endl
<< " allocate size:" << _do_allocate_lenth << std::endl
<< std::endl;
}
else
{
_adress = static_cast<_Ty *>(::operator new(_do_allocate_lenth));
std::cout << " allocate adress: " << _adress << std::endl
<< " allocate size:" << _do_allocate_lenth << std::endl
<< std::endl;
}
return _adress;
}
else
{
return _do_allocate_lenth == 0 ? nullptr : static_cast<_Ty *>(::operator new(_do_allocate_lenth));
}
}
template <typename _Ty>
inline constexpr void _do_deallocate(_Ty *ptr, size_t n)
{
if (show_allocate_and_deallocate_log)
{
std::cout << "do deallocate called: " << std::endl
<< " deallocate adress: " << ptr << std::endl
<< " deallocate size: " << n << std::endl
<< std::endl;
}
::operator delete(ptr, n);
}
//copy of std::allocator
template <typename Ty>
class vector_allocator
{
public:
using value_type = Ty;
using size_type = size_t;
using difference_type = ptrdiff_t;
using propagate_on_container_move_assignment = std::true_type;
using propagate_on_container_copy_assignment = std::true_type;
using is_always_equal = std::true_type;
using point = Ty *;
public:
vector_allocator() = default;
/*{
std::cout << "vector_allocator constructor called: " << this
<< " size: " << sizeof(*this)
<< std::endl
<< std::endl;
}*/
vector_allocator(const vector_allocator &other) = default;
/*{
std::cout << "vector_alloctor copied called: " << this
<< " size: " << sizeof(*this)
<< std::endl
<< std::endl;
}*/
vector_allocator(vector_allocator &&other) = default;
vector_allocator &operator=(const vector_allocator &other) = default;
~vector_allocator() = default;
/*{
std::cout << "vector_alloctor destroied : " << this
<< " size: " << sizeof(*this)
<< std::endl
<< std::endl;
}*/
public:
[[nodiscard]] constexpr point allocate(size_t n)
{
return _do_allocate<value_type>(n);
};
void constexpr deallocate(point ptr, size_t n)
{
_do_deallocate<value_type>(ptr, n * sizeof(value_type));
};
};
template <typename Ty, typename Other_Ty>
constexpr inline bool operator==(vector_allocator<Ty> &left, vector_allocator<Other_Ty> &right)
{
return false;
}
template <typename Ty>
constexpr inline bool operator==(vector_allocator<Ty> &left, vector_allocator<Ty> &right)
{
if (_allocate_is_always_equal<vector_allocator<Ty>>::value)
{
return true;
}
return std::addressof(left) == std::addressof(right);
}
template <typename Ty, typename Other_Ty>
constexpr inline bool operator!=(vector_allocator<Ty> &left, vector_allocator<Other_Ty> &right)
{
return true;
}
template <typename Ty>
constexpr inline bool operator!=(vector_allocator<Ty> &left, vector_allocator<Ty> &right)
{
if (_allocate_is_always_equal<vector_allocator<Ty>>::value)
{
return false;
}
return std::addressof(left) != std::addressof(right);
}
my_vector_namespace_end
#endif //my_vector_allocator_hpp