Skip to content

Commit

Permalink
Updates for intrusive_ptr refcounting
Browse files Browse the repository at this point in the history
- Initialize the refcounts with 1
- Use memory orderings that match the implementation of shared_ptr refcounting in libc++/Windows STL
  • Loading branch information
ajtribick committed Mar 27, 2024
1 parent fc0878f commit f656a12
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/celengine/shadermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,17 @@ class Sh_ExpressionContents
friend void
intrusive_ptr_add_ref(Sh_ExpressionContents* p)
{
++p->m_refCount;
++(p->m_refCount);
}

friend void
intrusive_ptr_release(Sh_ExpressionContents* p)
{
if (--p->m_refCount == 0)
if (--(p->m_refCount) == 0)
delete p;
}

std::uint32_t m_refCount{0};
std::uint32_t m_refCount{ 1 };
};


Expand Down Expand Up @@ -323,7 +323,7 @@ class Sh_Expression
{}

Sh_Expression(float f) :
m_contents(new Sh_ConstantExpression(f))
m_contents(new Sh_ConstantExpression(f), false)
{}

~Sh_Expression() = default;
Expand Down Expand Up @@ -354,7 +354,7 @@ class Sh_Expression
template<typename T, typename... Args>
Sh_Expression makeExpression(Args&&... args)
{
return Sh_Expression(boost::intrusive_ptr<T>(new T(std::forward<Args>(args)...)));
return Sh_Expression(boost::intrusive_ptr(new T(std::forward<Args>(args)...), false));
}


Expand Down
2 changes: 1 addition & 1 deletion src/celengine/star.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ StarDetails::StarDetails()
boost::intrusive_ptr<StarDetails>
StarDetails::create()
{
return boost::intrusive_ptr<StarDetails>(new StarDetails);
return boost::intrusive_ptr(new StarDetails, false);
}

boost::intrusive_ptr<StarDetails>
Expand Down
6 changes: 3 additions & 3 deletions src/celengine/star.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,17 @@ class StarDetails
friend void
intrusive_ptr_add_ref(StarDetails* p)
{
p->refCount.fetch_add(1);
p->refCount.fetch_add(1, std::memory_order_relaxed);
}

friend void
intrusive_ptr_release(StarDetails* p)
{
if (p->refCount.fetch_sub(1) == 1)
if (p->refCount.fetch_sub(1, std::memory_order_acq_rel) == 1)
delete p;
}

std::atomic_uint32_t refCount{ 0 };
std::atomic_uint32_t refCount{ 1 };

float radius{ 0.0f };
float temperature{ 0.0f };
Expand Down

0 comments on commit f656a12

Please sign in to comment.