-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCentralCache.h
More file actions
91 lines (89 loc) · 2.46 KB
/
CentralCache.h
File metadata and controls
91 lines (89 loc) · 2.46 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
//
// Created by ASUS on 2026/3/23.
//
#ifndef MEMORY_POOL_IMPROVEMENT_CENTRALCACHE_H
#define MEMORY_POOL_IMPROVEMENT_CENTRALCACHE_H
#include <cassert>
#include<mutex>
#include "ThreadCache.h"
typedef size_t PageID;
struct Span {
PageID _pageId=0;//页号
size_t _n=0;//页面数量
Span* _next=nullptr;
Span* _prev=nullptr;
bool _isUse=false;//判断当前span是在cc中还是在pc中
void* _freelist=nullptr;//大块内存切小链链接起来
size_t _usecount=0;
};
class SpanList {
public:
std::mutex _mtx;
void PushFront(Span* span) {
Insert(Begin(),span);
}
SpanList() {
_head=new Span();
//双向循环,所以都指向_head
_head->_next=_head;
_head->_prev=_head;
}
void Insert(Span* pos,Span*ptr) {
assert(pos);
assert(ptr);
Span* prev=pos->_prev;
prev->_next=ptr;
ptr->_prev=prev;
ptr->_next=pos;
pos->_prev=ptr;
}
void Erase(Span* pos) {
assert( pos);
assert(pos!=_head);
Span* prev=pos->_prev;
Span* next=pos->_next;
prev->_next=next;
next->_prev=prev;
}
Span* Begin() {
return _head->_next;
}
Span* End() {
return _head;
}
bool Empty() {
return _head->_next==_head;
}
Span* PopFront() {
//先获取到_head后面的第一个span
Span* front=_head->_next;
Erase(front);
//返回原来的第一个span
return front;
}
private:
Span* _head=nullptr;
};
class CentralCache {
private:
SpanList _spanLists[FREE_LIST_SIZE];//哈希桶中挂的是一个一个的Span
static CentralCache _sInt;//饿汉模式创建一个CentralCache
CentralCache(){}
CentralCache(CentralCache©)=delete;
CentralCache& operator=(const CentralCache©)=delete;
public:
//将tc还回来的多块空间放入span中
void ReleaseListToSpans(void*start,size_t size);
//单例接口
static CentralCache* GetInstance() {
return &_sInt;
}
size_t FetchRangeObj(void*& start,void*& end,size_t size,size_t batchNum);
/*start和end表示cc提供的空间的开始结尾,输出型参数
* batchNum表示tc需要多少块size大小的空间
* size表示tc需要的单空间的大小
* 返回值是cc实际提供的空间大小
*/
Span* GetOneSpan(SpanList& list,size_t size);
};
#endif //MEMORY_POOL_IMPROVEMENT_CENTRALCACHE_H