Introduce needsCleanup() for APFloat and APInt.
This is needed in clang so one can check if the object needs the
destructor called after its memory was freed. This is useful when
creating many APInt/APFloat objects with placement new, where the
overhead of tracking the pointers for cleanup is significant.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@183100 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/llvm/ADT/APFloat.h b/include/llvm/ADT/APFloat.h
index 64e56a0..33f997e 100644
--- a/include/llvm/ADT/APFloat.h
+++ b/include/llvm/ADT/APFloat.h
@@ -201,6 +201,9 @@
/// @}
+ /// \brief Returns whether this instance allocated memory.
+ bool needsCleanup() const { return partCount() > 1; }
+
/// \name Convenience "constructors"
/// @{
diff --git a/include/llvm/ADT/APInt.h b/include/llvm/ADT/APInt.h
index a9df403..e5797b8 100644
--- a/include/llvm/ADT/APInt.h
+++ b/include/llvm/ADT/APInt.h
@@ -293,7 +293,7 @@
/// \brief Destructor.
~APInt() {
- if (!isSingleWord())
+ if (needsCleanup())
delete[] pVal;
}
@@ -303,6 +303,9 @@
/// method Read).
explicit APInt() : BitWidth(1) {}
+ /// \brief Returns whether this instance allocated memory.
+ bool needsCleanup() const { return !isSingleWord(); }
+
/// Used to insert APInt objects, or objects that contain APInt objects, into
/// FoldingSets.
void Profile(FoldingSetNodeID &id) const;
diff --git a/lib/Support/APFloat.cpp b/lib/Support/APFloat.cpp
index 16586fb..2ad66c5 100644
--- a/lib/Support/APFloat.cpp
+++ b/lib/Support/APFloat.cpp
@@ -580,7 +580,7 @@
void
APFloat::freeSignificand()
{
- if (partCount() > 1)
+ if (needsCleanup())
delete [] significand.parts;
}