-
Notifications
You must be signed in to change notification settings - Fork 1
/
Common.h
317 lines (267 loc) · 5.68 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#pragma once
#include <iostream>
#include <assert.h>
#include <thread>
#include <vector>
#include <mutex>
#include <unordered_map>
using std::cout;
using std::endl;
#ifdef _WIN32
#include <windows.h>
#endif // _WIN32
const size_t NLISTS = 184; //管理对象自由链表的长度,由对齐规则决定,即array决定
const size_t MAXBYTES = 64 * 1024; //自定义规定小于64k,在threadcache申请,即threadcache最大64k;
const size_t PAGE_SHIFT = 12; //移位数
const size_t NPAGES = 129; //pagecache链表最大长度
// pagecache内存不足,需要向系统申请内存(windows用VirtualAlloc,Linux用brk/mmap)
inline static void* SystemAlloc(size_t npage)
{
#ifdef _WIN32
//1,指定位置:0(任意)2,字节大小 3,分配类型 4,属性(可读可写)
void* ptr = VirtualAlloc(NULL, (NPAGES - 1) << PAGE_SHIFT, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (ptr == nullptr)
{
throw std::bad_alloc();
}
#else
//brk mmap
#endif
return ptr;
}
inline static void SystemFree(void* ptr)
{
#ifdef _WIN32
//1,指针 ,2,大小,3,MEM_RELEASE
VirtualFree(ptr,0,MEM_RELEASE);
#else
//brk mumap
#endif
}
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);
}
Span* PopFront()
{
Span* span = begin();
Erase(span);
return span;
}
void PushBack(Span* span)
{
Insert(end(),span);
}
Span* PopBack()
{
Span* tail = _head->_prev;
Erase(tail);
return tail;
}
public:
//本来是申请内存就加锁,但是不同的线程申请不同大小内存就会严重影响效率,所以可以在每个spanlist上加锁
std::mutex _mutex;
private:
Span* _head = nullptr;
};
//当前对象的下一个对象地址
static inline void*& NEXT_OBJ(void* obj)
{
return *((void**)obj);
}
//自由链表
class FreeList
{
public:
bool Empty()
{
return _list == nullptr;
}
//头删
void* Pop()
{
void* obj = _list;
_list = NEXT_OBJ(obj);
--_size;
return obj;
}
//头插
void Push(void* obj)
{
NEXT_OBJ(obj) = _list;
_list = obj;
++_size;
}
//将多个结点一次性插入
void PushRange(void* start, void* end, size_t num)
{
NEXT_OBJ(end) = _list;
_list = start;
_size += num;
}
void* Clear()
{
_size = 0;//maxsize不清零,便于下次大批量申请
void* list = _list;
_list = nullptr;
return list;
}
//获取结点个数
size_t Size()
{
return _size;
}
//获取结点个数最大值
size_t MaxSize()
{
return _maxsize;
}
void SetMaxSize(size_t max)
{
_maxsize = max;
}
private:
void* _list = nullptr; //c++11赋值默认初始化
size_t _size = 0; //链表结点个数
size_t _maxsize = 1; //一次从中心Cache获取链表对象个数
};
//
// 管理对齐映射的规则
class ClassSize
{
// 控制在12%左右的内碎片浪费 ,划分越小,内碎片越少,浪费越清
// [1,128] 8byte对齐 freelist[0,16) //8太小,
// [129,1024] 16byte对齐 freelist[16,72) //15/129=12%
// [1025,8*1024] 128byte对齐 freelist[72,128)
// [8*1024+1,64*1024] 1024byte对齐 freelist[128,184)
public:
//字节对齐到某对齐数 eg:对齐到8 (size+7)& ~(7):size+对齐数-1,就是跃迁到下一个区域,
//eg:按8对齐:7对齐到8,10对齐到16等
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, 1024);
}
else {
return -1;
}
}
static inline size_t _Index(size_t bytes, size_t align_shift)
{
//eg:按8对齐:字节数+8-1,,跃迁到下一个区域,除以8(>>3),看有多少个8,最后减一就是下标
//return (bytes+对齐数-1)/对齐数-1;此处用移位运算解决
return ((bytes + (1 << align_shift) - 1) >> align_shift) - 1;
}
static inline size_t Index(size_t bytes)
{
assert(bytes <= MAXBYTES);
// 每个区间有多少个自由链表
static int group_array[4] = { 16, 56, 56, 56 };
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] + group_array[0];
}
else if (bytes <= 65536){
return _Index(bytes - 8192, 10) + group_array[2] + group_array[1] +
group_array[0];
}
else{
return -1;
}
}
static size_t BytesMoveNum(size_t size)
{
if (size == 0)
return 0;
//获取对象个数:大对象少申请点,小对象多申请。为什么MAXBYTES/byte?:首先是自定义的,也因为链表对象最大64k,
int num = static_cast<int>(MAXBYTES / size); //
if (num < 2)
num = 2;
if (num > 512)
num = 512;
return (size_t)num;
}
// 计算一次向系统获取几个页
static size_t BytesMovePage(size_t bytes)
{
//控制对象个数上限下限
size_t num = BytesMoveNum(bytes);
size_t count_bytes= num*bytes;
//(总的字节数/4k)就是页数,也就是右移12位
size_t npage = count_bytes >> 12;
if (npage == 0)
npage = 1;
return npage;
}
};