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 unsigned newCount = sys::AtomicIncrement(&IDCounter);
73 (*IDMap)[Name] = newCount-1; // Add a new element
74 return AnnotationID(newCount-1);
76 return AnnotationID(I->second);
78 return AnnotationID(I->second);
81 // getID - Name -> ID + registration of a factory function for demand driven
82 // annotation support.
83 AnnotationID AnnotationManager::getID(const char *Name, Factory Fact,
85 AnnotationID Result(getID(Name));
86 registerAnnotationFactory(Result, Fact, Data);
90 // getName - This function is especially slow, but that's okay because it should
91 // only be used for debugging.
93 const char *AnnotationManager::getName(AnnotationID ID) { // ID -> Name
94 sys::SmartScopedReader<true> Reader(&*AnnotationsLock);
95 IDMapType &TheMap = *IDMap;
96 for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
97 assert(I != TheMap.end() && "Annotation ID is unknown!");
98 if (I->second == ID.ID) return I->first;
102 // registerAnnotationFactory - This method is used to register a callback
103 // function used to create an annotation on demand if it is needed by the
104 // Annotable::findOrCreateAnnotation method.
106 void AnnotationManager::registerAnnotationFactory(AnnotationID ID, AnnFactory F,
109 sys::SmartScopedWriter<true> Writer(&*AnnotationsLock);
110 getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
112 eraseFromFactMap(ID.ID);
116 // createAnnotation - Create an annotation of the specified ID for the
117 // specified object, using a register annotation creation function.
119 Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
120 const Annotable *Obj) {
121 AnnotationsLock->reader_acquire();
122 FactMapType::iterator I = getFactMap().find(ID.ID);
123 if (I == getFactMap().end()) {
124 AnnotationsLock->reader_release();
128 AnnotationsLock->reader_release();
129 return I->second.first(ID, Obj, I->second.second);