当前位置:网站首页>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;
}
边栏推荐
- 漫谈金丝雀部署(Canary Deployment)
- saltstack学习2grains&pillar
- 多表联查的学习
- 句柄与指针的简单理解
- 【Kaggle:UW-Madison GI Tract Image Segmentation】肠胃分割比赛:赛后复盘+数据再理解
- OpenHarmony环境搭建报错: ImportError: cannot import name ‘VERSION‘ from ‘hb.__main__‘
- Decoding Redis' most overlooked high CPU and memory usage issues
- 【语音识别】基于GMM-HMM的语音识别系统
- 【记一个kaggle划水比赛】PetFinder.my - Pawpularity Contest 宠物预测
- [PostgreSQL] - Storage structure and cache shared_buffers
猜你喜欢

历时两月,终拿字节跳动offer,算法面试题分享「带答案」

常见的云计算安全问题以及如何解决

概率论的学习整理2:如何对随机实验的对象:“事件” 进行计数呢? 四种计数方法,不只是排列组合

OneNote如何修改已有的笔记本为默认的快速笔记?

【Kaggle:UW-Madison GI Tract Image Segmentation】肠胃分割比赛:赛后复盘+数据再理解

云主机上的MongoDB被威胁,开启AUTH认证

unity对象池(学习)

OpenHarmony环境搭建报错: ImportError: cannot import name ‘VERSION‘ from ‘hb.__main__‘

使用百度EasyDL实现明厨亮灶厨师帽识别

别被隐私计算表象骗了 | 量子位智库报告(附下载)
随机推荐
湖仓一体电商项目(一):项目背景和架构介绍
物理服务器与虚拟机:主要区别和相似之处
Mysql 批量插入事务唯一键重复处理
概率论的学习整理4:全概率公式
Mysql索引结构
MySQL中的select,from, join, on where groupby等执行顺序
OneNote如何修改已有的笔记本为默认的快速笔记?
unity初学6——简易的UI制作(血条制作)和音频加入以及NPC的对话气泡(2d)
无人艇轨迹跟踪的预设性能抗扰控制研究
概率论的学习整理2:如何对随机实验的对象:“事件” 进行计数呢? 四种计数方法,不只是排列组合
大手笔!两所“双一流”大学,获75亿元重点支持!
使用百度EasyDL实现明厨亮灶厨师帽识别
从“校园贷”到“直播带货”,追风少年罗敏一直行走在风口浪尖
最基础01/完全背包
[SCTF2019]Flag Shop
AlphaFold预测了几乎所有已知蛋白质!涵盖100万物种2.14亿结构,数据集开放免费用...
I built another wheel: GrpcGateway
Apifox generates interface documentation tutorial and operation steps
[PostgreSQL] - Storage structure and cache shared_buffers
Analysis of AI recognition technology and application scenarios of TSINGSEE intelligent video analysis gateway