当前位置:网站首页>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;
}
边栏推荐
- Vivado安装后添加器件库
- dolphinscheduler简单任务定义及复杂的跨节点传参
- WinForm枚举容器中的控件,实现控件统一事件处理机制
- 使用百度EasyDL实现明厨亮灶厨师帽识别
- Mysql batch insert transaction unique key repeated processing
- Beijing, Shanghai and Guangzhou offline events丨The most unmissable technology gatherings at the end of the year are all gathered
- EXCEL解决问题:如何查找目标区域,是否包含指定字符串?
- Scala基础:数组(Array)、映射(Map)、元组(Tuple)、集合(List)
- 别被隐私计算表象骗了 | 量子位智库报告(附下载)
- 作业7.29 目录相关函数和文件属性相关函数
猜你喜欢
随机推荐
[BJDCTF2020]Cookie is so stable-1|SSTI injection
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
Another blast!Ali's popular MySQL advanced collection is open source, reaching P7
京东二面痛遭中间件虐杀,30天学透这套中间件小册,挺进阿里
力扣——15. 三数之和
初级永磁直线电机双动子电流镜像容错控制
企业如何成功完成云迁移?
Rust 从入门到精通02-安装
Mysql 批量插入事务唯一键重复处理
最基础01/完全背包
AlphaFold预测了几乎所有已知蛋白质!涵盖100万物种2.14亿结构,数据集开放免费用...
Js - 内置对象
大手笔!两所“双一流”大学,获75亿元重点支持!
即时通讯-改变社交与工作状态的新型软件
Analysis of AI recognition technology and application scenarios of TSINGSEE intelligent video analysis gateway
手慢无!阿里亿级流量高并发系统设计核心原理全彩笔记现实开源
湖仓一体电商项目(二):项目使用技术及版本和基础环境准备
打破原则引入SQL,MongoDB到底想要干啥???
【Kaggle比赛常用trick】K折交叉验证、TTA
13-GuliMall 基础篇总结









