mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
C++ atomic's fetch_sub returns the previous value, where we want to check the new value. This was causing massive memory leaks in SourceKit. For ThreadSafeRefCountedBase, just switch to the one in LLVM that's already correct. We should move the VPTR one to LLVM as well and then we can get rid of this header. rdar://problem/27358273
52 lines
1.3 KiB
C++
52 lines
1.3 KiB
C++
#include "swift/Basic/ThreadSafeRefCounted.h"
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
|
#include "gtest/gtest.h"
|
|
|
|
using llvm::IntrusiveRefCntPtr;
|
|
|
|
struct TestRelease : llvm::ThreadSafeRefCountedBase<TestRelease> {
|
|
bool &destroy;
|
|
TestRelease(bool &destroy) : destroy(destroy) {}
|
|
~TestRelease() { destroy = true; }
|
|
};
|
|
|
|
TEST(ThreadSafeRefCountedBase, ReleaseSimple) {
|
|
bool destroyed = false;
|
|
{
|
|
IntrusiveRefCntPtr<TestRelease> ref = new TestRelease(destroyed);
|
|
}
|
|
EXPECT_TRUE(destroyed);
|
|
}
|
|
TEST(ThreadSafeRefCountedBase, Release) {
|
|
bool destroyed = false;
|
|
{
|
|
IntrusiveRefCntPtr<TestRelease> ref = new TestRelease(destroyed);
|
|
ref->Retain();
|
|
ref->Release();
|
|
}
|
|
EXPECT_TRUE(destroyed);
|
|
}
|
|
|
|
struct TestReleaseVPTR : swift::ThreadSafeRefCountedBaseVPTR {
|
|
bool &destroy;
|
|
TestReleaseVPTR(bool &destroy) : destroy(destroy) {}
|
|
virtual ~TestReleaseVPTR() { destroy = true; }
|
|
};
|
|
|
|
TEST(ThreadSafeRefCountedBaseVPTR, ReleaseSimple) {
|
|
bool destroyed = false;
|
|
{
|
|
IntrusiveRefCntPtr<TestReleaseVPTR> ref = new TestReleaseVPTR(destroyed);
|
|
}
|
|
EXPECT_TRUE(destroyed);
|
|
}
|
|
TEST(ThreadSafeRefCountedBaseVPTR, Release) {
|
|
bool destroyed = false;
|
|
{
|
|
IntrusiveRefCntPtr<TestReleaseVPTR> ref = new TestReleaseVPTR(destroyed);
|
|
ref->Retain();
|
|
ref->Release();
|
|
}
|
|
EXPECT_TRUE(destroyed);
|
|
}
|