1 //===- RelocData.h --------------------------------------------------------===//
2 //
3 //                     The MCLinker Project
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #ifndef MCLD_LD_RELOCDATA_H
10 #define MCLD_LD_RELOCDATA_H
11 
12 #include <mcld/Config/Config.h>
13 #include <mcld/Fragment/Relocation.h>
14 #include <mcld/Support/Allocators.h>
15 #include <mcld/Support/GCFactoryListTraits.h>
16 
17 #include <llvm/ADT/ilist.h>
18 #include <llvm/ADT/ilist_node.h>
19 #include <llvm/Support/DataTypes.h>
20 
21 #include <list>
22 
23 namespace mcld {
24 
25 class LDSection;
26 
27 /** \class RelocData
28  *  \brief RelocData stores Relocation.
29  *
30  *  Since Relocations are created by GCFactory, we use GCFactoryListTraits for the
31  *  RelocationList here to avoid iplist to delete Relocations.
32  */
33 class RelocData
34 {
35 private:
36   friend class Chunk<RelocData, MCLD_SECTIONS_PER_INPUT>;
37 
38   RelocData();
39   explicit RelocData(LDSection &pSection);
40 
41   RelocData(const RelocData &);            // DO NOT IMPLEMENT
42   RelocData& operator=(const RelocData &); // DO NOT IMPLEMENT
43 
44 public:
45   typedef llvm::iplist<Relocation,
46                        GCFactoryListTraits<Relocation> > RelocationListType;
47 
48   typedef RelocationListType::reference reference;
49   typedef RelocationListType::const_reference const_reference;
50 
51   typedef RelocationListType::iterator iterator;
52   typedef RelocationListType::const_iterator const_iterator;
53 
54   typedef RelocationListType::reverse_iterator reverse_iterator;
55   typedef RelocationListType::const_reverse_iterator const_reverse_iterator;
56 
57 public:
58   static RelocData* Create(LDSection& pSection);
59 
60   static void Destroy(RelocData*& pSection);
61 
62   static void Clear();
63 
getSection()64   const LDSection& getSection() const { return *m_pSection; }
getSection()65   LDSection&       getSection()       { return *m_pSection; }
66 
getRelocationList()67   const RelocationListType& getRelocationList() const { return m_Relocations; }
getRelocationList()68   RelocationListType&       getRelocationList()       { return m_Relocations; }
69 
size()70   size_t size() const { return m_Relocations.size(); }
71 
empty()72   bool empty() const { return m_Relocations.empty(); }
73 
74   RelocData& append(Relocation& pRelocation);
75   Relocation& remove(Relocation& pRelocation);
76 
front()77   reference              front ()       { return m_Relocations.front();  }
front()78   const_reference        front () const { return m_Relocations.front();  }
back()79   reference              back  ()       { return m_Relocations.back();   }
back()80   const_reference        back  () const { return m_Relocations.back();   }
81 
begin()82   const_iterator         begin () const { return m_Relocations.begin();  }
begin()83   iterator               begin ()       { return m_Relocations.begin();  }
end()84   const_iterator         end   () const { return m_Relocations.end();    }
end()85   iterator               end   ()       { return m_Relocations.end();    }
rbegin()86   const_reverse_iterator rbegin() const { return m_Relocations.rbegin(); }
rbegin()87   reverse_iterator       rbegin()       { return m_Relocations.rbegin(); }
rend()88   const_reverse_iterator rend  () const { return m_Relocations.rend();   }
rend()89   reverse_iterator       rend  ()       { return m_Relocations.rend();   }
90 
sort(Comparator pComparator)91   template<class Comparator> void sort(Comparator pComparator) {
92     /* FIXME: use llvm::iplist::sort */
93     std::list<Relocation*> relocs;
94     for (iterator it = begin(), ie = end(); it != ie; ++it)
95       relocs.push_back(it);
96     relocs.sort(pComparator);
97     m_Relocations.clear();
98     for (std::list<Relocation*>::iterator it = relocs.begin(),
99       ie = relocs.end(); it != ie; ++it)
100       m_Relocations.push_back(*it);
101   }
102 
103 private:
104   RelocationListType m_Relocations;
105   LDSection* m_pSection;
106 
107 };
108 
109 } // namespace of mcld
110 
111 #endif
112 
113