-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryReclamationByReference.h
67 lines (66 loc) · 1.87 KB
/
MemoryReclamationByReference.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
60
61
62
63
64
65
66
67
/*************************************************************************
@Author: jiapeng.wen([email protected])
@Created Time : Wed 11 Sep 2019 02:57:00 PM CST
@File Name: MemoryReclamationByReference.h
@brief:
************************************************************************/
#pragma once
#include <atomic>
#include <cassert>
template<typename _Node>
class MemoryReclamationByReferenceCountingT
{
std::atomic<size_t> counter_ = ATOMIC_VAR_INIT(0);
std::atomic<_Node*> will_be_deleted_list_ = ATOMIC_VAR_INIT(nullptr);
void InsertToList(_Node* first, _Node* last)
{
last->next = will_be_deleted_list_;
while (!will_be_deleted_list_.compare_exchange_strong(last->next, first));
}
void InsertToList(_Node* head)
{
_Node* last = head;
while (_Node* next = last->next) last = next;
InsertToList(head, last);
}
public:
~MemoryReclamationByReferenceCountingT()
{
// 如果线程正常退出,Reference Counting算法能删除所有数据
assert(will_be_deleted_list_.load() == nullptr);
assert(counter_.load() == 0);
_Node* to_delete_list = will_be_deleted_list_.exchange(nullptr);
while (to_delete_list) {
_Node* node = to_delete_list;
to_delete_list = node->next;
delete node;
}
}
inline void Addref()
{
++counter_;
}
inline bool Store(_Node* node) { return true; }
bool Release(_Node* node)
{
if (!node) return false;
if (counter_ == 1)
{
_Node* to_delete_list = will_be_deleted_list_.exchange(nullptr);
if (!--counter_) {
while (to_delete_list) {
_Node* node = to_delete_list;
to_delete_list = node->next;
delete node;
}
} else if (to_delete_list) {
InsertToList(to_delete_list);
}
delete node;
} else {
if (node) InsertToList(node, node);
--counter_;
}
return true;
}
};