-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmy_allocator.h
More file actions
347 lines (292 loc) · 7.69 KB
/
my_allocator.h
File metadata and controls
347 lines (292 loc) · 7.69 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#pragma once
#include <stdarg.h>
#include <string>
#define __DEBUG__
static string GetFileName(const string& path)
{
char ch = '/';
#ifdef _WIN32
ch = '\\';
#endif
size_t pos = path.rfind(ch);
if (pos == string::npos)
return path;
else
return path.substr(pos + 1);
}
//用于调试追溯的trace log
inline static void __trace_debug(const char* function,
const char* filename, int line, char* format, ...)
{
#ifdef __DEBUG__
//输出调用函数的信息
fprintf(stdout, "【%s:%d】%s", GetFileName(filename).c_str(), line, function);
//输出用户打的trace信息
va_list args;
va_start(args, format);
vfprintf(stdout, format, args);
va_end(args);
#endif
}
#define __TRACE_DEBUG(...) \
__trace_debug(__FUNCTION__,__FILE__,__LINE__,__VA_ARGS__);
typedef void(*HandlerFunc)();
// 一级空间配置器
template <int inst>
class __MallocAllocTemplate
{
private:
static void* oomMalloc(size_t size)
{
while (1)
{
if (__malloc_alloc_oom_handler == NULL)
{
throw bad_alloc();
}
else
{
(*__malloc_alloc_oom_handler)();
void* ret = malloc(size);
if (ret != NULL)
return ret;
}
}
}
private:
static HandlerFunc __malloc_alloc_oom_handler;
public:
static void* Allocate(size_t n)
{
__TRACE_DEBUG("一级空间配置器分配内存:%d\n", n);
void* result = malloc(n);
if (NULL == result)
result = oomMalloc(n);
return result;
}
static void Deallocate(void* p, size_t /*n*/)
{
free(p);
}
//static void (* set_malloc_handler(void (*f)()))()
static HandlerFunc SetMallocHandler(HandlerFunc f)
{
void(*old)() = __malloc_alloc_oom_handler;
__malloc_alloc_oom_handler = f;
return old;
}
};
template <int inst>
void(*__MallocAllocTemplate<inst>::__malloc_alloc_oom_handler)() = 0;
// 二级空间配置器
template <bool threads, int inst>
class __DefaultAllocTemplate
{
private:
enum {__ALIGN = 8};
enum {__MAX_BYTES = 128};
enum {__NFREELISTS = __MAX_BYTES / __ALIGN };
union Obj
{
union Obj* _freeListLink;
char client_data[1]; /* The client sees this. */
};
static Obj* _freeList[__NFREELISTS];
// Chunk allocation state
static char* _startFree; // 内存池的开始
static char* _endFree; // 内存池的结束
static size_t _heapSize; // 已分配内存的大小
static size_t FREELIST_INDEX(size_t bytes)
{
return ((bytes + __ALIGN - 1) / __ALIGN - 1);
}
static size_t ROUND_UP(size_t bytes)
{
return ((bytes + __ALIGN - 1) & ~(__ALIGN - 1));
}
public:
static char* ChunkAlloc(size_t n, size_t& nobjs)
{
char* ret = NULL;
size_t totalBytes = n * nobjs;
size_t bytesLeft = _endFree - _startFree;
// 内存池有足够nobjs个对象的内存
if (bytesLeft >= totalBytes)
{
__TRACE_DEBUG("内存池有足够nobjs:%d个对象的内存\n", nobjs);
ret = _startFree;
_startFree += totalBytes;
return ret;
}
// 内存池只够分配n个对象的内存,不足nobjs
else if (bytesLeft >= n)
{
nobjs = bytesLeft / n;
__TRACE_DEBUG("只有nobjs:%d个对象的内存\n", nobjs);
totalBytes = nobjs * n;
ret = _startFree;
_startFree += totalBytes;
return ret;
}
// 内存池连一个对象的内存都分配不出来
else
{
// 计算向系统申请的字节数
size_t bytesToGet = totalBytes * 2 + ROUND_UP(_heapSize >> 4);
__TRACE_DEBUG("找系统分配%d字节的内存\n", bytesToGet);
// 处理内存池剩余的字节数
if (bytesLeft > 0)
{
size_t index = FREELIST_INDEX(bytesLeft);
((Obj*)_startFree)->_freeListLink = _freeList[index];
_freeList[index] = (Obj*)_startFree;
}
_startFree = (char*)malloc(bytesToGet);
// 分配失败
if (_startFree == NULL)
{
// 在更大的自由链表中去取
for (size_t i = n; i < __MAX_BYTES; i += __ALIGN)
{
size_t index = FREELIST_INDEX(i);
if (_freeList[index] != NULL)
{
_startFree = (char*)_freeList[index];
_freeList[index] = ((Obj*)_startFree)->_freeListLink;
_endFree = _startFree + i;
return ChunkAlloc(n, nobjs);
}
}
// 山穷水尽-->调用一级空间配置器
_endFree = NULL; // 此处有可能抛出异常,外界可能捕获异常,保证_startFree和_endFree相等
_startFree = (char*)__MallocAllocTemplate<0>::Allocate(bytesToGet);
}
// malloc或一级空间配置器分配成功
_endFree = _startFree + bytesToGet;
_heapSize = bytesToGet;
return ChunkAlloc(n, nobjs);
}
}
static void* Refill(size_t n)
{
// 分配nobjs个对象的大小
size_t nobjs = 20;
char* chunk = ChunkAlloc(n, nobjs);
if (n == 1)
return chunk;
__TRACE_DEBUG("挂内存块到自由链表nobjs:%d\n", nobjs);
// 挂内存块
Obj* cur, *next;
size_t index = FREELIST_INDEX(n);
cur = (Obj*)(chunk + n);
_freeList[index] = cur;
for (size_t i = 1; i < nobjs - 1; i++)
{
next = (Obj*)((char*)cur + n);
cur->_freeListLink = next;
cur = next;
}
cur->_freeListLink = NULL;
return chunk;
}
static void* Allocate(size_t n)
{
__TRACE_DEBUG("二级空间配置器分配内存:%d\n", n);
// 大于128调用一级空间配置器申请
if (n > __MAX_BYTES)
{
return __MallocAllocTemplate<inst>::Allocate(n);
}
size_t index = FREELIST_INDEX(n);
// 自由链表对应位置没有-->填充
if (_freeList[index] == NULL)
{
return Refill(ROUND_UP(n));
}
// 有-->头删
else
{
__TRACE_DEBUG("到自由链表中去取内存块:%d\n", index);
Obj* first = _freeList[index];
_freeList[index] = first->_freeListLink;
return first;
}
}
static void Deallocate(void* p, size_t n)
{
// 大于128调用一级空间配置器释放
if (n > __MAX_BYTES)
{
__MallocAllocTemplate<0>::Deallocate(p, n);
}
// 找到内存块在自由链表中的位置-->头插
else
{
size_t index = FREELIST_INDEX(n);
Obj* obj = (Obj*)p;
obj->_freeListLink = _freeList[index];
_freeList[index] = obj;
}
}
};
template <bool threads, int inst>
typename __DefaultAllocTemplate<threads, inst>::Obj*
__DefaultAllocTemplate<threads, inst>::_freeList[__NFREELISTS] = {0};
// Chunk allocation state.
template <bool threads, int inst>
char* __DefaultAllocTemplate<threads, inst>::_startFree = NULL;
template <bool threads, int inst>
char* __DefaultAllocTemplate<threads, inst>::_endFree = NULL;
template <bool threads, int inst>
size_t __DefaultAllocTemplate<threads, inst>::_heapSize = 0;
// 封装
template <typename T, class Alloc>
class SimpleAlloc
{
public:
static T* Allocate(size_t n)
{
return 0 == n ? 0 : (T*)Alloc::Allocate(n * sizeof(T));
}
static T* Allocate()
{
return (T*)Alloc::Allocate(sizeof(T));
}
static void Deallocate(T* p, size_t n)
{
if (0 != n)
Alloc::Deallocate(p, n * sizeof(T));
}
static void Deallocate(T *p)
{
Alloc::Deallocate(p, sizeof(T));
}
};
#ifdef __USE_MALLOC
typedef __MallocAllocTemplate<0> Alloc;
#else
typedef __DefaultAllocTemplate<false, 0> Alloc;
#endif // __USE_MALLOC
// 测试内存池的一级、二级配置器功能
void TestAllocator()
{
// 测试调用一级配置器分配内存
cout << " 测试调用一级配置器分配内存 " << endl;
char*p1 = SimpleAlloc< char, Alloc>::Allocate(129);
SimpleAlloc<char, Alloc>::Deallocate(p1, 129);
// 测试调用二级配置器分配内存
cout << " 测试调用二级配置器分配内存 " << endl;
char*p2 = SimpleAlloc< char, Alloc>::Allocate(128);
char*p3 = SimpleAlloc< char, Alloc>::Allocate(128);
char*p4 = SimpleAlloc< char, Alloc>::Allocate(128);
char*p5 = SimpleAlloc< char, Alloc>::Allocate(128);
SimpleAlloc<char, Alloc>::Deallocate(p2, 128);
SimpleAlloc<char, Alloc>::Deallocate(p3, 128);
SimpleAlloc<char, Alloc>::Deallocate(p4, 128);
SimpleAlloc<char, Alloc>::Deallocate(p5, 128);
for (int i = 0; i < 21; ++i)
{
printf(" 测试第%d次分配 \n", i + 1);
char*p = SimpleAlloc< char, Alloc>::Allocate(128);
}
}