当前位置:网站首页>Smart pointer implementation conjecture
Smart pointer implementation conjecture
2022-07-30 12:53:00 【Jun Meng Ru Yan Brian】
Automatic memory management envisioned by strong and weak references,Only the part that manages memory is implemented,仍有许多C++11function could not be realized.
SharedPtr => std::shared_ptr
SentinelPtr => std::weak_ptr (Why take it this way,因为SentinelPtr Behaving more likeSharedPtr 的观察窗口)
首先,实现了SharedPtr, 这个很好理解.Reference counts whenever copy behavior occurs+1,Because the counter is shared among multiple objects,So the counter must also be a pointer.
难点在于,引入SentinelPtr后,同时保持SharedPtr Conditional control of behavior.
Comparatively speaking,We can think of strong references as counters of values,Then a weak pointer is equivalent to a counter of strong references.
欠缺:
(-) 功能还没完全实现,Just implemented the part involving counting.
(-) Counters are not thread safe
template<typename T>
class SentinelPtr;
template <typename T>
class SharedPtr {
size_t* ref_counter_ = nullptr;// 强引用计数器
T* val_ = nullptr; // 值引用
size_t * soft_ref_counter_ = nullptr; // 弱引用计数器
// template <T>
friend class SentinelPtr<T>;
public:
SharedPtr operator=(const SharedPtr& rhs) = delete;
SharedPtr():ref_counter_(nullptr), val_(nullptr), soft_ref_counter_(nullptr){
}
SharedPtr(T val) :val_(new T(val)), ref_counter_(new size_t(1)), soft_ref_counter_(new size_t(1)) {
}
SharedPtr(T* val_pointer) {
if (val_pointer != nullptr) {
val_ = val_pointer;
ref_counter_ = new size_t(1);
soft_ref_counter_ = new size_t(1);
}
else {
val_ = nullptr;
ref_counter_ = nullptr;
soft_ref_counter_ = nullptr;
}
}
SharedPtr(const SharedPtr<T>& rhs) {
// 歧义: The reference count itself must be incremented,constThe expression is a bit strange
if(val_ != rhs.val_) try_release();
soft_ref_counter_ = rhs.soft_ref_counter_;
ref_counter_ = rhs.ref_counter_;
val_ = rhs.val_;
// rhsConstructed by default
if (val_ == nullptr) return;
assert(soft_ref_counter_ != nullptr);
assert(ref_counter_ != nullptr);
(*soft_ref_counter_)++;
(*ref_counter_)++;
}
~SharedPtr() {
if (val_ == nullptr) return;
assert(ref_counter_ != nullptr);
assert(val_ != nullptr);
try_release();
soft_ref_counter_ = nullptr;
ref_counter_ = nullptr;
val_ = nullptr;
}
public:
size_t use_count()const {
if (ref_counter_) return *ref_counter_;
return 0;
}
private:
// 释放了val就return true
void try_release() {
// An object possibly constructed from a null pointer
if (!soft_ref_counter_) return;
// 弱引用计数, Pay later
(*soft_ref_counter_)--;
assert(ref_counter_ != nullptr);
(*ref_counter_)--;
// 强引用计数为0, 释放值
if (*ref_counter_ == 0) {
delete val_;
val_ = nullptr;
}
// 弱引用计数为0, Free all counters
if ((*soft_ref_counter_) == 0) {
delete ref_counter_;
delete soft_ref_counter_;
ref_counter_ = nullptr;
soft_ref_counter_ = nullptr;
}
}
};
template<typename T>
class SentinelPtr {
size_t* ref_counter_; // 强引用计数器
size_t* soft_ref_counter_; // 弱引用计数器(Equivalent to the reference count of a strong referrer)
T* val_; // 提升值
public:
SharedPtr<T> update() {
if (!soft_ref_counter_ || !ref_counter_) return SharedPtr<T>;
SharedPtr<T> ex;
ex.ref_counter_ = ref_counter_;
ex.soft_ref_counter_ = soft_ref_counter_;
ex.val_ = val_;
(*soft_ref_counter_)++;
(*ref_counter_)++;
return ex;
}
SentinelPtr(SharedPtr<T> sha_ptr) {
ref_counter_ = sha_ptr.ref_counter_;
soft_ref_counter_ = sha_ptr.soft_ref_counter_;
val_ = sha_ptr.val_;
if (soft_ref_counter_) {
assert(ref_counter_ != nullptr);
(*soft_ref_counter_)++;
}
else {
assert(ref_counter_ == nullptr);
assert(soft_ref_counter_ == nullptr);
assert(val_ == nullptr);
}
}
SentinelPtr(const SentinelPtr<T>& st_ptr) {
if(soft_ref_counter_ != st_ptr)
try_release();
ref_counter_ = st_ptr.ref_counter_;
soft_ref_counter_ = st_ptr.soft_ref_counter_;
val_ = st_ptr.val_;
if (soft_ref_counter_) {
assert(ref_counter_ != nullptr);
(*soft_ref_counter_)++;
}
else {
assert(ref_counter_ == nullptr);
assert(soft_ref_counter_ == nullptr);
assert(val_ == nullptr);
}
}
~SentinelPtr() {
try_release();
ref_counter_ = nullptr;
val_ = nullptr;
soft_ref_counter_ = nullptr;
}
/* size_t use_count() { if (!soft_ref_counter_ || !ref_counter_) return 0; return (*ref_counter_); } */
private:
void try_release() {
if (!soft_ref_counter_) return;
assert(ref_counter_ != nullptr);
(*soft_ref_counter_)--;
if (*soft_ref_counter_ == 0) {
delete soft_ref_counter_;
delete ref_counter_;
soft_ref_counter_ = nullptr;
ref_counter_ = nullptr;
}
}
};
int main()
{
// default ctor
{
SharedPtr<int> emp_ptr;
}
// ctor(val)
{
SharedPtr<int> ptr(4);
assert(ptr.use_count() == 1);
}
// ctor(valpointer)
{
SharedPtr<int> b(new int(4));
assert(b.use_count() == 1);
}
// dctr
{
SharedPtr<int> outer(4);
{
SharedPtr<int> inner(outer);
assert(outer.use_count() == inner.use_count());
assert(inner.use_count() == 2);
}
assert(outer.use_count() == 1);
}
{
SharedPtr<int> outer(4);
SentinelPtr<int> p = outer;
assert(outer.use_count() == 1);
}
return 0;
}
边栏推荐
- CMake库搜索函数居然不搜索LD_LIBRARY_PATH
- 为什么说Prometheus是足以取代Zabbix的监控神器?
- New:WebKitX ActiveX :::Crack
- 多表联查的学习
- saltstack学习3模块
- nodeJs--fs模块
- Analysis of AI recognition technology and application scenarios of TSINGSEE intelligent video analysis gateway
- datax enables hana support and dolphinscheduler enables datax tasks
- [BJDCTF2020]Cookie is so stable-1|SSTI注入
- 维护数千规模MySQL实例,数据库灾备体系构建指南
猜你喜欢
随机推荐
亚洲高校首现KDD博士论文奖:清华裘捷中获Runner Up奖,WINNER奖也是位华人
[BJDCTF2020]Cookie is so stable-1|SSTI注入
湖仓一体电商项目(二):项目使用技术及版本和基础环境准备
解码Redis最易被忽视的CPU和内存占用高问题
Anaconda\Scripts\pip-script.py is not present ? 解决方案
关于香港高防IP需要关注的几个问题
重建丢失的数据
【CVA估值训练营】如何快速读懂上市公司年报——第五讲
基于反步积分滑模摩擦补偿的光电伺服转台控制
即时通讯-改变社交与工作状态的新型软件
电流电压采集模块DAM-6160
DOM常用方法以及项目
Based on MySQL database, Redis cache, MQ message middleware, ES high availability scheme of search engine parsing
并行化快速排序设想
【河北工业大学】考研初试复试资料分享
Why is Prometheus a monitoring artifact sufficient to replace Zabbix?
基于柔性人机接口的人机协调运动控制方法
开源出来的fuse版pfs文件系统主要就是解决缓存问题吧。nfs挂载参数带sync规避缓存问题是不是
什么是私有云?您应该知道的 6 个优势
Vivado安装后添加器件库