-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.h
212 lines (182 loc) · 5.94 KB
/
memory.h
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#ifndef MEMORY_H
#define MEMORY_H
// 这个头文件负责更高级的动态内存管理
// 包含一些基本函数、空间配置器、未初始化的存储空间管理,以及一个模板类auto_ptr
#include <cstddef>
#include <cstdlib>
#include <climits>
#include "algobase.h"
#include "allocator.h"
#include "construct.h"
#include "uninitialized.h"
namespace mystl
{
namespace mystl
{
// 获取对象地址
template <class Tp>
constexpr Tp* address_of(Tp& value) noexcept
{
return &value;
}
// 获取 / 释放 临时缓冲区
template <class T>
pair<T*, ptrdiff_t> get_buffer_helper(ptrdiff_t len, T*)
{
if (len > static_cast<ptrdiff_t>(INT_MAX / sizeof(T)))
len = INT_MAX / sizeof(T);
while (len > 0)
{
T* tmp = static_cast<T*>(malloc(static_cast<size_t>(len) * sizeof(T)));
if (tmp)
return pair<T*, ptrdiff_t>(tmp, len);
len /= 2; // 申请失败时减少 len 的大小
}
return pair<T*, ptrdiff_t>(nullptr, 0);
}
template <class T>
pair<T*, ptrdiff_t> get_temporary_buffer(ptrdiff_t len)
{
return get_buffer_helper(len, static_cast<T*>(0));
}
template <class T>
pair<T*, ptrdiff_t> get_temporary_buffer(ptrdiff_t len, T*)
{
return get_buffer_helper(len, static_cast<T*>(0));
}
template <class T>
void release_temporary_buffer(T* ptr)
{
free(ptr);
}
// --------------------------------------------------------------------------------------
// 类模板 : temporary_buffer
// 进行临时缓冲区的申请与释放
template <class ForwardIterator, class T>
class temporary_buffer
{
private:
ptrdiff_t original_len; // 缓冲区申请的大小
ptrdiff_t len; // 缓冲区实际的大小
T* buffer; // 指向缓冲区的指针
public:
// 构造、析构函数
temporary_buffer(ForwardIterator first, ForwardIterator last);
~temporary_buffer()
{
mystl::destroy(buffer, buffer + len);
free(buffer);
}
public:
ptrdiff_t size() const noexcept { return len; }
ptrdiff_t requested_size() const noexcept { return original_len; }
T* begin() noexcept { return buffer; }
T* end() noexcept { return buffer + len; }
private:
void allocate_buffer();
void initialize_buffer(const T&, std::true_type) {}
void initialize_buffer(const T& value, std::false_type)
{
mystl::uninitialized_fill_n(buffer, len, value);
}
private:
temporary_buffer(const temporary_buffer&);
void operator=(const temporary_buffer&);
};
// 构造函数
template <class ForwardIterator, class T>
temporary_buffer<ForwardIterator, T>::
temporary_buffer(ForwardIterator first, ForwardIterator last)
{
try
{
len = mystl::distance(first, last);
allocate_buffer();
if (len > 0)
{
initialize_buffer(*first, std::is_trivially_default_constructible<T>());
}
}
catch (...)
{
free(buffer);
buffer = nullptr;
len = 0;
}
}
// allocate_buffer 函数
template <class ForwardIterator, class T>
void temporary_buffer<ForwardIterator, T>::allocate_buffer()
{
original_len = len;
if (len > static_cast<ptrdiff_t>(INT_MAX / sizeof(T)))
len = INT_MAX / sizeof(T);
while (len > 0)
{
buffer = static_cast<T*>(malloc(len * sizeof(T)));
if (buffer)
break;
len /= 2; // 申请失败时减少申请空间大小
}
}
// --------------------------------------------------------------------------------------
// 模板类: auto_ptr
// 一个具有严格对象所有权的小型智能指针
template <class T>
class auto_ptr
{
public:
typedef T elem_type;
private:
T* m_ptr; // 实际指针
public:
// 构造、复制、析构函数
explicit auto_ptr(T* p = nullptr) :m_ptr(p) {}
auto_ptr(auto_ptr& rhs) :m_ptr(rhs.release()) {}
template <class U>
auto_ptr(auto_ptr<U>& rhs) : m_ptr(rhs.release()) {}
auto_ptr& operator=(auto_ptr& rhs)
{
if (this != &rhs)
{
delete m_ptr;
m_ptr = rhs.release();
}
return *this;
}
template <class U>
auto_ptr& operator=(auto_ptr<U>& rhs)
{
if (this->get() != rhs.get())
{
delete m_ptr;
m_ptr = rhs.release();
}
return *this;
}
~auto_ptr() { delete m_ptr; }
public:
// 重载 operator* 和 operator->
T& operator*() const { return *m_ptr; }
T* operator->() const { return m_ptr; }
// 获得指针
T* get() const { return m_ptr; }
// 释放指针
T* release()
{
T* tmp = m_ptr;
m_ptr = nullptr;
return tmp;
}
// 重置指针
void reset(T* p = nullptr)
{
if (m_ptr != p)
{
delete m_ptr;
m_ptr = p;
}
}
};
} // namespace mystl
#endif // !MEMORY_H