-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathreference_counting.h
More file actions
200 lines (160 loc) · 3.55 KB
/
reference_counting.h
File metadata and controls
200 lines (160 loc) · 3.55 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*******************************************************************\
Module: Reference Counting
Author: Daniel Kroening, kroening@kroening.com
\*******************************************************************/
/// \file
/// Reference Counting
#ifndef CPROVER_UTIL_REFERENCE_COUNTING_H
#define CPROVER_UTIL_REFERENCE_COUNTING_H
#include "invariant.h"
/// \tparam empty: pointer to empty data, if unspecified use a reference to
/// T::blank
template <typename T, const T &empty = T::blank>
// NOLINTNEXTLINE(readability/identifiers)
class reference_counting
{
public:
reference_counting():d(nullptr)
{
}
explicit reference_counting(const T &other):d(NULL)
{
write()=other;
}
// copy constructor
reference_counting(const reference_counting &other):d(other.d)
{
if(d!=nullptr)
{
PRECONDITION(d->ref_count != 0);
d->ref_count++;
#ifdef REFERENCE_COUNTING_DEBUG
std::cout << "COPY " << d << " " << d->ref_count << '\n';
#endif
}
}
reference_counting &operator=(const reference_counting &other)
{
copy_from(other);
return *this;
}
~reference_counting()
{
remove_ref(d);
d=nullptr;
}
void swap(reference_counting &other)
{
std::swap(other.d, d);
}
void clear()
{
remove_ref(d);
d=nullptr;
}
const T &read() const
{
if(d==nullptr)
return empty;
return *d;
}
T &write()
{
detach();
return *d;
}
protected:
class dt:public T
{
public:
unsigned ref_count;
dt():ref_count(1)
{
}
};
dt *d;
void remove_ref(dt *old_d);
void detach();
void copy_from(const reference_counting &other)
{
PRECONDITION(&other != this); // check if we assign to ourselves
#ifdef REFERENCE_COUNTING_DEBUG
std::cout << "COPY " << (&other) << "\n";
#endif
remove_ref(d);
d=other.d;
if(d!=nullptr)
d->ref_count++;
}
public:
dt *get_d() const
{
return d;
}
};
template <class T, const T &empty>
void reference_counting<T, empty>::remove_ref(dt *old_d)
{
if(old_d==nullptr)
return;
PRECONDITION(old_d->ref_count != 0);
#ifdef REFERENCE_COUNTING_DEBUG
std::cout << "R: " << old_d << " " << old_d->ref_count << '\n';
#endif
old_d->ref_count--;
if(old_d->ref_count==0)
{
#ifdef REFERENCE_COUNTING_DEBUG
std::cout << "DELETING " << old_d << '\n';
old_d->clear();
std::cout << "DEALLOCATING " << old_d << "\n";
#endif
delete old_d;
#ifdef REFERENCE_COUNTING_DEBUG
std::cout << "DONE\n";
#endif
}
}
template <class T, const T &empty>
void reference_counting<T, empty>::detach()
{
#ifdef REFERENCE_COUNTING_DEBUG
std::cout << "DETACH1: " << d << '\n';
#endif
if(d==nullptr)
{
d=new dt;
#ifdef REFERENCE_COUNTING_DEBUG
std::cout << "ALLOCATED " << d << '\n';
#endif
}
else if(d->ref_count>1)
{
dt *old_d(d);
d=new dt(*old_d);
#ifdef REFERENCE_COUNTING_DEBUG
std::cout << "ALLOCATED " << d << '\n';
#endif
d->ref_count=1;
remove_ref(old_d);
}
POSTCONDITION(d->ref_count == 1);
#ifdef REFERENCE_COUNTING_DEBUG
std::cout << "DETACH2: " << d << '\n'
#endif
}
template <class T, const T &empty>
bool operator==(
const reference_counting<T, empty> &o1,
const reference_counting<T, empty> &o2)
{
if(o1.get_d()==o2.get_d())
return true;
return o1.read()==o2.read();
}
template <class T, const T &empty>
inline bool operator!=(
const reference_counting<T, empty> &i1,
const reference_counting<T, empty> &i2)
{ return !(i1==i2); }
#endif // CPROVER_UTIL_REFERENCE_COUNTING_H