[llvm-commits] CVS: llvm/lib/Target/TargetData.cpp

Chris Lattner sabre at nondot.org
Sat Feb 10 11:43:36 PST 2007



Changes in directory llvm/lib/Target:

TargetData.cpp updated: 1.86 -> 1.87
---
Log message:

Use ManagedStatic to manage LayoutInfo, instead of rolling our own.


---
Diffs of the changes:  (+24 -27)

 TargetData.cpp |   51 ++++++++++++++++++++++++---------------------------
 1 files changed, 24 insertions(+), 27 deletions(-)


Index: llvm/lib/Target/TargetData.cpp
diff -u llvm/lib/Target/TargetData.cpp:1.86 llvm/lib/Target/TargetData.cpp:1.87
--- llvm/lib/Target/TargetData.cpp:1.86	Sat Feb 10 13:33:15 2007
+++ llvm/lib/Target/TargetData.cpp	Sat Feb 10 13:43:18 2007
@@ -22,6 +22,7 @@
 #include "llvm/Constants.h"
 #include "llvm/Support/GetElementPtrTypeIterator.h"
 #include "llvm/Support/MathExtras.h"
+#include "llvm/Support/ManagedStatic.h"
 #include "llvm/ADT/StringExtras.h"
 #include <algorithm>
 #include <cstdlib>
@@ -201,25 +202,23 @@
   init(M->getDataLayout());
 }
 
-/// Layouts - The lazy cache of structure layout information maintained by
+/// LayoutInfo - The lazy cache of structure layout information maintained by
 /// TargetData.
 ///
-static std::map<std::pair<const TargetData*,const StructType*>,
-                StructLayout> *Layouts = 0;
+typedef std::pair<const TargetData*,const StructType*> LayoutKey;
+static ManagedStatic<std::map<LayoutKey, StructLayout> > LayoutInfo;
 
 
 TargetData::~TargetData() {
-  if (Layouts) {
+  if (LayoutInfo.isConstructed()) {
     // Remove any layouts for this TD.
-    std::map<std::pair<const TargetData*,
-      const StructType*>, StructLayout>::iterator
-      I = Layouts->lower_bound(std::make_pair(this, (const StructType*)0));
-    while (I != Layouts->end() && I->first.first == this)
-      Layouts->erase(I++);
-    if (Layouts->empty()) {
-      delete Layouts;
-      Layouts = 0;
-    }
+    std::map<LayoutKey, StructLayout> &TheMap = *LayoutInfo;
+    std::map<LayoutKey, StructLayout>::iterator
+      I = TheMap.lower_bound(LayoutKey(this, (const StructType*)0));
+    
+    for (std::map<LayoutKey, StructLayout>::iterator E = TheMap.end();
+         I != E && I->first.first == this; )
+      TheMap.erase(I++);
   }
 }
 
@@ -252,17 +251,15 @@
 }
 
 const StructLayout *TargetData::getStructLayout(const StructType *Ty) const {
-  if (Layouts == 0)
-    Layouts = new std::map<std::pair<const TargetData*,const StructType*>,
-                           StructLayout>();
-  std::map<std::pair<const TargetData*,const StructType*>,
-                     StructLayout>::iterator
-    I = Layouts->lower_bound(std::make_pair(this, Ty));
-  if (I != Layouts->end() && I->first.first == this && I->first.second == Ty)
+  std::map<LayoutKey, StructLayout> &TheMap = *LayoutInfo;
+  
+  std::map<LayoutKey, StructLayout>::iterator
+    I = TheMap.lower_bound(LayoutKey(this, Ty));
+  if (I != TheMap.end() && I->first.first == this && I->first.second == Ty)
     return &I->second;
   else {
-    return &Layouts->insert(I, std::make_pair(std::make_pair(this, Ty),
-                                              StructLayout(Ty, *this)))->second;
+    return &TheMap.insert(I, std::make_pair(LayoutKey(this, Ty),
+                                            StructLayout(Ty, *this)))->second;
   }
 }
 
@@ -271,12 +268,12 @@
 /// removed, this method must be called whenever a StructType is removed to
 /// avoid a dangling pointer in this cache.
 void TargetData::InvalidateStructLayoutInfo(const StructType *Ty) const {
-  if (!Layouts) return;  // No cache.
+  if (!LayoutInfo.isConstructed()) return;  // No cache.
 
-  std::map<std::pair<const TargetData*,const StructType*>,
-           StructLayout>::iterator I = Layouts->find(std::make_pair(this, Ty));
-  if (I != Layouts->end())
-    Layouts->erase(I);
+  std::map<LayoutKey, StructLayout>::iterator I = 
+    LayoutInfo->find(std::make_pair(this, Ty));
+  if (I != LayoutInfo->end())
+    LayoutInfo->erase(I);
 }
 
 






More information about the llvm-commits mailing list