From 7cc4f07bdcbfda2a8bb65a41adfef84857f1165a Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Fri, 24 Apr 2015 05:38:48 +0000 Subject: [PATCH] [TableGen] Fix all remaining memory leaks of Init and RecTy objects. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@235696 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/TableGen/Record.h | 7 +++- lib/TableGen/Record.cpp | 67 +++++++++++++++++----------------- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/include/llvm/TableGen/Record.h b/include/llvm/TableGen/Record.h index 3c5723155eb..fb5a6573526 100644 --- a/include/llvm/TableGen/Record.h +++ b/include/llvm/TableGen/Record.h @@ -81,7 +81,7 @@ public: private: RecTyKind Kind; - ListRecTy *ListTy; + std::unique_ptr ListTy; virtual void anchor(); public: @@ -566,6 +566,11 @@ class TypedInit : public Init { protected: explicit TypedInit(InitKind K, RecTy *T) : Init(K), Ty(T) {} + ~TypedInit() { + // If this is a DefInit we need to delete the RecordRecTy. + if (getKind() == IK_DefInit) + delete Ty; + } public: static bool classof(const Init *I) { diff --git a/lib/TableGen/Record.cpp b/lib/TableGen/Record.cpp index 5698de02d68..560e7c991d9 100644 --- a/lib/TableGen/Record.cpp +++ b/lib/TableGen/Record.cpp @@ -91,8 +91,8 @@ void RecTy::dump() const { print(errs()); } ListRecTy *RecTy::getListTy() { if (!ListTy) - ListTy = new ListRecTy(this); - return ListTy; + ListTy.reset(new ListRecTy(this)); + return ListTy.get(); } bool RecTy::baseClassOf(const RecTy *RHS) const{ @@ -141,13 +141,13 @@ bool BitRecTy::baseClassOf(const RecTy *RHS) const{ } BitsRecTy *BitsRecTy::get(unsigned Sz) { - static std::vector Shared; + static std::vector> Shared; if (Sz >= Shared.size()) Shared.resize(Sz + 1); - BitsRecTy *&Ty = Shared[Sz]; + std::unique_ptr &Ty = Shared[Sz]; if (!Ty) - Ty = new BitsRecTy(Sz); - return Ty; + Ty.reset(new BitsRecTy(Sz)); + return Ty.get(); } std::string BitsRecTy::getAsString() const { @@ -451,8 +451,8 @@ ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef Range) { } BitsInit *BitsInit::get(ArrayRef Range) { - typedef FoldingSet Pool; - static Pool ThePool; + static FoldingSet ThePool; + static std::vector> TheActualPool; FoldingSetNodeID ID; ProfileBitsInit(ID, Range); @@ -463,7 +463,7 @@ BitsInit *BitsInit::get(ArrayRef Range) { BitsInit *I = new BitsInit(Range); ThePool.InsertNode(I, IP); - + TheActualPool.push_back(std::unique_ptr(I)); return I; } @@ -601,8 +601,7 @@ static void ProfileListInit(FoldingSetNodeID &ID, } ListInit *ListInit::get(ArrayRef Range, RecTy *EltTy) { - typedef FoldingSet Pool; - static Pool ThePool; + static FoldingSet ThePool; static std::vector> TheActualPool; FoldingSetNodeID ID; @@ -995,8 +994,7 @@ TernOpInit *TernOpInit::get(TernaryOp opc, Init *lhs, Init * > Key; - typedef DenseMap Pool; - static Pool ThePool; + static DenseMap> ThePool; Key TheKey(std::make_pair(std::make_pair(std::make_pair(std::make_pair(opc, Type), @@ -1004,9 +1002,9 @@ TernOpInit *TernOpInit::get(TernaryOp opc, Init *lhs, mhs), rhs)); - TernOpInit *&I = ThePool[TheKey]; - if (!I) I = new TernOpInit(opc, lhs, mhs, rhs, Type); - return I; + std::unique_ptr &I = ThePool[TheKey]; + if (!I) I.reset(new TernOpInit(opc, lhs, mhs, rhs, Type)); + return I.get(); } static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, @@ -1402,15 +1400,13 @@ Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) const { VarListElementInit *VarListElementInit::get(TypedInit *T, unsigned E) { typedef std::pair Key; - typedef DenseMap Pool; - - static Pool ThePool; + static DenseMap> ThePool; Key TheKey(std::make_pair(T, E)); - VarListElementInit *&I = ThePool[TheKey]; - if (!I) I = new VarListElementInit(T, E); - return I; + std::unique_ptr &I = ThePool[TheKey]; + if (!I) I.reset(new VarListElementInit(T, E)); + return I.get(); } std::string VarListElementInit::getAsString() const { @@ -1440,7 +1436,7 @@ Init *VarListElementInit:: resolveListElementReference(Record &R, if (TypedInit *TInit = dyn_cast(Result)) { Init *Result2 = TInit->resolveListElementReference(R, RV, Elt); if (Result2) return Result2; - return new VarListElementInit(TInit, Elt); + return VarListElementInit::get(TInit, Elt); } return Result; } @@ -1470,14 +1466,13 @@ std::string DefInit::getAsString() const { FieldInit *FieldInit::get(Init *R, const std::string &FN) { typedef std::pair Key; - typedef DenseMap Pool; - static Pool ThePool; + static DenseMap> ThePool; Key TheKey(std::make_pair(R, FN)); - FieldInit *&I = ThePool[TheKey]; - if (!I) I = new FieldInit(R, FN); - return I; + std::unique_ptr &I = ThePool[TheKey]; + if (!I) I.reset(new FieldInit(R, FN)); + return I.get(); } Init *FieldInit::getBit(unsigned Bit) const { @@ -1537,8 +1532,8 @@ DagInit * DagInit::get(Init *V, const std::string &VN, ArrayRef ArgRange, ArrayRef NameRange) { - typedef FoldingSet Pool; - static Pool ThePool; + static FoldingSet ThePool; + static std::vector> TheActualPool; FoldingSetNodeID ID; ProfileDagInit(ID, V, VN, ArgRange, NameRange); @@ -1549,7 +1544,7 @@ DagInit::get(Init *V, const std::string &VN, DagInit *I = new DagInit(V, VN, ArgRange, NameRange); ThePool.InsertNode(I, IP); - + TheActualPool.push_back(std::unique_ptr(I)); return I; } @@ -1661,9 +1656,13 @@ void Record::checkName() { } DefInit *Record::getDefInit() { - if (!TheInit) - TheInit = new DefInit(this, new RecordRecTy(this)); - return TheInit; + static DenseMap> ThePool; + if (TheInit) + return TheInit; + + std::unique_ptr &I = ThePool[this]; + if (!I) I.reset(new DefInit(this, new RecordRecTy(this))); + return I.get(); } const std::string &Record::getName() const { -- 2.34.1