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
|
/*-
* 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 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;
}
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);
local_shared_ptr(nullptr_t) : local_shared_ptr() { }
~local_shared_ptr() {
if (cnt) {
if (cnt->release_shared() <= 0) {
cnt->dispose();
if (cnt->release_weak() <= 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);
};
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;
}
}
#endif //RSPAMD_LOCAL_SHARED_PTR_HXX
|