/
Utils.h
312 lines (265 loc) · 9.6 KB
/
Utils.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*****************************************************************************/
/* Milliways - B+ trees and key-value store C++ library */
/* */
/* Copyright 2016 Marco Pantaleoni and J CUBE Inc. Tokyo, Japan. */
/* */
/* Author: Marco Pantaleoni <marco.pantaleoni@gmail.com> */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/*****************************************************************************/
#ifndef MILLIWAYS_UTILS_H
#define MILLIWAYS_UTILS_H
#include <iostream>
#include <map>
#include <unordered_map>
#include <assert.h>
#include "config.h"
// #define USE_MILLIWAYS_SHPTR
#if !defined(USE_MILLIWAYS_SHPTR)
#if defined(HAVE_STD_SHARED_PTR)
#include <memory>
#define USE_STD_SHARED_PTR
#elif defined(HAVE_STD_TR1_SHARED_PTR)
#include <tr1/memory>
#define USE_TR1_SHARED_PTR
#else
#define USE_MILLIWAYS_SHPTR
#endif
#endif
#if ! defined(NDEBUG)
#undef TRACE
#define TRACE( TEXT ) \
do \
{ \
std::cerr << "[TRACE] " << TEXT << \
std::endl; \
std::cerr.flush(); \
} \
while( 0 )
#undef CTRACE
#define CTRACE( COND, TEXT ) \
do \
{ \
if ((COND)) { \
std::cerr << "[TRACE] " << TEXT << \
std::endl; \
std::cerr.flush(); \
} \
} \
while( 0 )
#else
#undef TRACE
#define TRACE( TEXT ) do { } while(0)
#undef CTRACE
#define CTRACE( COND, TEXT ) do { } while(0)
#endif
namespace milliways {
void hexdump(void* ptr, int buflen);
std::ostream& hexdump(std::ostream& out, const void* ptr, int buflen);
std::string s_hexdump(const void* ptr, int buflen);
std::string hexify(const std::string& input);
std::string dehexify(const std::string& input);
/* ----------------------------------------------------------------- *
* shptr<T> *
* ----------------------------------------------------------------- */
#if defined(USE_STD_SHARED_PTR)
template <typename T>
class shptr : public std::shared_ptr<T>
{
public:
shptr() : std::shared_ptr<T>() {}
explicit shptr(T* naked) : std::shared_ptr<T>(naked) {}
shptr(const shptr<T>& other) : std::shared_ptr<T>(other) {}
virtual ~shptr() {}
shptr<T>& operator=(const shptr<T>& rhs) { std::shared_ptr<T>::operator=(rhs); return *this; }
bool operator==(const shptr<T>& rhs) { return ((this == &rhs) || (this->get() == rhs.get())); }
bool operator!=(const shptr<T>& rhs) { return !(*this == rhs); }
bool operator<(const shptr<T>& rhs) const { return std::shared_ptr<T>::operator<(rhs); }
operator void*() const { return this->get(); }
operator bool() const { return this->get() ? true : false; }
long count() const { return -1; }
};
#elif defined(USE_STD_TR1_SHARED_PTR)
template <typename T>
class shptr : public std::tr1::shared_ptr<T>
{
public:
shptr() : std::tr1::shared_ptr<T>() {}
explicit shptr(T* naked) : std::tr1::shared_ptr<T>(naked) {}
shptr(const shptr<T>& other) : std::tr1::shared_ptr<T>(other) {}
virtual ~shptr() {}
shptr<T>& operator=(const shptr<T>& rhs) { std::tr1::shared_ptr<T>::operator=(rhs); return *this; }
bool operator==(const shptr<T>& rhs) { return ((this == &rhs) || (this->get() == rhs.get())); }
bool operator!=(const shptr<T>& rhs) { return !(*this == rhs); }
bool operator<(const shptr<T>& rhs) const { return std::tr1::shared_ptr<T>::operator<(rhs); }
operator void*() const { return this->get(); }
operator bool() const { return this->get() ? true : false; }
long count() const { return -1; }
};
#elif defined(USE_MILLIWAYS_SHPTR)
template <typename T>
class shptr;
class shptr_manager
{
typedef std::map<void*, long> refcnt_map_t;
public:
static shptr_manager& Instance() { return s_instance; }
protected:
refcnt_map_t& refcnt_map() { return m_refcnt_map; }
static refcnt_map_t& RefcntMap() { return Instance().refcnt_map(); }
template <typename T>
friend class shptr;
static shptr_manager s_instance;
refcnt_map_t m_refcnt_map;
private:
shptr_manager() {}
shptr_manager(const shptr_manager& other) {}
virtual ~shptr_manager() {
refcnt_map_t::const_iterator it;
for (it = m_refcnt_map.begin(); it != m_refcnt_map.end(); ++it) {
if (it->first && it->second) {
std::cerr << "WARNING: pointer " << (void*)it->first << " still has " << it->second << " references at the end of program lifetime." << std::endl;
}
}
}
};
template <typename T>
class shptr
{
typedef shptr_manager::refcnt_map_t refcnt_map_t;
public:
shptr() : m_naked(NULL) {}
explicit shptr(T* naked) : m_naked(NULL) { initialize_ptr(naked); }
shptr(const shptr<T>& other) : m_naked(NULL) { assert(other.verify()); initialize_ptr(other.m_naked); }
virtual ~shptr() { assert(verify()); finalize_ptr(); }
shptr<T>& operator=(const shptr<T>& rhs) {
if ((this == &rhs) || (*this == rhs)) return *this;
assert(verify());
finalize_ptr();
assert(rhs.verify());
initialize_ptr(rhs.m_naked);
return *this;
}
bool operator==(const shptr<T>& rhs) {
if (this == &rhs) return true;
assert(rhs.verify());
return (m_naked == rhs.m_naked) ? true : false;
}
bool operator!=(const shptr<T>& rhs) { return !(*this == rhs); }
bool operator<(const shptr<T>& rhs) const {
if (*this == rhs) return false;
assert(rhs.verify());
return (m_naked < rhs.m_naked);
}
T* get() const { assert(verify()); return m_naked; }
const T& operator*() const { assert(verify()); return *m_naked; }
T& operator*() { assert(verify()); return *m_naked; }
const T* operator->() const { assert(verify()); return m_naked; }
T* operator->() { assert(verify()); return m_naked; }
operator void*() const { assert(verify()); return m_naked; }
operator bool() const { assert(verify()); return m_naked ? true : false; }
shptr<T>& reset() { assert(verify()); finalize_ptr(); m_naked = NULL; assert(! m_naked); return *this; }
shptr<T>& reset(T* naked) {
if (naked == m_naked) return *this;
assert(verify());
finalize_ptr();
if (! naked) {
m_naked = naked;
return *this;
}
initialize_ptr(naked);
return *this;
}
shptr<T>& swap(shptr<T>& other) {
if ((this == &other) || (*this == other)) return *this;
assert(verify());
T* tmp = m_naked;
m_naked = other.m_naked; other.m_naked = tmp;
return *this;
}
long count() const {
if (! m_naked) return 0;
// WARNING: not thread safe
// TODO: implement mutex locking for thread-safety
refcnt_map_t& refcnt_map = shptr_manager::RefcntMap();
if (! refcnt_map.count(m_naked))
return 0;
return refcnt_map[m_naked];
}
#if defined(MILLIWAYS_CHECK_SHARED_POINTER_CORRUPTION)
bool verify() const {
if (! m_naked) return true;
// WARNING: not thread safe
// TODO: implement mutex locking for thread-safety
refcnt_map_t& refcnt_map = shptr_manager::RefcntMap();
if (! refcnt_map.count(m_naked))
return false;
// return (refcnt_map[m_naked] > 0) ? true : false;
return true;
}
#else
bool verify() const { return true; }
#endif
protected:
void finalize_ptr() {
if (! m_naked) return;
assert(m_naked);
assert(verify());
// WARNING: not thread safe
// TODO: implement mutex locking for thread-safety
refcnt_map_t& refcnt_map = shptr_manager::RefcntMap();
if (! refcnt_map.count((void*)m_naked)) {
throw std::runtime_error("ERROR: raw pointer missing from internal books!");
assert(false);
} else {
refcnt_map[(void*)m_naked]--;
if (refcnt_map[(void*)m_naked] == 0) {
refcnt_map.erase((void*)m_naked);
delete m_naked;
m_naked = NULL;
}
}
m_naked = NULL;
}
void initialize_ptr(T* naked) {
assert(! m_naked);
m_naked = naked;
if (! naked)
return;
assert(m_naked);
// WARNING: not thread safe
// TODO: implement mutex locking for thread-safety
refcnt_map_t& refcnt_map = shptr_manager::RefcntMap();
if (! refcnt_map.count((void*)m_naked)) {
refcnt_map[(void*)m_naked] = 1;
} else {
refcnt_map[(void*)m_naked]++;
}
assert(m_naked == naked);
}
T* m_naked;
private:
};
template <typename T>
inline std::ostream& operator<< ( std::ostream& out, const shptr<T>& value )
{
if (value)
out << "<shptr ptr:" << (void*)value << " (" << value.count() << " refs)>";
else
out << "<shptr null>";
return out;
}
#endif /* defined(USE_MILLIWAYS_SHPTR) */
} /* end of namespace milliways */
#include "Utils.impl.hpp"
#endif /* MILLIWAYS_UTILS_H */