llvm.org GIT mirror llvm / 57a73c2
Use IntrusiveRefCntPtr to manage the lifetime of BitCodeAbbrevs. This doesn't change the interface or gives additional safety but removes a ton of retain/release boilerplate. No functionality change. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217778 91177308-0d34-0410-b5e6-96231b3b80d8 Benjamin Kramer 6 years ago
4 changed file(s) with 19 addition(s) and 101 deletion(s). Raw diff Collapse all Expand all
1717 #ifndef LLVM_BITCODE_BITCODES_H
1818 #define LLVM_BITCODE_BITCODES_H
1919
20 #include "llvm/ADT/IntrusiveRefCntPtr.h"
2021 #include "llvm/ADT/SmallVector.h"
2122 #include "llvm/Support/DataTypes.h"
2223 #include "llvm/Support/ErrorHandling.h"
160161 /// BitCodeAbbrev - This class represents an abbreviation record. An
161162 /// abbreviation allows a complex record that has redundancy to be stored in a
162163 /// specialized format instead of the fully-general, fully-vbr, format.
163 class BitCodeAbbrev {
164 class BitCodeAbbrev : public RefCountedBase {
164165 SmallVector OperandList;
165 unsigned char RefCount; // Number of things using this.
166166 ~BitCodeAbbrev() {}
167 friend class RefCountedBase; // Only RefCountedBase is allowed to delete.
168
167169 public:
168 BitCodeAbbrev() : RefCount(1) {}
169
170 void addRef() { ++RefCount; }
171 void dropRef() { if (--RefCount == 0) delete this; }
172
173170 unsigned getNumOperandInfos() const {
174171 return static_cast(OperandList.size());
175172 }
3636 /// These describe abbreviations that all blocks of the specified ID inherit.
3737 struct BlockInfo {
3838 unsigned BlockID;
39 std::vector<BitCodeAbbrev*> Abbrevs;
39 std::vector<IntrusiveRefCntPtr> Abbrevs;
4040 std::string Name;
4141
4242 std::vector > RecordNames;
8484 }
8585
8686 StreamableMemoryObject &getBitcodeBytes() { return *BitcodeBytes; }
87
88 ~BitstreamReader() {
89 // Free the BlockInfoRecords.
90 while (!BlockInfoRecords.empty()) {
91 BlockInfo &Info = BlockInfoRecords.back();
92 // Free blockinfo abbrev info.
93 for (unsigned i = 0, e = static_cast(Info.Abbrevs.size());
94 i != e; ++i)
95 Info.Abbrevs[i]->dropRef();
96 BlockInfoRecords.pop_back();
97 }
98 }
9987
10088 /// CollectBlockInfoNames - This is called by clients that want block/record
10189 /// name information.
207195 unsigned CurCodeSize;
208196
209197 /// CurAbbrevs - Abbrevs installed at in this block.
210 std::vector<BitCodeAbbrev*> CurAbbrevs;
198 std::vector<IntrusiveRefCntPtr> CurAbbrevs;
211199
212200 struct Block {
213201 unsigned PrevCodeSize;
214 std::vector<BitCodeAbbrev*> PrevAbbrevs;
202 std::vector<IntrusiveRefCntPtr> PrevAbbrevs;
215203 explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
216204 };
217205
221209
222210 public:
223211 BitstreamCursor() : BitStream(nullptr), NextChar(0) {}
224 BitstreamCursor(const BitstreamCursor &RHS)
225 : BitStream(nullptr), NextChar(0) {
226 operator=(RHS);
227 }
228212
229213 explicit BitstreamCursor(BitstreamReader &R) : BitStream(&R) {
230214 NextChar = 0;
242226 BitsInCurWord = 0;
243227 CurCodeSize = 2;
244228 }
245
246 ~BitstreamCursor() {
247 freeState();
248 }
249
250 void operator=(const BitstreamCursor &RHS);
251229
252230 void freeState();
253231
528506 void popBlockScope() {
529507 CurCodeSize = BlockScope.back().PrevCodeSize;
530508
531 // Delete abbrevs from popped scope.
532 for (unsigned i = 0, e = static_cast(CurAbbrevs.size());
533 i != e; ++i)
534 CurAbbrevs[i]->dropRef();
535
536 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
509 CurAbbrevs = std::move(BlockScope.back().PrevAbbrevs);
537510 BlockScope.pop_back();
538511 }
539512
554527 const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) {
555528 unsigned AbbrevNo = AbbrevID-bitc::FIRST_APPLICATION_ABBREV;
556529 assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
557 return CurAbbrevs[AbbrevNo];
530 return CurAbbrevs[AbbrevNo].get();
558531 }
559532
560533 /// skipRecord - Read the current record and discard it.
3939 unsigned BlockInfoCurBID;
4040
4141 /// CurAbbrevs - Abbrevs installed at in this block.
42 std::vector<BitCodeAbbrev*> CurAbbrevs;
42 std::vector<IntrusiveRefCntPtr> CurAbbrevs;
4343
4444 struct Block {
4545 unsigned PrevCodeSize;
4646 unsigned StartSizeWord;
47 std::vector<BitCodeAbbrev*> PrevAbbrevs;
47 std::vector<IntrusiveRefCntPtr> PrevAbbrevs;
4848 Block(unsigned PCS, unsigned SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {}
4949 };
5050
5555 /// These describe abbreviations that all blocks of the specified ID inherit.
5656 struct BlockInfo {
5757 unsigned BlockID;
58 std::vector<BitCodeAbbrev*> Abbrevs;
58 std::vector<IntrusiveRefCntPtr> Abbrevs;
5959 };
6060 std::vector BlockInfoRecords;
6161
9898 ~BitstreamWriter() {
9999 assert(CurBit == 0 && "Unflushed data remaining");
100100 assert(BlockScope.empty() && CurAbbrevs.empty() && "Block imbalance");
101
102 // Free the BlockInfoRecords.
103 while (!BlockInfoRecords.empty()) {
104 BlockInfo &Info = BlockInfoRecords.back();
105 // Free blockinfo abbrev info.
106 for (unsigned i = 0, e = static_cast(Info.Abbrevs.size());
107 i != e; ++i)
108 Info.Abbrevs[i]->dropRef();
109 BlockInfoRecords.pop_back();
110 }
111101 }
112102
113103 /// \brief Retrieve the current position in the stream, in bits.
230220 // If there is a blockinfo for this BlockID, add all the predefined abbrevs
231221 // to the abbrev list.
232222 if (BlockInfo *Info = getBlockInfo(BlockID)) {
233 for (unsigned i = 0, e = static_cast(Info->Abbrevs.size());
234 i != e; ++i) {
235 CurAbbrevs.push_back(Info->Abbrevs[i]);
236 Info->Abbrevs[i]->addRef();
237 }
223 CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
224 Info->Abbrevs.end());
238225 }
239226 }
240227
241228 void ExitBlock() {
242229 assert(!BlockScope.empty() && "Block scope imbalance!");
243
244 // Delete all abbrevs.
245 for (unsigned i = 0, e = static_cast(CurAbbrevs.size());
246 i != e; ++i)
247 CurAbbrevs[i]->dropRef();
248
249230 const Block &B = BlockScope.back();
250231
251232 // Block tail:
262243
263244 // Restore the inner block's code size and abbrev table.
264245 CurCodeSize = B.PrevCodeSize;
265 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
246 CurAbbrevs = std::move(B.PrevAbbrevs);
266247 BlockScope.pop_back();
267248 }
268249
316297 unsigned BlobLen = (unsigned) Blob.size();
317298 unsigned AbbrevNo = Abbrev-bitc::FIRST_APPLICATION_ABBREV;
318299 assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
319 BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo];
300 const BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo].get();
320301
321302 EmitCode(Abbrev);
322303
1414 // BitstreamCursor implementation
1515 //===----------------------------------------------------------------------===//
1616
17 void BitstreamCursor::operator=(const BitstreamCursor &RHS) {
18 freeState();
19
20 BitStream = RHS.BitStream;
21 NextChar = RHS.NextChar;
22 CurWord = RHS.CurWord;
23 BitsInCurWord = RHS.BitsInCurWord;
24 CurCodeSize = RHS.CurCodeSize;
25
26 // Copy abbreviations, and bump ref counts.
27 CurAbbrevs = RHS.CurAbbrevs;
28 for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
29 CurAbbrevs[i]->addRef();
30
31 // Copy block scope and bump ref counts.
32 BlockScope = RHS.BlockScope;
33 for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
34 std::vector &Abbrevs = BlockScope[S].PrevAbbrevs;
35 for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
36 Abbrevs[i]->addRef();
37 }
38 }
39
4017 void BitstreamCursor::freeState() {
4118 // Free all the Abbrevs.
42 for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
43 CurAbbrevs[i]->dropRef();
4419 CurAbbrevs.clear();
4520
4621 // Free all the Abbrevs in the block scope.
47 for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
48 std::vector &Abbrevs = BlockScope[S].PrevAbbrevs;
49 for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
50 Abbrevs[i]->dropRef();
51 }
5222 BlockScope.clear();
5323 }
5424
6232 // Add the abbrevs specific to this block to the CurAbbrevs list.
6333 if (const BitstreamReader::BlockInfo *Info =
6434 BitStream->getBlockInfo(BlockID)) {
65 for (size_t i = 0, e = Info->Abbrevs.size(); i != e; ++i) {
66 CurAbbrevs.push_back(Info->Abbrevs[i]);
67 CurAbbrevs.back()->addRef();
68 }
35 CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
36 Info->Abbrevs.end());
6937 }
7038
7139 // Get the codesize of this block.
338306
339307 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
340308 // appropriate BlockInfo.
341 BitCodeAbbrev *Abbv = CurAbbrevs.back();
309 CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
342310 CurAbbrevs.pop_back();
343 CurBlockInfo->Abbrevs.push_back(Abbv);
344311 continue;
345312 }
346313