Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions API/fleece/RefCounted.hh
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,11 @@ namespace fleece {
friend void release(const RefCounted* FL_NULLABLE) noexcept;
friend void assignRef(RefCounted* FL_NULLABLE &dst, RefCounted* FL_NULLABLE src) noexcept;

#if DEBUG
void _retain() const noexcept {_careful_retain();}
void _release() const noexcept {_careful_release();}
#else
ALWAYS_INLINE void _retain() const noexcept { ++_refCount; }
static constexpr int32_t kCarefulInitialRefCount = -6666666;
void _retain() const noexcept;
void _release() const noexcept;
#endif

static constexpr int32_t kCarefulInitialRefCount = -6666666;
void _careful_retain() const noexcept;
void _careful_release() const noexcept;

mutable std::atomic<int32_t> _refCount
#if DEBUG
{kCarefulInitialRefCount};
#else
{0};
#endif
mutable std::atomic<int32_t> _refCount {kCarefulInitialRefCount};
};

template <class T> concept RefCountedType = std::derived_from<T, RefCounted>;
Expand Down
14 changes: 2 additions & 12 deletions Fleece/Support/RefCounted.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@

namespace fleece {

#if !DEBUG
__hot void RefCounted::_release() const noexcept {
if (--_refCount <= 0)
delete this;
}
#endif


__hot void release(const RefCounted *r) noexcept {
if (r) r->_release();
}
Expand Down Expand Up @@ -74,9 +66,7 @@ namespace fleece {
// Store a garbage value to detect use-after-free
int32_t oldRef = _refCount.exchange(-9999999);
if (_usuallyFalse(oldRef != 0)) {
#if DEBUG
if (oldRef != kCarefulInitialRefCount)
#endif
{
// Detect if the destructor is not called from _release, i.e. the object still has
// references. This is probably a direct call to delete, which is illegal.
Expand All @@ -96,7 +86,7 @@ namespace fleece {
// the object.


void RefCounted::_careful_retain() const noexcept {
void RefCounted::_retain() const noexcept {
auto oldRef = _refCount++;

// Special case: the initial retain of a new object that takes it to refCount 1
Expand All @@ -113,7 +103,7 @@ namespace fleece {
}


void RefCounted::_careful_release() const noexcept {
void RefCounted::_release() const noexcept {
auto oldRef = _refCount--;

// If the refCount was 0 we have a bug where another thread is destructing
Expand Down
Loading