-
Notifications
You must be signed in to change notification settings - Fork 1
/
Common.h
289 lines (247 loc) · 4.36 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
283
284
285
286
287
288
289
#pragma once
#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <unordered_map>
#include <map>
#ifdef _WIN32
#include <windows.h>
#endif // _WIN32
using std::cout;
using std::endl;
#include <assert.h>
// 管理对象自由链表的长度
const size_t NLISTS = 240; //?
const size_t MAXBYTES = 64 * 1024;
const size_t PAGE_SHIFT = 12;
const size_t NPAGES = 129;
static inline void*& NEXT_OBJ(void* obj)
{
return *((void**)obj);
}
static inline void* SystemAlloc(size_t npage)
{
#ifdef _WIN32
void* ptr = VirtualAlloc(NULL, (npage) << PAGE_SHIFT, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (ptr == nullptr)
{
throw std::bad_alloc();
}
#else
//brk,mmap
#endif
return ptr;
}
static inline void SystemFree(void* ptr)
{
#ifdef _WIN32
VirtualFree(ptr, 0, MEM_RELEASE);
#else
//brk,mmap
#endif
return;
}
typedef size_t PageID;
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;
}
bool Empty()
{
return _head->_next == _head;
}
void Insert(Span* cur, Span* newspan)
{
assert(cur);
Span* prev = cur->_prev;
// prev newspan cur
prev->_next = newspan;
newspan->_prev = prev;
newspan->_next = cur;
cur->_prev = newspan;
}
void Erase(Span* cur)
{
assert(cur != nullptr && cur != _head);
Span* prev = cur->_prev;
Span* next = cur->_next;
prev->_next = next;
next->_prev = prev;
}
void PushFront(Span* span)
{
Insert(begin(), span);
}
void PushBack(Span* span)
{
Insert(end(), span);
}
Span* PopFront()
{
Span* span = begin();
Erase(span);
return span;
}
Span* PopBack()
{
Span* span = _head->_prev;
Erase(span);
return span;
}
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 = 10;
};
// 管理对齐映射
class ClassSize
{
// 控制在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:
static inline size_t _Roundup(size_t size, size_t align)
{
return (size + (align) - 1) & ~((align) - 1);
}
static inline size_t Roundup(size_t size)
{
assert(size <= MAXBYTES);
if (size <= 128) {
return _Roundup(size, 8);
}
else if (size <= 1024){
return _Roundup(size, 16);
}
else if (size <= 8192){
return _Roundup(size, 128);
}
else if (size <= 65536){
return _Roundup(size, 512);
}
else {
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 < MAXBYTES);
// 每个区间有多少个自由链表
static int group_array[3] = { 16, 72, 128 };
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[1];
}
else if (bytes <= 65536){
return _Index(bytes - 8192, 9) + group_array[2];
}
else{
return -1;
}
}
static size_t NumMoveSize(size_t size)
{
if (size <= 0)
return 0;
int num = (int)(MAXBYTES / 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;
if (!npage)
npage = 1;
return npage;
}
};