1 //===-- Annotation.cpp - Implement the Annotation Classes -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the AnnotationManager class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Annotation.h"
15 #include "llvm/Support/ManagedStatic.h"
16 #include "llvm/System/RWMutex.h"
21 Annotation::~Annotation() {} // Designed to be subclassed
23 Annotable::~Annotable() { // Virtual because it's designed to be subclassed...
24 Annotation *A = AnnotationList;
26 Annotation *Next = A->getNext();
35 bool operator()(const char *a, const char *b) const {
36 return strcmp(a, b) < 0;
41 typedef std::map<const char*, unsigned, StrCmp> IDMapType;
42 static unsigned IDCounter = 0; // Unique ID counter
44 // Static member to ensure initialiation on demand.
45 static ManagedStatic<IDMapType> IDMap;
46 static ManagedStatic<sys::SmartRWMutex<true> > AnnotationsLock;
48 // On demand annotation creation support...
49 typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
50 typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
52 static ManagedStatic<FactMapType> TheFactMap;
53 static FactMapType &getFactMap() {
57 static void eraseFromFactMap(unsigned ID) {
58 sys::SmartScopedWriter<true> Writer(&*AnnotationsLock);
59 TheFactMap->erase(ID);
62 AnnotationID AnnotationManager::getID(const char *Name) { // Name -> ID
63 AnnotationsLock->reader_acquire();
64 IDMapType::iterator I = IDMap->find(Name);
65 IDMapType::iterator E = IDMap->end();
66 AnnotationsLock->reader_release();
69 sys::SmartScopedWriter<true> Writer(&*AnnotationsLock);
70 I = IDMap->find(Name);
71 if (I == IDMap->end())
72 (*IDMap)[Name] = IDCounter++; // Add a new element
73 return AnnotationID(IDCounter-1);
75 return AnnotationID(I->second);
78 // getID - Name -> ID + registration of a factory function for demand driven
79 // annotation support.
80 AnnotationID AnnotationManager::getID(const char *Name, Factory Fact,
82 AnnotationID Result(getID(Name));
83 registerAnnotationFactory(Result, Fact, Data);
87 // getName - This function is especially slow, but that's okay because it should
88 // only be used for debugging.
90 const char *AnnotationManager::getName(AnnotationID ID) { // ID -> Name
91 sys::SmartScopedReader<true> Reader(&*AnnotationsLock);
92 IDMapType &TheMap = *IDMap;
93 for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
94 assert(I != TheMap.end() && "Annotation ID is unknown!");
95 if (I->second == ID.ID) return I->first;
99 // registerAnnotationFactory - This method is used to register a callback
100 // function used to create an annotation on demand if it is needed by the
101 // Annotable::findOrCreateAnnotation method.
103 void AnnotationManager::registerAnnotationFactory(AnnotationID ID, AnnFactory F,
106 sys::SmartScopedWriter<true> Writer(&*AnnotationsLock);
107 getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
109 eraseFromFactMap(ID.ID);
113 // createAnnotation - Create an annotation of the specified ID for the
114 // specified object, using a register annotation creation function.
116 Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
117 const Annotable *Obj) {
118 AnnotationsLock->reader_acquire();
119 FactMapType::iterator I = getFactMap().find(ID.ID);
120 if (I == getFactMap().end()) {
121 AnnotationsLock->reader_release();
125 AnnotationsLock->reader_release();
126 return I->second.first(ID, Obj, I->second.second);