mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +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
51 lines
1.5 KiB
C++
51 lines
1.5 KiB
C++
//===--- ThreadSafeRefCounted.h - Thread-safe Refcounting Base --*- C++ -*-===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See http://swift.org/LICENSE.txt for license information
|
|
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_BASIC_THREADSAFEREFCOUNTED_H
|
|
#define SWIFT_BASIC_THREADSAFEREFCOUNTED_H
|
|
|
|
#include <atomic>
|
|
#include <cassert>
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
|
|
|
namespace swift {
|
|
|
|
/// A class that has the same function as \c ThreadSafeRefCountedBase, but with
|
|
/// a virtual destructor.
|
|
///
|
|
/// Should be used instead of \c ThreadSafeRefCountedBase for classes that
|
|
/// already have virtual methods to enforce dynamic allocation via 'new'.
|
|
/// FIXME: This should eventually move to llvm.
|
|
class ThreadSafeRefCountedBaseVPTR {
|
|
mutable std::atomic<unsigned> ref_cnt;
|
|
virtual void anchor();
|
|
|
|
protected:
|
|
ThreadSafeRefCountedBaseVPTR() : ref_cnt(0) {}
|
|
virtual ~ThreadSafeRefCountedBaseVPTR() {}
|
|
|
|
public:
|
|
void Retain() const {
|
|
ref_cnt += 1;
|
|
}
|
|
|
|
void Release() const {
|
|
int refCount = static_cast<int>(--ref_cnt);
|
|
assert(refCount >= 0 && "Reference count was already zero.");
|
|
if (refCount == 0) delete this;
|
|
}
|
|
};
|
|
|
|
} // end namespace swift
|
|
|
|
#endif // SWIFT_BASIC_THREADSAFEREFCOUNTED_H
|