[llvm] r217778 - Use IntrusiveRefCntPtr to manage the lifetime of BitCodeAbbrevs.
Benjamin Kramer
benny.kra at googlemail.com
Mon Sep 15 08:44:15 PDT 2014
Author: d0k
Date: Mon Sep 15 10:44:14 2014
New Revision: 217778
URL: http://llvm.org/viewvc/llvm-project?rev=217778&view=rev
Log:
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.
Modified:
llvm/trunk/include/llvm/Bitcode/BitCodes.h
llvm/trunk/include/llvm/Bitcode/BitstreamReader.h
llvm/trunk/include/llvm/Bitcode/BitstreamWriter.h
llvm/trunk/lib/Bitcode/Reader/BitstreamReader.cpp
Modified: llvm/trunk/include/llvm/Bitcode/BitCodes.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/BitCodes.h?rev=217778&r1=217777&r2=217778&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Bitcode/BitCodes.h (original)
+++ llvm/trunk/include/llvm/Bitcode/BitCodes.h Mon Sep 15 10:44:14 2014
@@ -18,6 +18,7 @@
#ifndef LLVM_BITCODE_BITCODES_H
#define LLVM_BITCODE_BITCODES_H
+#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/ErrorHandling.h"
@@ -161,16 +162,12 @@ template <> struct isPodLike<BitCodeAbbr
/// BitCodeAbbrev - This class represents an abbreviation record. An
/// abbreviation allows a complex record that has redundancy to be stored in a
/// specialized format instead of the fully-general, fully-vbr, format.
-class BitCodeAbbrev {
+class BitCodeAbbrev : public RefCountedBase<BitCodeAbbrev> {
SmallVector<BitCodeAbbrevOp, 32> OperandList;
- unsigned char RefCount; // Number of things using this.
~BitCodeAbbrev() {}
-public:
- BitCodeAbbrev() : RefCount(1) {}
-
- void addRef() { ++RefCount; }
- void dropRef() { if (--RefCount == 0) delete this; }
+ friend class RefCountedBase; // Only RefCountedBase is allowed to delete.
+public:
unsigned getNumOperandInfos() const {
return static_cast<unsigned>(OperandList.size());
}
Modified: llvm/trunk/include/llvm/Bitcode/BitstreamReader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/BitstreamReader.h?rev=217778&r1=217777&r2=217778&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Bitcode/BitstreamReader.h (original)
+++ llvm/trunk/include/llvm/Bitcode/BitstreamReader.h Mon Sep 15 10:44:14 2014
@@ -37,7 +37,7 @@ public:
/// These describe abbreviations that all blocks of the specified ID inherit.
struct BlockInfo {
unsigned BlockID;
- std::vector<BitCodeAbbrev*> Abbrevs;
+ std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> Abbrevs;
std::string Name;
std::vector<std::pair<unsigned, std::string> > RecordNames;
@@ -86,18 +86,6 @@ public:
StreamableMemoryObject &getBitcodeBytes() { return *BitcodeBytes; }
- ~BitstreamReader() {
- // Free the BlockInfoRecords.
- while (!BlockInfoRecords.empty()) {
- BlockInfo &Info = BlockInfoRecords.back();
- // Free blockinfo abbrev info.
- for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size());
- i != e; ++i)
- Info.Abbrevs[i]->dropRef();
- BlockInfoRecords.pop_back();
- }
- }
-
/// CollectBlockInfoNames - This is called by clients that want block/record
/// name information.
void CollectBlockInfoNames() { IgnoreBlockInfoNames = false; }
@@ -208,11 +196,11 @@ class BitstreamCursor {
unsigned CurCodeSize;
/// CurAbbrevs - Abbrevs installed at in this block.
- std::vector<BitCodeAbbrev*> CurAbbrevs;
+ std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> CurAbbrevs;
struct Block {
unsigned PrevCodeSize;
- std::vector<BitCodeAbbrev*> PrevAbbrevs;
+ std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> PrevAbbrevs;
explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
};
@@ -222,10 +210,6 @@ class BitstreamCursor {
public:
BitstreamCursor() : BitStream(nullptr), NextChar(0) {}
- BitstreamCursor(const BitstreamCursor &RHS)
- : BitStream(nullptr), NextChar(0) {
- operator=(RHS);
- }
explicit BitstreamCursor(BitstreamReader &R) : BitStream(&R) {
NextChar = 0;
@@ -244,12 +228,6 @@ public:
CurCodeSize = 2;
}
- ~BitstreamCursor() {
- freeState();
- }
-
- void operator=(const BitstreamCursor &RHS);
-
void freeState();
bool isEndPos(size_t pos) {
@@ -529,12 +507,7 @@ private:
void popBlockScope() {
CurCodeSize = BlockScope.back().PrevCodeSize;
- // Delete abbrevs from popped scope.
- for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
- i != e; ++i)
- CurAbbrevs[i]->dropRef();
-
- BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
+ CurAbbrevs = std::move(BlockScope.back().PrevAbbrevs);
BlockScope.pop_back();
}
@@ -555,7 +528,7 @@ public:
const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) {
unsigned AbbrevNo = AbbrevID-bitc::FIRST_APPLICATION_ABBREV;
assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
- return CurAbbrevs[AbbrevNo];
+ return CurAbbrevs[AbbrevNo].get();
}
/// skipRecord - Read the current record and discard it.
Modified: llvm/trunk/include/llvm/Bitcode/BitstreamWriter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/BitstreamWriter.h?rev=217778&r1=217777&r2=217778&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Bitcode/BitstreamWriter.h (original)
+++ llvm/trunk/include/llvm/Bitcode/BitstreamWriter.h Mon Sep 15 10:44:14 2014
@@ -40,12 +40,12 @@ class BitstreamWriter {
unsigned BlockInfoCurBID;
/// CurAbbrevs - Abbrevs installed at in this block.
- std::vector<BitCodeAbbrev*> CurAbbrevs;
+ std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> CurAbbrevs;
struct Block {
unsigned PrevCodeSize;
unsigned StartSizeWord;
- std::vector<BitCodeAbbrev*> PrevAbbrevs;
+ std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> PrevAbbrevs;
Block(unsigned PCS, unsigned SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {}
};
@@ -56,7 +56,7 @@ class BitstreamWriter {
/// These describe abbreviations that all blocks of the specified ID inherit.
struct BlockInfo {
unsigned BlockID;
- std::vector<BitCodeAbbrev*> Abbrevs;
+ std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> Abbrevs;
};
std::vector<BlockInfo> BlockInfoRecords;
@@ -99,16 +99,6 @@ public:
~BitstreamWriter() {
assert(CurBit == 0 && "Unflushed data remaining");
assert(BlockScope.empty() && CurAbbrevs.empty() && "Block imbalance");
-
- // Free the BlockInfoRecords.
- while (!BlockInfoRecords.empty()) {
- BlockInfo &Info = BlockInfoRecords.back();
- // Free blockinfo abbrev info.
- for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size());
- i != e; ++i)
- Info.Abbrevs[i]->dropRef();
- BlockInfoRecords.pop_back();
- }
}
/// \brief Retrieve the current position in the stream, in bits.
@@ -231,22 +221,13 @@ public:
// If there is a blockinfo for this BlockID, add all the predefined abbrevs
// to the abbrev list.
if (BlockInfo *Info = getBlockInfo(BlockID)) {
- for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size());
- i != e; ++i) {
- CurAbbrevs.push_back(Info->Abbrevs[i]);
- Info->Abbrevs[i]->addRef();
- }
+ CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
+ Info->Abbrevs.end());
}
}
void ExitBlock() {
assert(!BlockScope.empty() && "Block scope imbalance!");
-
- // Delete all abbrevs.
- for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
- i != e; ++i)
- CurAbbrevs[i]->dropRef();
-
const Block &B = BlockScope.back();
// Block tail:
@@ -263,7 +244,7 @@ public:
// Restore the inner block's code size and abbrev table.
CurCodeSize = B.PrevCodeSize;
- BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
+ CurAbbrevs = std::move(B.PrevAbbrevs);
BlockScope.pop_back();
}
@@ -317,7 +298,7 @@ private:
unsigned BlobLen = (unsigned) Blob.size();
unsigned AbbrevNo = Abbrev-bitc::FIRST_APPLICATION_ABBREV;
assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
- BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo];
+ const BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo].get();
EmitCode(Abbrev);
Modified: llvm/trunk/lib/Bitcode/Reader/BitstreamReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitstreamReader.cpp?rev=217778&r1=217777&r2=217778&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/BitstreamReader.cpp (original)
+++ llvm/trunk/lib/Bitcode/Reader/BitstreamReader.cpp Mon Sep 15 10:44:14 2014
@@ -15,41 +15,11 @@ using namespace llvm;
// BitstreamCursor implementation
//===----------------------------------------------------------------------===//
-void BitstreamCursor::operator=(const BitstreamCursor &RHS) {
- freeState();
-
- BitStream = RHS.BitStream;
- NextChar = RHS.NextChar;
- CurWord = RHS.CurWord;
- BitsInCurWord = RHS.BitsInCurWord;
- CurCodeSize = RHS.CurCodeSize;
-
- // Copy abbreviations, and bump ref counts.
- CurAbbrevs = RHS.CurAbbrevs;
- for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
- CurAbbrevs[i]->addRef();
-
- // Copy block scope and bump ref counts.
- BlockScope = RHS.BlockScope;
- for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
- std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
- for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
- Abbrevs[i]->addRef();
- }
-}
-
void BitstreamCursor::freeState() {
// Free all the Abbrevs.
- for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
- CurAbbrevs[i]->dropRef();
CurAbbrevs.clear();
// Free all the Abbrevs in the block scope.
- for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
- std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
- for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
- Abbrevs[i]->dropRef();
- }
BlockScope.clear();
}
@@ -63,10 +33,8 @@ bool BitstreamCursor::EnterSubBlock(unsi
// Add the abbrevs specific to this block to the CurAbbrevs list.
if (const BitstreamReader::BlockInfo *Info =
BitStream->getBlockInfo(BlockID)) {
- for (size_t i = 0, e = Info->Abbrevs.size(); i != e; ++i) {
- CurAbbrevs.push_back(Info->Abbrevs[i]);
- CurAbbrevs.back()->addRef();
- }
+ CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
+ Info->Abbrevs.end());
}
// Get the codesize of this block.
@@ -339,9 +307,8 @@ bool BitstreamCursor::ReadBlockInfoBlock
// ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
// appropriate BlockInfo.
- BitCodeAbbrev *Abbv = CurAbbrevs.back();
+ CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
CurAbbrevs.pop_back();
- CurBlockInfo->Abbrevs.push_back(Abbv);
continue;
}
More information about the llvm-commits
mailing list