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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/*-
* Copyright 2021 Vsevolod Stakhov
*
* 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 RSPAMD_LOCAL_SHARED_PTR_HXX
#define RSPAMD_LOCAL_SHARED_PTR_HXX
#pragma once
#include <memory>
#include <algorithm> // for std::swap
#include <cstddef> // for std::size_t
#include <functional> // for std::less
/*
* Smart pointers with no atomic refcounts to speed up Rspamd which is
* apparently single threaded
*/
namespace rspamd {
namespace detail {
class ref_cnt {
public:
using refcount_t = int;
constexpr auto add_shared() -> refcount_t {
return ++ref_shared;
}
constexpr auto add_weak() -> refcount_t {
return ++ref_weak;
}
constexpr auto release_shared() -> refcount_t {
return --ref_shared;
}
constexpr auto release_weak() -> refcount_t {
return --ref_weak;
}
constexpr auto shared_count() const -> refcount_t {
return ref_shared;
}
constexpr auto weak_count() const -> refcount_t {
return ref_weak;
}
virtual ~ref_cnt() {}
virtual void dispose() = 0;
private:
refcount_t ref_weak = 0;
refcount_t ref_shared = 1;
};
template <class T>
class obj_and_refcnt : public ref_cnt {
private:
typedef typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type storage_type;
storage_type storage;
bool initialized;
virtual void dispose() override {
if (initialized) {
T *p = reinterpret_cast<T *>(&storage);
p->~T();
initialized = false;
}
}
public:
template <typename... Args>
explicit obj_and_refcnt(Args&&... args) : initialized(true)
{
new(&storage) T(std::forward<Args>(args)...);
}
auto get(void) -> T* {
if (initialized) {
return reinterpret_cast<T *>(&storage);
}
return nullptr;
}
virtual ~obj_and_refcnt() = default;
};
template <class T, class D = typename std::default_delete<T>>
class ptr_and_refcnt : public ref_cnt {
private:
T* ptr;
D deleter;
virtual void dispose() override {
deleter(ptr);
ptr = nullptr;
}
public:
explicit ptr_and_refcnt(T *_ptr, D d = std::default_delete<T>()) : ptr(_ptr),
deleter(std::move(d)) {}
virtual ~ptr_and_refcnt() = default;
};
}
template <class T> class local_weak_ptr;
template <class T>
class local_shared_ptr {
public:
typedef T element_type;
typedef local_weak_ptr<T> weak_type;
// Simplified comparing to libc++, no custom deleter and no rebind here
// constructors:
constexpr local_shared_ptr() noexcept : px(nullptr), cnt(nullptr) {}
template<class Y, typename std::enable_if<
std::is_convertible<Y*, element_type*>::value, bool>::type = true>
explicit local_shared_ptr(Y* p) : px(p), cnt(new detail::ptr_and_refcnt(p))
{
}
// custom deleter
template<class Y, class D, typename std::enable_if<
std::is_convertible<Y*, element_type*>::value, bool>::type = true>
explicit local_shared_ptr(Y* p, D d) : px(p), cnt(new detail::ptr_and_refcnt(p, std::move(d)))
{
}
local_shared_ptr(const local_shared_ptr& r) noexcept : px(r.px), cnt(r.cnt) {
if (cnt) {
cnt->add_shared();
}
}
local_shared_ptr(local_shared_ptr&& r) noexcept : px(r.px), cnt(r.cnt) {
r.px = nullptr;
r.cnt = nullptr;
}
template<class Y> explicit local_shared_ptr(const local_weak_ptr<Y>& r) : px(r.px), cnt(r.cnt) {
if (cnt) {
cnt->add_shared();
}
}
local_shared_ptr(std::nullptr_t) : local_shared_ptr() { }
~local_shared_ptr() {
if (cnt) {
if (cnt->release_shared() <= 0) {
cnt->dispose();
if (cnt->weak_count() == 0) {
delete cnt;
}
}
}
}
// assignment:
local_shared_ptr& operator=(const local_shared_ptr& r) noexcept {
local_shared_ptr(r).swap(*this);
return *this;
}
local_shared_ptr& operator=(local_shared_ptr&& r) noexcept {
local_shared_ptr(std::move(r)).swap(*this);
return *this;
}
// Mutators
void swap(local_shared_ptr& r) noexcept {
std::swap(this->cnt, r.cnt);
std::swap(this->px, r.px);
}
void reset() noexcept {
local_shared_ptr().swap(*this);
}
// Observers:
T* get() const noexcept {
return px;
}
T& operator*() const noexcept {
return *px;
}
T* operator->() const noexcept {
return px;
}
long use_count() const noexcept {
if (cnt) {
return cnt->shared_count();
}
return 0;
}
bool unique() const noexcept {
return use_count() == 1;
}
explicit operator bool() const noexcept {
return px != nullptr;
}
template<class Y, typename std::enable_if<
std::is_convertible<Y*, element_type*>::value, bool>::type = true>
auto operator ==(const local_shared_ptr<Y> &other) const -> bool {
return px == other.px;
}
template<class Y, typename std::enable_if<
std::is_convertible<Y*, element_type*>::value, bool>::type = true>
auto operator <(const local_shared_ptr<Y> &other) const -> auto {
return *px < *other.px;
}
private:
T *px; // contained pointer
detail::ref_cnt *cnt;
template<class _T, class ... Args>
friend local_shared_ptr<_T> local_make_shared(Args && ... args);
friend class local_weak_ptr<T>;
};
template<class T, class ... Args>
local_shared_ptr<T> local_make_shared(Args && ... args)
{
local_shared_ptr<T> ptr;
auto tmp_object = new detail::obj_and_refcnt<T>(std::forward<Args>(args)...);
ptr.px = tmp_object->get();
ptr.cnt = tmp_object;
return ptr;
}
template<class T>
class local_weak_ptr
{
public:
typedef T element_type;
// constructors
constexpr local_weak_ptr() noexcept : px(nullptr), cnt(nullptr) {}
template<class Y, typename std::enable_if<
std::is_convertible<Y*, element_type*>::value, bool>::type = true>
local_weak_ptr(local_shared_ptr<Y> const& r) noexcept : px(r.px),cnt(r.cnt) {
if (cnt) {
cnt->add_weak();
}
}
local_weak_ptr(local_weak_ptr const& r) noexcept : px(r.px),cnt(r.cnt) {
if (cnt) {
cnt->add_weak();
}
}
local_weak_ptr(local_weak_ptr && r) noexcept : px(r.px), cnt(r.cnt) {
r.px = nullptr;
r.cnt = nullptr;
}
~local_weak_ptr()
{
if (cnt) {
if (cnt->release_weak() <= 0 && cnt->shared_count() == 0) {
delete cnt;
}
}
}
// assignment
local_weak_ptr& operator=(local_weak_ptr const& r) noexcept {
local_weak_ptr(r).swap(*this);
return *this;
}
local_weak_ptr& operator=(local_shared_ptr<T> const& r) noexcept {
local_weak_ptr(r).swap(*this);
return *this;
}
template<class Y, typename std::enable_if<
std::is_convertible<Y*, element_type*>::value, bool>::type = true>
local_weak_ptr& operator=(local_weak_ptr<Y> const& r) noexcept {
local_weak_ptr(r).swap(*this);
return *this;
}
local_weak_ptr& operator=(local_weak_ptr&& r) noexcept {
local_weak_ptr(std::move(r)).swap(*this);
return *this;
}
// modifiers
void swap(local_weak_ptr& r) noexcept {
std::swap(this->cnt, r.cnt);
std::swap(this->px, r.px);
}
void reset() noexcept {
local_weak_ptr().swap(*this);
}
// observers
long use_count() const noexcept {
if (cnt) {
return cnt->shared_count();
}
return 0;
}
bool expired() const noexcept {
if (cnt) {
return cnt->shared_count() == 0;
}
return true;
}
local_shared_ptr<T> lock() const noexcept {
local_shared_ptr<T> tmp;
tmp.cnt = cnt;
if (cnt) {
cnt->add_shared();
tmp.px = px;
}
return tmp;
}
private:
element_type* px;
detail::ref_cnt *cnt;
};
}
/* Hashing stuff */
namespace std {
template <class T>
struct hash<rspamd::local_shared_ptr<T>> {
inline auto operator()(const rspamd::local_shared_ptr<T> &p) const -> auto {
if (!p) {
throw std::logic_error("no hash for dangling pointer");
}
return hash<T>()(*p.get());
}
};
template <class T>
struct hash<rspamd::local_weak_ptr<T>> {
inline auto operator()(const rspamd::local_weak_ptr<T> &p) const -> auto {
if (!p) {
throw std::logic_error("no hash for dangling pointer");
}
return hash<T>()(*p.get());
}
};
template<class T>
inline void swap(rspamd::local_shared_ptr<T> &x, rspamd::local_shared_ptr<T> &y) noexcept
{
x.swap(y);
}
template<class T>
inline void swap(rspamd::local_weak_ptr<T> &x, rspamd::local_weak_ptr<T> &y) noexcept
{
x.swap(y);
}
}
#endif //RSPAMD_LOCAL_SHARED_PTR_HXX
|