-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConcurrentAlloc.h
59 lines (53 loc) · 1.01 KB
/
ConcurrentAlloc.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
#pragma once
#include "Common.h"
#include "ThreadCache.h"
#include "PageCache.h"
//void* tcmalloc(size_t size)
static void* ConcurrentAlloc(size_t size)
{
try
{
if (size > MAX_BYTES)
{
size_t npage = SizeClass::RoundUp(size) >> PAGE_SHIFT;
Span* span = PageCache::GetInstance()->NewSpan(npage);
span->_objsize = size;
void* ptr = (void*)(span->_pageId << PAGE_SHIFT);
return ptr;
}
else
{
if (tls_threadcache == nullptr)
{
tls_threadcache = new ThreadCache;
}
return tls_threadcache->Allocate(size);
}
}
catch (const std::exception& e)
{
cout << e.what() << endl;
}
return nullptr;
}
static void ConcurrentFree(void* ptr)
{
try
{
Span* span = PageCache::GetInstance()->MapObjectToSpan(ptr);
size_t size = span->_objsize;
if (size > MAX_BYTES)
{
PageCache::GetInstance()->ReleaseSpanToPageCache(span);
}
else
{
assert(tls_threadcache);
tls_threadcache->Deallocate(ptr, size);
}
}
catch (const std::exception& e)
{
cout << e.what() << endl;
}
}