summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeffrey Yasskin <jyasskin@google.com>2009-12-17 19:55:06 +0000
committerJeffrey Yasskin <jyasskin@google.com>2009-12-17 19:55:06 +0000
commitad715f86c90b06cc4ab9e1336d1bc3bf13ecb16d (patch)
tree4a10bbf7f6939ab35b86d00d5b25e54d669aabf4
parentaf15ffb9127dff185b74ff8b37a9d570ca547c61 (diff)
downloadllvm-ad715f86c90b06cc4ab9e1336d1bc3bf13ecb16d.tar.gz
llvm-ad715f86c90b06cc4ab9e1336d1bc3bf13ecb16d.tar.bz2
llvm-ad715f86c90b06cc4ab9e1336d1bc3bf13ecb16d.tar.xz
This fixes a memory leak in OpaqueType found by Google's internal heapchecker.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@91611 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/DerivedTypes.h4
-rw-r--r--lib/VMCore/LLVMContextImpl.h11
-rw-r--r--lib/VMCore/Type.cpp17
-rw-r--r--unittests/VMCore/DerivedTypesTest.cpp31
4 files changed, 60 insertions, 3 deletions
diff --git a/include/llvm/DerivedTypes.h b/include/llvm/DerivedTypes.h
index fb51430b48..c22060852a 100644
--- a/include/llvm/DerivedTypes.h
+++ b/include/llvm/DerivedTypes.h
@@ -502,9 +502,7 @@ class OpaqueType : public DerivedType {
public:
/// OpaqueType::get - Static factory method for the OpaqueType class...
///
- static OpaqueType *get(LLVMContext &C) {
- return new OpaqueType(C); // All opaque types are distinct
- }
+ static OpaqueType *get(LLVMContext &C);
// Implement support for type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const OpaqueType *) { return true; }
diff --git a/lib/VMCore/LLVMContextImpl.h b/lib/VMCore/LLVMContextImpl.h
index 8a2378ec7c..2ea2d5e910 100644
--- a/lib/VMCore/LLVMContextImpl.h
+++ b/lib/VMCore/LLVMContextImpl.h
@@ -27,6 +27,7 @@
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/FoldingSet.h"
+#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringMap.h"
#include <vector>
@@ -159,6 +160,11 @@ public:
TypeMap<StructValType, StructType> StructTypes;
TypeMap<IntegerValType, IntegerType> IntegerTypes;
+ // Opaque types are not structurally uniqued, so don't use TypeMap.
+ typedef SmallPtrSet<const OpaqueType*, 8> OpaqueTypesTy;
+ OpaqueTypesTy OpaqueTypes;
+
+
/// ValueHandles - This map keeps track of all of the value handles that are
/// watching a Value*. The Value::HasValueHandle bit is used to know
// whether or not a value has an entry in this map.
@@ -201,6 +207,11 @@ public:
delete I->second;
}
MDNodeSet.clear();
+ for (OpaqueTypesTy::iterator I = OpaqueTypes.begin(), E = OpaqueTypes.end();
+ I != E; ++I) {
+ (*I)->AbstractTypeUsers.clear();
+ delete *I;
+ }
}
};
diff --git a/lib/VMCore/Type.cpp b/lib/VMCore/Type.cpp
index 739c463d91..310d0e3a29 100644
--- a/lib/VMCore/Type.cpp
+++ b/lib/VMCore/Type.cpp
@@ -79,6 +79,9 @@ void Type::destroy() const {
operator delete(const_cast<Type *>(this));
return;
+ } else if (const OpaqueType *opaque_this = dyn_cast<OpaqueType>(this)) {
+ LLVMContextImpl *pImpl = this->getContext().pImpl;
+ pImpl->OpaqueTypes.erase(opaque_this);
}
// For all the other type subclasses, there is either no contained types or
@@ -955,6 +958,20 @@ bool PointerType::isValidElementType(const Type *ElemTy) {
//===----------------------------------------------------------------------===//
+// Opaque Type Factory...
+//
+
+OpaqueType *OpaqueType::get(LLVMContext &C) {
+ OpaqueType *OT = new OpaqueType(C); // All opaque types are distinct
+
+ LLVMContextImpl *pImpl = C.pImpl;
+ pImpl->OpaqueTypes.insert(OT);
+ return OT;
+}
+
+
+
+//===----------------------------------------------------------------------===//
// Derived Type Refinement Functions
//===----------------------------------------------------------------------===//
diff --git a/unittests/VMCore/DerivedTypesTest.cpp b/unittests/VMCore/DerivedTypesTest.cpp
new file mode 100644
index 0000000000..11b4dffb5f
--- /dev/null
+++ b/unittests/VMCore/DerivedTypesTest.cpp
@@ -0,0 +1,31 @@
+//===- llvm/unittest/VMCore/DerivedTypesTest.cpp - Types unit tests -------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+#include "../lib/VMCore/LLVMContextImpl.h"
+#include "llvm/Type.h"
+#include "llvm/DerivedTypes.h"
+#include "llvm/LLVMContext.h"
+using namespace llvm;
+
+namespace {
+
+TEST(OpaqueTypeTest, RegisterWithContext) {
+ LLVMContext C;
+ LLVMContextImpl *pImpl = C.pImpl;
+
+ EXPECT_EQ(0u, pImpl->OpaqueTypes.size());
+ {
+ PATypeHolder Type = OpaqueType::get(C);
+ EXPECT_EQ(1u, pImpl->OpaqueTypes.size());
+ }
+ EXPECT_EQ(0u, pImpl->OpaqueTypes.size());
+}
+
+} // namespace