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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34154 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2007-02-10 19:43:18 +00:00
parent ddce8d21ea
commit ec6478b8f3

View File

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