mirror of
https://github.com/scylladb/scylladb.git
synced 2026-04-21 00:50:35 +00:00
shared_ptr: rename to lw_shared_ptr (for light-weight)
The current shared_ptr implementation is efficient, but does not support polymorphic types. Rename it in order to make room for a polymorphic shared_ptr.
This commit is contained in:
@@ -63,13 +63,13 @@ public:
|
||||
};
|
||||
|
||||
class subdevice {
|
||||
foreign_ptr<shared_ptr<flashcache::devfile>> _dev;
|
||||
foreign_ptr<lw_shared_ptr<flashcache::devfile>> _dev;
|
||||
uint64_t _offset;
|
||||
uint64_t _end;
|
||||
std::queue<block> _free_blocks;
|
||||
semaphore _par = { 1000 };
|
||||
public:
|
||||
subdevice(foreign_ptr<shared_ptr<flashcache::devfile>> dev, uint64_t offset, uint64_t length)
|
||||
subdevice(foreign_ptr<lw_shared_ptr<flashcache::devfile>> dev, uint64_t offset, uint64_t length)
|
||||
: _dev(std::move(dev))
|
||||
, _offset(offset)
|
||||
, _end(offset + length)
|
||||
@@ -417,7 +417,7 @@ private:
|
||||
item_lru_list _lru;
|
||||
cache_stats _stats;
|
||||
public:
|
||||
void do_setup(foreign_ptr<shared_ptr<flashcache::devfile>> dev, uint64_t offset, uint64_t length) {}
|
||||
void do_setup(foreign_ptr<lw_shared_ptr<flashcache::devfile>> dev, uint64_t offset, uint64_t length) {}
|
||||
|
||||
void do_erase(item_type& item_ref) {
|
||||
_lru.erase(_lru.iterator_to(item_ref));
|
||||
@@ -462,7 +462,7 @@ public:
|
||||
return subdev_ref;
|
||||
}
|
||||
|
||||
void do_setup(foreign_ptr<shared_ptr<flashcache::devfile>> dev, uint64_t offset, uint64_t length) {
|
||||
void do_setup(foreign_ptr<lw_shared_ptr<flashcache::devfile>> dev, uint64_t offset, uint64_t length) {
|
||||
_subdev = std::make_unique<flashcache::subdevice>(std::move(dev), offset, length);
|
||||
}
|
||||
|
||||
@@ -878,7 +878,7 @@ public:
|
||||
_flush_timer.set_callback([this] { flush_all(); });
|
||||
}
|
||||
|
||||
future<> setup(foreign_ptr<shared_ptr<flashcache::devfile>> dev, uint64_t offset, uint64_t length) {
|
||||
future<> setup(foreign_ptr<lw_shared_ptr<flashcache::devfile>> dev, uint64_t offset, uint64_t length) {
|
||||
this->do_setup(std::move(dev), offset, length);
|
||||
return make_ready_future<>();
|
||||
}
|
||||
@@ -1058,7 +1058,7 @@ public:
|
||||
return decr<remote_origin_tag>(key, delta);
|
||||
}
|
||||
|
||||
std::pair<unsigned, foreign_ptr<shared_ptr<std::string>>> print_hash_stats() {
|
||||
std::pair<unsigned, foreign_ptr<lw_shared_ptr<std::string>>> print_hash_stats() {
|
||||
static constexpr unsigned bits = sizeof(size_t) * 8;
|
||||
size_t histo[bits + 1] {};
|
||||
size_t max_size = 0;
|
||||
@@ -1195,7 +1195,7 @@ public:
|
||||
}
|
||||
|
||||
future<> print_hash_stats(output_stream<char>& out) {
|
||||
return _peers.map_reduce([&out] (std::pair<unsigned, foreign_ptr<shared_ptr<std::string>>> data) mutable {
|
||||
return _peers.map_reduce([&out] (std::pair<unsigned, foreign_ptr<lw_shared_ptr<std::string>>> data) mutable {
|
||||
return out.write("=== CPU " + std::to_string(data.first) + " ===\r\n")
|
||||
.then([&out, str = std::move(data.second)] {
|
||||
return out.write(*str);
|
||||
@@ -1693,7 +1693,7 @@ public:
|
||||
template <bool WithFlashCache>
|
||||
class tcp_server {
|
||||
private:
|
||||
shared_ptr<server_socket> _listener;
|
||||
lw_shared_ptr<server_socket> _listener;
|
||||
sharded_cache<WithFlashCache>& _cache;
|
||||
distributed<system_stats>& _system_stats;
|
||||
uint16_t _port;
|
||||
|
||||
@@ -156,7 +156,7 @@ template <typename T>
|
||||
struct reducer_with_get_traits {
|
||||
using result_type = decltype(std::declval<T>().get());
|
||||
using future_type = future<result_type>;
|
||||
static future_type maybe_call_get(future<> f, shared_ptr<T> r) {
|
||||
static future_type maybe_call_get(future<> f, lw_shared_ptr<T> r) {
|
||||
return f.then([r = std::move(r)] () mutable {
|
||||
return make_ready_future<result_type>(std::move(*r).get());
|
||||
});
|
||||
@@ -166,7 +166,7 @@ struct reducer_with_get_traits {
|
||||
template <typename T, typename V = void>
|
||||
struct reducer_traits {
|
||||
using future_type = future<>;
|
||||
static future_type maybe_call_get(future<> f, shared_ptr<T> r) {
|
||||
static future_type maybe_call_get(future<> f, lw_shared_ptr<T> r) {
|
||||
return f.then([r = std::move(r)] {});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -742,7 +742,7 @@ void configure(std::vector<resource::memory> m,
|
||||
if (hugetlbfs_path) {
|
||||
// std::function is copyable, but file_desc is not, so we must use
|
||||
// a shared_ptr to allow sys_alloc to be copied around
|
||||
auto fdp = make_shared<file_desc>(file_desc::temporary(*hugetlbfs_path));
|
||||
auto fdp = make_lw_shared<file_desc>(file_desc::temporary(*hugetlbfs_path));
|
||||
sys_alloc = [fdp] (optional<void*> where, size_t how_much) {
|
||||
return allocate_hugetlbfs_memory(*fdp, where, how_much);
|
||||
};
|
||||
|
||||
@@ -286,7 +286,7 @@ private:
|
||||
typedef value_list_map::iterator iterator;
|
||||
typedef std::tuple<iterator, cpwriter> context;
|
||||
|
||||
auto ctxt = make_shared<context>();
|
||||
auto ctxt = make_lw_shared<context>();
|
||||
|
||||
// note we're doing this unsynced since we assume
|
||||
// all registrations to this instance will be done on the
|
||||
|
||||
@@ -9,19 +9,19 @@
|
||||
#include <type_traits>
|
||||
|
||||
template <typename T>
|
||||
class shared_ptr;
|
||||
class lw_shared_ptr;
|
||||
|
||||
template <typename T>
|
||||
class enable_shared_from_this;
|
||||
class enable_lw_shared_from_this;
|
||||
|
||||
template <typename T, typename... A>
|
||||
shared_ptr<T> make_shared(A&&... a);
|
||||
lw_shared_ptr<T> make_lw_shared(A&&... a);
|
||||
|
||||
template <typename T>
|
||||
shared_ptr<T> make_shared(T&& a);
|
||||
lw_shared_ptr<T> make_lw_shared(T&& a);
|
||||
|
||||
template <typename T>
|
||||
shared_ptr<T> make_shared(T& a);
|
||||
lw_shared_ptr<T> make_lw_shared(T& a);
|
||||
|
||||
// We want to support two use cases for shared_ptr<T>:
|
||||
//
|
||||
@@ -43,18 +43,18 @@ shared_ptr<T> make_shared(T& a);
|
||||
|
||||
// CRTP from this to enable shared_from_this:
|
||||
template <typename T>
|
||||
class enable_shared_from_this {
|
||||
class enable_lw_shared_from_this {
|
||||
long _count = 0;
|
||||
using ctor = T;
|
||||
T* to_value() { return static_cast<T*>(this); }
|
||||
T* to_internal_object() { return static_cast<T*>(this); }
|
||||
protected:
|
||||
enable_shared_from_this& operator=(const enable_shared_from_this&) { return *this; }
|
||||
enable_shared_from_this& operator=(enable_shared_from_this&&) { return *this; }
|
||||
enable_lw_shared_from_this& operator=(const enable_lw_shared_from_this&) { return *this; }
|
||||
enable_lw_shared_from_this& operator=(enable_lw_shared_from_this&&) { return *this; }
|
||||
public:
|
||||
shared_ptr<T> shared_from_this();
|
||||
lw_shared_ptr<T> shared_from_this();
|
||||
template <typename X>
|
||||
friend class shared_ptr;
|
||||
friend class lw_shared_ptr;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
@@ -71,64 +71,64 @@ struct shared_ptr_no_esft {
|
||||
template <typename... A>
|
||||
shared_ptr_no_esft(A&&... a) : _value(std::forward<A>(a)...) {}
|
||||
template <typename X>
|
||||
friend class shared_ptr;
|
||||
friend class lw_shared_ptr;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
using shared_ptr_impl
|
||||
= std::conditional_t<
|
||||
std::is_base_of<enable_shared_from_this<T>, T>::value,
|
||||
enable_shared_from_this<T>,
|
||||
std::is_base_of<enable_lw_shared_from_this<T>, T>::value,
|
||||
enable_lw_shared_from_this<T>,
|
||||
shared_ptr_no_esft<T>
|
||||
>;
|
||||
|
||||
template <typename T>
|
||||
class shared_ptr {
|
||||
class lw_shared_ptr {
|
||||
mutable shared_ptr_impl<T>* _p = nullptr;
|
||||
private:
|
||||
shared_ptr(shared_ptr_impl<T>* p) : _p(p) {
|
||||
lw_shared_ptr(shared_ptr_impl<T>* p) : _p(p) {
|
||||
if (_p) {
|
||||
++_p->_count;
|
||||
}
|
||||
}
|
||||
template <typename... A>
|
||||
static shared_ptr make(A&&... a) {
|
||||
return shared_ptr(new typename shared_ptr_impl<T>::ctor(std::forward<A>(a)...));
|
||||
static lw_shared_ptr make(A&&... a) {
|
||||
return lw_shared_ptr(new typename shared_ptr_impl<T>::ctor(std::forward<A>(a)...));
|
||||
}
|
||||
public:
|
||||
using element_type = T;
|
||||
|
||||
shared_ptr() = default;
|
||||
shared_ptr(const shared_ptr& x) : _p(x._p) {
|
||||
lw_shared_ptr() = default;
|
||||
lw_shared_ptr(const lw_shared_ptr& x) : _p(x._p) {
|
||||
if (_p) {
|
||||
++_p->_count;
|
||||
}
|
||||
}
|
||||
shared_ptr(shared_ptr&& x) : _p(x._p) {
|
||||
lw_shared_ptr(lw_shared_ptr&& x) : _p(x._p) {
|
||||
x._p = nullptr;
|
||||
}
|
||||
~shared_ptr() {
|
||||
~lw_shared_ptr() {
|
||||
if (_p && !--_p->_count) {
|
||||
delete _p->to_internal_object();
|
||||
}
|
||||
}
|
||||
shared_ptr& operator=(const shared_ptr& x) {
|
||||
lw_shared_ptr& operator=(const lw_shared_ptr& x) {
|
||||
if (_p != x._p) {
|
||||
this->~shared_ptr();
|
||||
new (this) shared_ptr(x);
|
||||
this->~lw_shared_ptr();
|
||||
new (this) lw_shared_ptr(x);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
shared_ptr& operator=(shared_ptr&& x) {
|
||||
lw_shared_ptr& operator=(lw_shared_ptr&& x) {
|
||||
if (_p != x._p) {
|
||||
this->~shared_ptr();
|
||||
new (this) shared_ptr(std::move(x));
|
||||
this->~lw_shared_ptr();
|
||||
new (this) lw_shared_ptr(std::move(x));
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
shared_ptr& operator=(T&& x) {
|
||||
this->~shared_ptr();
|
||||
new (this) shared_ptr(make_shared<T>(std::move(x)));
|
||||
lw_shared_ptr& operator=(T&& x) {
|
||||
this->~lw_shared_ptr();
|
||||
new (this) lw_shared_ptr(make_lw_shared<T>(std::move(x)));
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -144,8 +144,8 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
operator shared_ptr<const T>() const {
|
||||
return shared_ptr<const T>(_p);
|
||||
operator lw_shared_ptr<const T>() const {
|
||||
return lw_shared_ptr<const T>(_p);
|
||||
}
|
||||
|
||||
explicit operator bool() const {
|
||||
@@ -157,41 +157,41 @@ public:
|
||||
}
|
||||
|
||||
template <typename X, typename... A>
|
||||
friend shared_ptr<X> make_shared(A&&...);
|
||||
friend lw_shared_ptr<X> make_lw_shared(A&&...);
|
||||
|
||||
template <typename U>
|
||||
friend shared_ptr<U> make_shared(U&&);
|
||||
friend lw_shared_ptr<U> make_lw_shared(U&&);
|
||||
|
||||
template <typename U>
|
||||
friend shared_ptr<U> make_shared(U&);
|
||||
friend lw_shared_ptr<U> make_lw_shared(U&);
|
||||
|
||||
template <typename U>
|
||||
friend class enable_shared_from_this;
|
||||
friend class enable_lw_shared_from_this;
|
||||
};
|
||||
|
||||
template <typename T, typename... A>
|
||||
inline
|
||||
shared_ptr<T> make_shared(A&&... a) {
|
||||
return shared_ptr<T>::make(std::forward<A>(a)...);
|
||||
lw_shared_ptr<T> make_lw_shared(A&&... a) {
|
||||
return lw_shared_ptr<T>::make(std::forward<A>(a)...);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline
|
||||
shared_ptr<T> make_shared(T&& a) {
|
||||
return shared_ptr<T>::make(std::move(a));
|
||||
lw_shared_ptr<T> make_lw_shared(T&& a) {
|
||||
return lw_shared_ptr<T>::make(std::move(a));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline
|
||||
shared_ptr<T> make_shared(T& a) {
|
||||
return shared_ptr<T>::make(a);
|
||||
lw_shared_ptr<T> make_lw_shared(T& a) {
|
||||
return lw_shared_ptr<T>::make(a);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline
|
||||
shared_ptr<T>
|
||||
enable_shared_from_this<T>::shared_from_this() {
|
||||
return shared_ptr<T>(this);
|
||||
lw_shared_ptr<T>
|
||||
enable_lw_shared_from_this<T>::shared_from_this() {
|
||||
return lw_shared_ptr<T>(this);
|
||||
}
|
||||
|
||||
#endif /* SHARED_PTR_HH_ */
|
||||
|
||||
@@ -247,7 +247,7 @@ future<> ipv4::send(ipv4_address to, ip_protocol_num proto_num, packet p) {
|
||||
uint16_t remaining;
|
||||
uint16_t offset;
|
||||
};
|
||||
auto si = make_shared<send_info>({std::move(p), remaining, offset});
|
||||
auto si = make_lw_shared<send_info>({std::move(p), remaining, offset});
|
||||
auto stop = [si] { return si->remaining == 0; };
|
||||
auto send_frag = [this, send_pkt, si] () mutable {
|
||||
auto& remaining = si->remaining;
|
||||
|
||||
@@ -184,7 +184,7 @@ native_network_stack::listen(socket_address sa, listen_options opts) {
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
future<> native_network_stack::run_dhcp(bool is_renew, const dhcp::lease& res) {
|
||||
shared_ptr<dhcp> d = make_shared<dhcp>(_inet);
|
||||
lw_shared_ptr<dhcp> d = make_lw_shared<dhcp>(_inet);
|
||||
|
||||
// Hijack the ip-stack.
|
||||
for (unsigned i = 0; i < smp::count; i++) {
|
||||
|
||||
12
net/tcp.hh
12
net/tcp.hh
@@ -148,7 +148,7 @@ public:
|
||||
private:
|
||||
class tcb;
|
||||
|
||||
class tcb : public enable_shared_from_this<tcb> {
|
||||
class tcb : public enable_lw_shared_from_this<tcb> {
|
||||
using clock_type = lowres_clock;
|
||||
// Instead of tracking state through an enum, track individual
|
||||
// bits of the state. This reduces duplication in state handling.
|
||||
@@ -298,13 +298,13 @@ private:
|
||||
friend class connection;
|
||||
};
|
||||
inet_type& _inet;
|
||||
std::unordered_map<connid, shared_ptr<tcb>, connid_hash> _tcbs;
|
||||
std::unordered_map<connid, lw_shared_ptr<tcb>, connid_hash> _tcbs;
|
||||
std::unordered_map<uint16_t, listener*> _listening;
|
||||
public:
|
||||
class connection {
|
||||
shared_ptr<tcb> _tcb;
|
||||
lw_shared_ptr<tcb> _tcb;
|
||||
public:
|
||||
explicit connection(shared_ptr<tcb> tcbp) : _tcb(std::move(tcbp)) { _tcb->_conn = this; }
|
||||
explicit connection(lw_shared_ptr<tcb> tcbp) : _tcb(std::move(tcbp)) { _tcb->_conn = this; }
|
||||
connection(const connection&) = delete;
|
||||
connection(connection&& x) noexcept : _tcb(std::move(x._tcb)) {
|
||||
_tcb->_conn = this;
|
||||
@@ -406,14 +406,14 @@ void tcp<InetTraits>::received(packet p, ipaddr from, ipaddr to) {
|
||||
auto h = ntoh(*th);
|
||||
auto id = connid{to, from, h.dst_port, h.src_port};
|
||||
auto tcbi = _tcbs.find(id);
|
||||
shared_ptr<tcb> tcbp;
|
||||
lw_shared_ptr<tcb> tcbp;
|
||||
if (tcbi == _tcbs.end()) {
|
||||
if (h.f_syn && !h.f_ack) {
|
||||
auto listener = _listening.find(id.local_port);
|
||||
if (listener == _listening.end() || listener->second->_q.full()) {
|
||||
return respond_with_reset(&h, id.local_ip, id.foreign_ip);
|
||||
}
|
||||
tcbp = make_shared<tcb>(*this, id);
|
||||
tcbp = make_lw_shared<tcb>(*this, id);
|
||||
listener->second->_q.push(connection(tcbp));
|
||||
_tcbs.insert({id, tcbp});
|
||||
}
|
||||
|
||||
@@ -46,9 +46,9 @@ private:
|
||||
udp_v4& _proto;
|
||||
udp_v4::registration _reg;
|
||||
bool _closed;
|
||||
shared_ptr<udp_channel_state> _state;
|
||||
lw_shared_ptr<udp_channel_state> _state;
|
||||
public:
|
||||
native_channel(udp_v4 &proto, udp_v4::registration reg, shared_ptr<udp_channel_state> state)
|
||||
native_channel(udp_v4 &proto, udp_v4::registration reg, lw_shared_ptr<udp_channel_state> state)
|
||||
: _proto(proto)
|
||||
, _reg(reg)
|
||||
, _closed(false)
|
||||
@@ -168,7 +168,7 @@ udp_v4::make_channel(ipv4_addr addr) {
|
||||
_next_anonymous_port = next_port(_next_anonymous_port);
|
||||
}
|
||||
|
||||
auto chan_state = make_shared<udp_channel_state>(_queue_size);
|
||||
auto chan_state = make_lw_shared<udp_channel_state>(_queue_size);
|
||||
_channels[bind_port] = chan_state;
|
||||
return udp_channel(std::make_unique<native_channel>(*this, registration(*this, bind_port), chan_state));
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ public:
|
||||
private:
|
||||
static const uint16_t min_anonymous_port = 32768;
|
||||
ipv4 &_inet;
|
||||
std::unordered_map<uint16_t, shared_ptr<udp_channel_state>> _channels;
|
||||
std::unordered_map<uint16_t, lw_shared_ptr<udp_channel_state>> _channels;
|
||||
int _queue_size = default_queue_size;
|
||||
uint16_t _next_anonymous_port = min_anonymous_port;
|
||||
private:
|
||||
|
||||
@@ -37,7 +37,7 @@ static auto parse(packet&& p) {
|
||||
auto parser = make_shared<parser_type>();
|
||||
parser->init();
|
||||
return is->consume(*parser).then([is, parser] {
|
||||
return make_ready_future<shared_ptr<parser_type>>(parser);
|
||||
return make_ready_future<lw_shared_ptr<parser_type>>(parser);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ struct stream_maker {
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
shared_ptr<output_stream<char>> operator()(data_sink sink) {
|
||||
lw_shared_ptr<output_stream<char>> operator()(data_sink sink) {
|
||||
return make_shared<output_stream<char>>(std::move(sink), _size, _trim);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -85,7 +85,7 @@ public:
|
||||
keep_doing([this] {
|
||||
return _chan.receive().then([this] (udp_datagram dgram) {
|
||||
auto chunk = next_chunk();
|
||||
shared_ptr<sstring> item;
|
||||
lw_shared_ptr<sstring> item;
|
||||
if (_copy) {
|
||||
_packets.clear();
|
||||
_out->write(chunk, _chunk_size);
|
||||
|
||||
Reference in New Issue
Block a user