2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/MpscLinkQueue.h"
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
|
|
|
#include <new>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
class AtomicRefCnt {
|
|
|
|
public:
|
|
|
|
explicit AtomicRefCnt(uint64 cnt) : cnt_(cnt) {
|
|
|
|
}
|
|
|
|
void inc() {
|
|
|
|
cnt_.fetch_add(1, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
bool dec() {
|
|
|
|
return cnt_.fetch_sub(1, std::memory_order_acq_rel) == 1;
|
|
|
|
}
|
|
|
|
uint64 value() const {
|
|
|
|
return cnt_.load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-01-24 04:18:14 +01:00
|
|
|
std::atomic<uint64> cnt_{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class DataT, class DeleterT>
|
2020-06-26 01:24:13 +02:00
|
|
|
class SharedPtrRaw
|
|
|
|
: public DeleterT
|
|
|
|
, private MpscLinkQueueImpl::Node {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit SharedPtrRaw(DeleterT deleter) : DeleterT(std::move(deleter)), ref_cnt_{0}, option_magic_(Magic) {
|
|
|
|
}
|
|
|
|
|
|
|
|
~SharedPtrRaw() {
|
|
|
|
CHECK(use_cnt() == 0);
|
|
|
|
CHECK(option_magic_ == Magic);
|
|
|
|
}
|
|
|
|
template <class... ArgsT>
|
2021-12-09 22:27:13 +01:00
|
|
|
void init_data(ArgsT &&...args) {
|
2018-12-31 20:04:05 +01:00
|
|
|
new (&option_data_) DataT(std::forward<ArgsT>(args)...);
|
|
|
|
}
|
|
|
|
void destroy_data() {
|
|
|
|
option_data_.~DataT();
|
|
|
|
option_magic_ = Magic;
|
|
|
|
}
|
|
|
|
uint64 use_cnt() const {
|
|
|
|
return ref_cnt_.value();
|
|
|
|
}
|
|
|
|
void inc() {
|
|
|
|
ref_cnt_.inc();
|
|
|
|
}
|
|
|
|
bool dec() {
|
|
|
|
return ref_cnt_.dec();
|
|
|
|
}
|
|
|
|
DataT &data() {
|
|
|
|
return option_data_;
|
|
|
|
}
|
|
|
|
static SharedPtrRaw *from_mpsc_link_queue_node(MpscLinkQueueImpl::Node *node) {
|
|
|
|
return static_cast<SharedPtrRaw<DataT, DeleterT> *>(node);
|
|
|
|
}
|
|
|
|
MpscLinkQueueImpl::Node *to_mpsc_link_queue_node() {
|
|
|
|
return static_cast<MpscLinkQueueImpl::Node *>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AtomicRefCnt ref_cnt_;
|
|
|
|
enum { Magic = 0x732817a2 };
|
|
|
|
union {
|
|
|
|
DataT option_data_;
|
|
|
|
uint32 option_magic_;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T, class DeleterT = std::default_delete<T>>
|
|
|
|
class SharedPtr {
|
|
|
|
public:
|
|
|
|
using Raw = detail::SharedPtrRaw<T, DeleterT>;
|
2020-06-24 13:47:36 +02:00
|
|
|
struct acquire_t {};
|
2018-12-31 20:04:05 +01:00
|
|
|
SharedPtr() = default;
|
|
|
|
~SharedPtr() {
|
|
|
|
if (!raw_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
explicit SharedPtr(Raw *raw) : raw_(raw) {
|
2018-08-13 19:15:09 +02:00
|
|
|
if (raw_) {
|
|
|
|
raw_->inc();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-06-24 13:47:36 +02:00
|
|
|
SharedPtr(acquire_t, Raw *raw) : raw_(raw) {
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
SharedPtr(const SharedPtr &other) : SharedPtr(other.raw_) {
|
|
|
|
}
|
|
|
|
SharedPtr &operator=(const SharedPtr &other) {
|
2021-10-18 11:57:13 +02:00
|
|
|
if (this == &other) {
|
|
|
|
return *this;
|
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
if (other.raw_) {
|
|
|
|
other.raw_->inc();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
reset(other.raw_);
|
|
|
|
return *this;
|
|
|
|
}
|
2021-10-18 13:36:15 +02:00
|
|
|
SharedPtr(SharedPtr &&other) noexcept : raw_(other.raw_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
other.raw_ = nullptr;
|
|
|
|
}
|
2021-10-18 13:36:15 +02:00
|
|
|
SharedPtr &operator=(SharedPtr &&other) noexcept {
|
2018-12-31 20:04:05 +01:00
|
|
|
reset(other.raw_);
|
|
|
|
other.raw_ = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
2022-10-09 18:30:18 +02:00
|
|
|
bool empty() const noexcept {
|
2018-12-31 20:04:05 +01:00
|
|
|
return raw_ == nullptr;
|
|
|
|
}
|
2022-10-09 18:30:18 +02:00
|
|
|
explicit operator bool() const noexcept {
|
2018-12-31 20:04:05 +01:00
|
|
|
return !empty();
|
|
|
|
}
|
|
|
|
uint64 use_cnt() const {
|
|
|
|
if (!raw_) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return raw_->use_cnt();
|
|
|
|
}
|
|
|
|
T &operator*() const {
|
|
|
|
return raw_->data();
|
|
|
|
}
|
|
|
|
T *operator->() const {
|
|
|
|
return &raw_->data();
|
|
|
|
}
|
|
|
|
|
|
|
|
Raw *release() {
|
|
|
|
auto res = raw_;
|
|
|
|
raw_ = nullptr;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset(Raw *new_raw = nullptr) {
|
|
|
|
if (raw_ && raw_->dec()) {
|
|
|
|
raw_->destroy_data();
|
|
|
|
auto deleter = std::move(static_cast<DeleterT &>(*raw_));
|
|
|
|
deleter(raw_);
|
|
|
|
}
|
|
|
|
raw_ = new_raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... ArgsT>
|
2021-12-09 22:27:13 +01:00
|
|
|
static SharedPtr<T, DeleterT> create(ArgsT &&...args) {
|
2018-09-27 03:19:03 +02:00
|
|
|
auto raw = make_unique<Raw>(DeleterT());
|
2018-12-31 20:04:05 +01:00
|
|
|
raw->init_data(std::forward<ArgsT>(args)...);
|
|
|
|
return SharedPtr<T, DeleterT>(raw.release());
|
|
|
|
}
|
|
|
|
template <class D, class... ArgsT>
|
2021-12-09 22:27:13 +01:00
|
|
|
static SharedPtr<T, DeleterT> create_with_deleter(D &&d, ArgsT &&...args) {
|
2018-09-27 03:19:03 +02:00
|
|
|
auto raw = make_unique<Raw>(std::forward<D>(d));
|
2018-12-31 20:04:05 +01:00
|
|
|
raw->init_data(std::forward<ArgsT>(args)...);
|
|
|
|
return SharedPtr<T, DeleterT>(raw.release());
|
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
bool operator==(const SharedPtr<T, DeleterT> &other) const {
|
|
|
|
return raw_ == other.raw_;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Raw *raw_{nullptr};
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template <class DataT>
|
|
|
|
class SharedObjectPool {
|
|
|
|
class Deleter;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using Ptr = detail::SharedPtr<DataT, Deleter>;
|
|
|
|
|
|
|
|
SharedObjectPool() = default;
|
2023-05-05 12:51:19 +02:00
|
|
|
SharedObjectPool(const SharedObjectPool &) = delete;
|
|
|
|
SharedObjectPool &operator=(const SharedObjectPool &) = delete;
|
|
|
|
SharedObjectPool(SharedObjectPool &&) = delete;
|
|
|
|
SharedObjectPool &operator=(SharedObjectPool &&) = delete;
|
2018-12-31 20:04:05 +01:00
|
|
|
~SharedObjectPool() {
|
|
|
|
free_queue_.pop_all(free_queue_reader_);
|
|
|
|
size_t free_cnt = 0;
|
|
|
|
while (free_queue_reader_.read()) {
|
|
|
|
free_cnt++;
|
|
|
|
}
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(free_cnt == allocated_.size()) << free_cnt << " " << allocated_.size();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class... ArgsT>
|
2021-12-09 22:27:13 +01:00
|
|
|
Ptr alloc(ArgsT &&...args) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto *raw = alloc_raw();
|
|
|
|
raw->init_data(std::forward<ArgsT>(args)...);
|
|
|
|
return Ptr(raw);
|
|
|
|
}
|
|
|
|
size_t total_size() const {
|
|
|
|
return allocated_.size();
|
|
|
|
}
|
|
|
|
uint64 calc_free_size() {
|
|
|
|
free_queue_.pop_all(free_queue_reader_);
|
|
|
|
return free_queue_reader_.calc_size();
|
|
|
|
}
|
|
|
|
|
2022-10-12 14:59:58 +02:00
|
|
|
// non-thread-safe
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class F>
|
|
|
|
void for_each(F &&f) {
|
|
|
|
for (auto &raw : allocated_) {
|
|
|
|
if (raw->use_cnt() > 0) {
|
|
|
|
f(raw->data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
using Raw = typename Ptr::Raw;
|
|
|
|
Raw *alloc_raw() {
|
|
|
|
free_queue_.pop_all(free_queue_reader_);
|
|
|
|
auto *raw = free_queue_reader_.read().get();
|
|
|
|
if (raw) {
|
|
|
|
return raw;
|
|
|
|
}
|
2018-09-27 03:19:03 +02:00
|
|
|
allocated_.push_back(make_unique<Raw>(deleter()));
|
2018-12-31 20:04:05 +01:00
|
|
|
return allocated_.back().get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_raw(Raw *raw) {
|
|
|
|
free_queue_.push(Node{raw});
|
|
|
|
}
|
|
|
|
|
|
|
|
class Node {
|
|
|
|
public:
|
|
|
|
Node() = default;
|
|
|
|
explicit Node(Raw *raw) : raw_(raw) {
|
|
|
|
}
|
|
|
|
|
|
|
|
MpscLinkQueueImpl::Node *to_mpsc_link_queue_node() {
|
|
|
|
return raw_->to_mpsc_link_queue_node();
|
|
|
|
}
|
|
|
|
static Node from_mpsc_link_queue_node(MpscLinkQueueImpl::Node *node) {
|
|
|
|
return Node{Raw::from_mpsc_link_queue_node(node)};
|
|
|
|
}
|
|
|
|
Raw *get() const {
|
|
|
|
return raw_;
|
|
|
|
}
|
2022-10-12 14:59:58 +02:00
|
|
|
explicit operator bool() const noexcept {
|
2018-12-31 20:04:05 +01:00
|
|
|
return raw_ != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Raw *raw_{nullptr};
|
|
|
|
};
|
|
|
|
|
|
|
|
class Deleter {
|
|
|
|
public:
|
|
|
|
explicit Deleter(SharedObjectPool<DataT> *pool) : pool_(pool) {
|
|
|
|
}
|
|
|
|
void operator()(Raw *raw) {
|
|
|
|
pool_->free_raw(raw);
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
SharedObjectPool<DataT> *pool_;
|
|
|
|
};
|
|
|
|
friend class Deleter;
|
|
|
|
|
2018-01-10 19:48:44 +01:00
|
|
|
Deleter deleter() {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Deleter(this);
|
|
|
|
}
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
std::vector<unique_ptr<Raw>> allocated_;
|
2018-12-31 20:04:05 +01:00
|
|
|
MpscLinkQueue<Node> free_queue_;
|
|
|
|
typename MpscLinkQueue<Node>::Reader free_queue_reader_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|