forked from shuaimu/refcell-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
borrow.h
241 lines (215 loc) · 5.07 KB
/
borrow.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <cstdint>
#include <memory>
#include <utility>
#include <atomic>
#include <cassert>
#include <cstdlib>
#include <csignal>
#include <execinfo.h>
#include <iostream>
namespace borrow {
// Macros for custom error handling
#define PRINT_STACK_TRACE() \
do { \
void* buffer[30]; \
int size = backtrace(buffer, 30); \
char** symbols = backtrace_symbols(buffer, size); \
if (symbols == nullptr) { \
std::cerr << "Failed to obtain stack trace." << std::endl; \
break; \
} \
std::cerr << "Stack trace:" << std::endl; \
for (int i = 0; i < size; ++i) { \
std::cerr << symbols[i] << std::endl; \
} \
free(symbols); \
} while(0)
#ifndef borrow_verify
#ifdef BORROW_INFER_CHECK
#define borrow_verify(x) {if (!(x)) {volatile int* a = nullptr ; *a;}}
//#define borrow_verify(x) nullptr
#else
#define borrow_verify(x, errmsg) \
do { \
if (!(x)) { \
fprintf(stderr, errmsg); \
PRINT_STACK_TRACE(); \
std::abort(); \
} \
} while(0)
#endif
#endif
template<class T>
class Ref {
public:
const T* raw_{nullptr};
std::atomic<int32_t>* p_cnt_{nullptr};
Ref() = default;
Ref(const Ref&) = delete;
Ref(Ref&& p) {
raw_ = p.raw_;
p_cnt_ = p.p_cnt_;
p.raw_ = nullptr;
p.p_cnt_ = nullptr;
};
Ref(Ref& p) {
raw_ = p.raw_;
p_cnt_ = p.p_cnt_;
auto i = (*p_cnt_)++;
borrow_verify(i > 0, "error in Ref constructor");
}
const T* operator->() {
return raw_;
}
void reset() {
auto i = (*p_cnt_)--;
borrow_verify(i > 0, "Trying to reset null pointer");
raw_ = nullptr;
p_cnt_ = nullptr;
}
~Ref() {
if (p_cnt_ != nullptr) {
auto i = (*p_cnt_)--;
borrow_verify(i > 0, "Trying to dereference null pointer"); // failure means - count became negative which is not possible
}
}
};
template <typename T>
class RefMut {
public:
T* raw_;
std::atomic<int32_t>* p_cnt_;
RefMut() = default;
RefMut(const RefMut&) = delete;
RefMut(RefMut&& p) : raw_(p.raw_), p_cnt_(p.p_cnt_) {
raw_ = p.raw_;
p.p_cnt_ = nullptr;
p.raw_ = nullptr;
}
T* operator->() {
return raw_;
}
void reset() {
auto i = (*p_cnt_)++;
borrow_verify(i == -1, "error in RefMut reset");
p_cnt_ = nullptr;
raw_ = nullptr;
}
~RefMut() {
if (p_cnt_) {
auto i = (*p_cnt_)++;
borrow_verify(i == -1, "error in checking just single reference of RefMut");
}
}
};
template <class T>
class RefCell {
public:
RefCell(const RefCell&) = delete;
RefCell(): raw_(nullptr), cnt_(0) {
}
explicit RefCell(T* p) : raw_(p), cnt_(0) {
};
RefCell(RefCell&& p) {
auto i = p.cnt_.exchange(-2);
borrow_verify(i==0, "verify failed in RefCell move constructor");
borrow_verify(cnt_ == 0, "verify failed in RefCell move constructor");
cnt_ = i;
raw_ = p.raw_;
p.raw_ = nullptr;
p.cnt_ = 0;
};
inline void reset(T* p) {
borrow_verify(cnt_ == 0, "error in RefCell reset");
raw_ = p;
borrow_verify(cnt_ == 0, "error in RefCell reset"); // is this enough to capture data race?
}
T* raw_{nullptr};
std::atomic<int32_t> cnt_{0};
inline RefMut<T> borrow_mut() {
RefMut<T> mut;
borrow_verify(cnt_ == 0, "verify failed in borrow_mut");
cnt_--;
mut.p_cnt_ = &cnt_;
mut.raw_ = raw_;
raw_ = nullptr;
return mut;
}
inline Ref<T> borrow_const() {
// *raw_; // for refer static analysis
auto i = cnt_++;
borrow_verify(i >= 0, "verify failed in borrow_const");
Ref<T> ref;
ref.raw_ = raw_;
ref.p_cnt_ = &cnt_;
return ref;
}
T* operator->() {
borrow_verify(cnt_==0, "verify failed in ->");
return raw_;
}
void reset() {
borrow_verify(cnt_ == 0, "verify failed in RefCell reset");
delete raw_;
raw_ = nullptr;
}
~RefCell() {
if (raw_) {
reset();
}
}
};
template <typename T>
inline RefMut<T> borrow_mut(RefCell<T>& RefCell) {
return std::forward<RefMut<T>>(RefCell.borrow_mut());
}
template <typename T>
inline Ref<T> borrow_const(RefCell<T>& RefCell) {
return std::forward<Ref<T>>(RefCell.borrow_const());
}
template <typename T>
inline void reset_ptr(RefCell<T>& ptr) {
return ptr.reset();
}
template <typename T>
inline void reset_ptr(RefMut<T>& ptr) {
return ptr.reset();
}
template <typename T>
inline void reset_ptr(Ref<T>& ptr) {
return ptr.reset();
}
} // namespace borrow;
// infer run --pulse-only -- clang++ -x c++ -std=c++11 -O0 borrow.h -D BORROW_TEST=1 -D BORROW_INFER_CHECK=1
#ifdef BORROW_TEST
using namespace borrow;
void test1() {
RefCell<int> owner;
owner.reset(new int(5));
auto x = borrow_mut(owner);
auto y = borrow_mut(owner);
}
void test2() {
RefCell<int> owner;
owner.reset(new int(5));
{
auto x = borrow_mut(owner);
}
auto y = borrow_mut(owner);
{
auto z = borrow_mut(owner);
}
}
void test3() {
RefCell<int> owner;
owner.reset(new int(5));
auto x = borrow_const(owner);
auto y = borrow_const(owner);
auto z = borrow_mut(owner);
}
int main() {
test1();
test2();
test3();
}
#endif