forked from MouCoder/cpp_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryManagement.h
More file actions
64 lines (59 loc) · 1.33 KB
/
MemoryManagement.h
File metadata and controls
64 lines (59 loc) · 1.33 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
//内存管理:直接向用户提供申请内存和释放内存的接口
#pragma once
#include"CommonResource.h"
#include"ThreadCache.h"
#include"PageCache.h"
//high concurrent memory pool
static void* hcMalloc(size_t memSize)
{
try
{
if (memSize > THREAD_MAX_SIZE)
{
//向page cache申请内存
size_t npage = SizeCalculation::RoundUp(memSize) >> PAGE_SHIFT;
Span* span = PageCache::GetInstance()->NewSpan(npage);
span->_objsize = memSize;
void* ptr = (void*)(span->_pageId << PAGE_SHIFT);
return ptr;
}
else
{
//创建tls,每个线程独有thread cache,向thread cache申请
if (tls_threadcache == nullptr)
{
tls_threadcache = new ThreadCache;
}
return tls_threadcache->Allocate(memSize);
}
}
catch (const std::exception& e)
{
cout << e.what() << std::endl;
}
return nullptr;
}
static void hcFree(void* mem)
{
try{
//根据内存大小在map中查找对应的span
Span* sp = PageCache::GetInstance()->GetSpanToMap(mem);
//获取mem的大小
size_t memSize = sp->_objsize;
if (memSize > THREAD_MAX_SIZE)
{
//还给pagecache
PageCache::GetInstance()->ReleaseSpanToPageCache(sp);
}
else
{
//还给threadcache
assert(tls_threadcache);
tls_threadcache->DeAllocate(mem, memSize);
}
}
catch (const std::exception& e)
{
cout << e.what() << std::endl;
}
}