-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common.h
282 lines (251 loc) · 4.83 KB
/
Common.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#pragma once
#include <iostream>
#include <unordered_map>
#include <map>
#include <thread>
#include <mutex>
#include <assert.h>
#ifdef _WIN32
#include <Windows.h>
#endif //_WIN32
using std::cout;
using std::endl;
//管理对象自由链表长度
const size_t NLISTS = 240;
const size_t MAXSIZE = 64 * 1024;
const size_t PAGE_SHIFT = 12;
const size_t NPAGES = 129;
typedef size_t PageID;
static inline void* SystemAlloc()
{
#ifdef _WIN32
void* ptr = VirtualAlloc(NULL, (NPAGES - 1) << PAGE_SHIFT, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
#else
// brk mmap等
#endif
if (ptr == nullptr)
{
throw std::bad_alloc();
}
return ptr;
}
static inline void SystemFree(void* ptr)
{
#ifdef _WIN32
VirtualFree(ptr,0, MEM_RELEASE);
#else
// sbrk unmmap等
#endif // _WIN32
}
static inline void*& NEXT_OBJ(void* obj)
{
return (*(void**)obj);
}
struct Span
{
PageID _pageid = 0; //页号
size_t _npage = 0; //页的数量
Span* _next = nullptr;
Span* _prev = nullptr;
void* _objlist = nullptr; //自由链表
size_t _objsize = 0; //对象大小
size_t _usecount = 0; //使用计数
};
//带头双向循环链表
class SpanList
{
public:
SpanList()
{
_head = new Span;
_head->_next = _head;
_head->_prev = _head;
}
//模式迭代器
Span* begin()
{
return _head->_next;
}
Span* end()
{
return _head;
}
void Insert(Span* cur, Span* newSpan)
{
assert(cur);
Span* prev = cur->_prev;
//prev newSpan cur
newSpan->_next = cur;
cur->_prev = newSpan;
prev->_next = newSpan;
newSpan->_prev = prev;
}
void Erease(Span* cur)
{
assert(cur!=nullptr && cur != _head);
Span* prev = cur->_prev;
Span* next = cur->_next;
prev->_next = next;
next->_prev = prev;
//delete cur;
}
void PushFront(Span* span)
{
Insert(begin(), span);
}
Span* PopFront()
{
Span* span = begin();
Erease(span);
return span;
}
void PushBack(Span* span)
{
Insert(end(), span);
}
Span* PopBack()
{
Span* tail = _head->_prev;
Erease(tail);
return tail;
}
bool Empty()
{
return _head->_next == _head;
}
public:
std::mutex _mtx;
private:
Span* _head = nullptr;
};
class FreeList
{
public:
bool empty()
{
return _list == nullptr;
}
void PushRange(void*start, void* end, size_t num)
{
NEXT_OBJ(end)=_list;
_list = start;
_size += num;
}
//回收内存
void* Clear()
{
_size = 0;
void* list = _list;
_list = nullptr;
return list;
}
//头删
void* pop()
{
void* obj = _list;
_list = NEXT_OBJ(obj);
--_size;
return obj;
}
//头插
void push(void* obj)
{
NEXT_OBJ(obj) = _list;
_list = obj;
++_size;
}
size_t Size()
{
return _size;
}
void SetMaxSize(size_t maxsize)
{
_maxsize = maxsize;
}
size_t MaxSize()
{
return _maxsize;
}
private:
void* _list = nullptr;
size_t _size = 0; //记录有多少个对象
size_t _maxsize = 1; //一个自由链表后面最多可以挂多少个list
};
//管理内存对齐
class SizeClass
{
// 控制在12%左右的内碎片浪费
// [1,128] 8byte对齐 freelist[0,16)
// [129,1024] 16byte对齐 freelist[16,72)
// [1025,8*1024] 128byte对齐 freelist[72,128)
// [8*1024+1,64*1024] 512byte对齐 freelist[128,240)
public:
//对齐规则 align:对准 比如8字节对齐:用12字节 (12+7)&~7
static inline size_t _RoundUp(size_t bytes, size_t align)
{
return (((bytes)+align-1)&~(align-1));
}
//对齐大小计算 向上取整(上面给出的对齐规则)
static inline size_t RoundUp(size_t bytes)
{
assert(bytes <= MAXSIZE);
if (bytes <= 128) {
return _RoundUp(bytes, 8);
}
else if (bytes <= 1024) {
return _RoundUp(bytes, 16);
}
else if (bytes <= 8192) {
return _RoundUp(bytes, 128);
}
else if (bytes <= 65536) {
return _RoundUp(bytes, 512);
}
return -1;
}
//映射自由链表位置
static inline size_t _Index(size_t bytes, size_t align_shift)
{
return ((bytes + (1 << align_shift) - 1) >> align_shift) - 1;
}
static inline size_t Index(size_t bytes)
{
assert(bytes <= MAXSIZE);
//每个区间有多少个链
static int group_array[] = { 16,56,56,112 };
if (bytes <= 128) {
return _Index(bytes, 3);
}
else if (bytes <= 1024) {
return _Index((bytes - 128), 4) + group_array[0];
}
else if (bytes <= 8192) {
return _Index((bytes - 1024), 7) + group_array[0] + group_array[1];
}
else if (bytes <= 65536) {
return _Index((bytes - 8192), 9) + group_array[0] + group_array[1] + group_array[2];
}
return -1;
}
static size_t NumMoveSize(size_t size)
{
if (size == 0)
return 0;
int num = static_cast<int>(MAXSIZE / size);
if (num < 2)
num = 2;
if (num > 512)
num = 512;
return num;
}
//计算一次向系统获取多少个页
static size_t NumMovePage(size_t size)
{
size_t num = NumMoveSize(size);
size_t npage = num * size;
npage >>= 12; //算出几个页 >>=12 == /4096 一页 = 4k
if (npage == 0)
npage = 1;
return npage;
}
};