forked from caozhiyi/foundation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MemaryPool.cpp
126 lines (102 loc) · 2.67 KB
/
MemaryPool.cpp
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
#include "MemaryPool.h"
#include <assert.h>
CMemaryPool::CMemaryPool() {
memset(_free_list, 0, sizeof(_free_list[__number_of_free_lists]));
_create_thread_id = std::this_thread::get_id();
}
CMemaryPool::CMemaryPool(const int large_sz, const int add_num) : _large_size(RoundUp(large_sz)), _number_large_add_nodes(add_num){
memset(_free_list, 0, sizeof(_free_list[__number_of_free_lists]));
_create_thread_id = std::this_thread::get_id();
}
CMemaryPool::~CMemaryPool() {
assert(_create_thread_id == std::this_thread::get_id());
for (auto iter = _malloc_vec.begin(); iter != _malloc_vec.end(); ++iter) {
if (*iter) {
free(*iter);
}
}
}
std::thread::id CMemaryPool::GetCreateThreadId() {
return _create_thread_id;
}
int CMemaryPool::GetLargeSize() const {
return _large_size;
}
void* CMemaryPool::ReFill(int size, int num, bool is_large) {
int nums = num;
char* chunk = (char*)ChunkAlloc(size, nums);
MemNode** my_free;
MemNode* res, *current, *next;
if (1 == nums) {
return chunk;
}
res = (MemNode*)chunk;
if (is_large) {
if (_free_large.find(size) == _free_large.end()) {
_free_large[size] = nullptr;
}
my_free = &_free_large[size];
*my_free = next = (MemNode*)(chunk + size);
for (int i = 1;; i++) {
current = next;
next = (MemNode*)((char*)next + size);
if (nums - 1 == i) {
current->_next = nullptr;
break;
}
else {
current->_next = next;
}
}
} else {
my_free = &(_free_list[FreeListIndex(size)]);
*my_free = next = (MemNode*)(chunk + size);
for (int i = 1;; i++) {
current = next;
next = (MemNode*)((char*)next + size);
if (nums - 1 == i) {
current->_next = nullptr;
break;
}
else {
current->_next = next;
}
}
}
return res;
}
void* CMemaryPool::ChunkAlloc(int size, int& nums, bool is_large) {
char* res;
int need_bytes = size * nums;
int left_bytes = _pool_end - _pool_start;
//内存池够用
if (left_bytes >= need_bytes) {
res = _pool_start;
_pool_start += need_bytes;
return res;
} else if (left_bytes >= size) {
nums = left_bytes / size;
need_bytes = size * nums;
res = _pool_start;
_pool_start += need_bytes;
return res;
}
int bytes_to_get = size * nums;
if (!is_large) {
if (left_bytes > 0) {
MemNode* my_free = _free_list[FreeListIndex(left_bytes)];
((MemNode*)_pool_start)->_next = my_free;
_free_list[FreeListIndex(size)] = (MemNode*)_pool_start;
}
} else {
free(_pool_start);
}
_pool_start = (char*)malloc(bytes_to_get);
//内存分配失败
if (0 == _pool_start) {
throw std::exception("There memary is not enough!");
}
_malloc_vec.push_back(_pool_start);
_pool_end = _pool_start + bytes_to_get;
return ChunkAlloc(size, nums, is_large);
}