diff --git a/ChangeLog b/ChangeLog index c229fd24..5e4866ad 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2008-03-08 Tatsuhiro Tsujikawa + + Type clarification + * src/PeerSessionResource.{h, cc} + * src/DefaultPieceStorage.{h, cc} + * src/Peer.{h, cc} + * test/PeerSessionResourceTest.cc + + Use div function + * src/BtPieceMessage.cc (erasePieceOnDisk) + 2008-03-08 Tatsuhiro Tsujikawa Type clarification diff --git a/src/BtPieceMessage.cc b/src/BtPieceMessage.cc index 41d8bb5a..26755a99 100644 --- a/src/BtPieceMessage.cc +++ b/src/BtPieceMessage.cc @@ -223,18 +223,17 @@ void BtPieceMessage::onWrongPiece(const PieceHandle& piece) { } void BtPieceMessage::erasePieceOnDisk(const PieceHandle& piece) { - int32_t BUFSIZE = 4096; + size_t BUFSIZE = 4096; unsigned char buf[BUFSIZE]; memset(buf, 0, BUFSIZE); - int64_t offset = - ((int64_t)piece->getIndex())*btContext->getPieceLength(); - for(int32_t i = 0; i < piece->getLength()/BUFSIZE; i++) { + off_t offset = (off_t)piece->getIndex()*btContext->getPieceLength(); + div_t res = div(piece->getLength(), BUFSIZE); + for(int i = 0; i < res.quot; i++) { pieceStorage->getDiskAdaptor()->writeData(buf, BUFSIZE, offset); offset += BUFSIZE; } - int32_t r = piece->getLength()%BUFSIZE; - if(r > 0) { - pieceStorage->getDiskAdaptor()->writeData(buf, r, offset); + if(res.rem > 0) { + pieceStorage->getDiskAdaptor()->writeData(buf, res.rem, offset); } } diff --git a/src/DefaultPieceStorage.cc b/src/DefaultPieceStorage.cc index 4752ed00..34c9a4bc 100644 --- a/src/DefaultPieceStorage.cc +++ b/src/DefaultPieceStorage.cc @@ -175,7 +175,7 @@ bool DefaultPieceStorage::getMissingFastPieceIndex(size_t& index, if(peer->isFastExtensionEnabled() && peer->countPeerAllowedIndexSet() > 0) { BitfieldMan tempBitfield(bitfieldMan->getBlockLength(), bitfieldMan->getTotalLength()); - for(std::deque::const_iterator itr = peer->getPeerAllowedIndexSet().begin(); + for(std::deque::const_iterator itr = peer->getPeerAllowedIndexSet().begin(); itr != peer->getPeerAllowedIndexSet().end(); itr++) { if(!bitfieldMan->isBitSet(index) && peer->hasPiece(*itr)) { tempBitfield.setBit(*itr); diff --git a/src/DefaultPieceStorage.h b/src/DefaultPieceStorage.h index 31d0981f..61fe5a70 100644 --- a/src/DefaultPieceStorage.h +++ b/src/DefaultPieceStorage.h @@ -60,7 +60,7 @@ public: int32_t getCuid() const { return cuid; } - int32_t getIndex() const { return index; } + size_t getIndex() const { return index; } const Time& getRegisteredTime() const { return registeredTime; } }; diff --git a/src/Peer.cc b/src/Peer.cc index cecc2d8e..c89a26b7 100644 --- a/src/Peer.cc +++ b/src/Peer.cc @@ -96,7 +96,7 @@ bool Peer::unused() const return _cuid == 0; } -void Peer::allocateSessionResource(int32_t pieceLength, int64_t totalLength) +void Peer::allocateSessionResource(size_t pieceLength, uint64_t totalLength) { delete _res; _res = new PeerSessionResource(pieceLength, totalLength); @@ -218,13 +218,13 @@ void Peer::snubbing(bool b) _res->snubbing(b); } -void Peer::updateUploadLength(int32_t bytes) +void Peer::updateUploadLength(size_t bytes) { assert(_res); _res->updateUploadLength(bytes); } -void Peer::updateDownloadLength(int32_t bytes) +void Peer::updateDownloadLength(size_t bytes) { assert(_res); _res->updateDownloadLength(bytes); @@ -238,49 +238,49 @@ void Peer::updateSeeder() } } -void Peer::updateBitfield(int32_t index, int32_t operation) { +void Peer::updateBitfield(size_t index, int operation) { assert(_res); _res->updateBitfield(index, operation); updateSeeder(); } -int32_t Peer::calculateUploadSpeed() +unsigned int Peer::calculateUploadSpeed() { assert(_res); return _res->getPeerStat().calculateUploadSpeed(); } -int32_t Peer::calculateUploadSpeed(const struct timeval& now) +unsigned int Peer::calculateUploadSpeed(const struct timeval& now) { assert(_res); return _res->getPeerStat().calculateUploadSpeed(now); } -int32_t Peer::calculateDownloadSpeed() +unsigned int Peer::calculateDownloadSpeed() { assert(_res); return _res->getPeerStat().calculateDownloadSpeed(); } -int32_t Peer::calculateDownloadSpeed(const struct timeval& now) +unsigned int Peer::calculateDownloadSpeed(const struct timeval& now) { assert(_res); return _res->getPeerStat().calculateDownloadSpeed(now); } -int64_t Peer::getSessionUploadLength() const +uint64_t Peer::getSessionUploadLength() const { assert(_res); return _res->uploadLength(); } -int64_t Peer::getSessionDownloadLength() const +uint64_t Peer::getSessionDownloadLength() const { assert(_res); return _res->downloadLength(); } -void Peer::setBitfield(const unsigned char* bitfield, int32_t bitfieldLength) +void Peer::setBitfield(const unsigned char* bitfield, size_t bitfieldLength) { assert(_res); _res->setBitfield(bitfield, bitfieldLength); @@ -293,7 +293,7 @@ const unsigned char* Peer::getBitfield() const return _res->getBitfield(); } -int32_t Peer::getBitfieldLength() const +size_t Peer::getBitfieldLength() const { assert(_res); return _res->getBitfieldLength(); @@ -304,7 +304,7 @@ bool Peer::shouldBeChoking() const { return _res->shouldBeChoking(); } -bool Peer::hasPiece(int32_t index) const { +bool Peer::hasPiece(size_t index) const { assert(_res); return _res->hasPiece(index); } @@ -327,31 +327,31 @@ size_t Peer::countPeerAllowedIndexSet() const return _res->peerAllowedIndexSet().size(); } -const std::deque& Peer::getPeerAllowedIndexSet() const +const std::deque& Peer::getPeerAllowedIndexSet() const { assert(_res); return _res->peerAllowedIndexSet(); } -bool Peer::isInPeerAllowedIndexSet(int32_t index) const +bool Peer::isInPeerAllowedIndexSet(size_t index) const { assert(_res); return _res->peerAllowedIndexSetContains(index); } -void Peer::addPeerAllowedIndex(int32_t index) +void Peer::addPeerAllowedIndex(size_t index) { assert(_res); _res->addPeerAllowedIndex(index); } -bool Peer::isInAmAllowedIndexSet(int32_t index) const +bool Peer::isInAmAllowedIndexSet(size_t index) const { assert(_res); return _res->amAllowedIndexSetContains(index); } -void Peer::addAmAllowedIndex(int32_t index) +void Peer::addAmAllowedIndex(size_t index) { assert(_res); _res->addAmAllowedIndex(index); @@ -363,13 +363,13 @@ void Peer::setAllBitfield() { _seeder = true; } -void Peer::updateLatency(int32_t latency) +void Peer::updateLatency(unsigned int latency) { assert(_res); _res->updateLatency(latency); } -int32_t Peer::getLatency() const +unsigned int Peer::getLatency() const { assert(_res); return _res->latency(); diff --git a/src/Peer.h b/src/Peer.h index 98058e91..c228470f 100644 --- a/src/Peer.h +++ b/src/Peer.h @@ -105,7 +105,7 @@ public: bool isGood() const; - void allocateSessionResource(int32_t pieceLength, int64_t totalLength); + void allocateSessionResource(size_t pieceLength, uint64_t totalLength); void releaseSessionResource(); @@ -152,39 +152,39 @@ public: void snubbing(bool b); - void updateUploadLength(int32_t bytes); + void updateUploadLength(size_t bytes); - void updateDownloadLength(int32_t bytes); + void updateDownloadLength(size_t bytes); /** * Returns the transfer rate from localhost to remote host. */ - int32_t calculateUploadSpeed(); + unsigned int calculateUploadSpeed(); - int32_t calculateUploadSpeed(const struct timeval& now); + unsigned int calculateUploadSpeed(const struct timeval& now); /** * Returns the transfer rate from remote host to localhost. */ - int32_t calculateDownloadSpeed(); + unsigned int calculateDownloadSpeed(); - int32_t calculateDownloadSpeed(const struct timeval& now); + unsigned int calculateDownloadSpeed(const struct timeval& now); /** * Returns the number of bytes uploaded to the remote host. */ - int64_t getSessionUploadLength() const; + uint64_t getSessionUploadLength() const; /** * Returns the number of bytes downloaded from the remote host. */ - int64_t getSessionDownloadLength() const; + uint64_t getSessionDownloadLength() const; - void setBitfield(const unsigned char* bitfield, int32_t bitfieldLength); + void setBitfield(const unsigned char* bitfield, size_t bitfieldLength); const unsigned char* getBitfield() const; - int32_t getBitfieldLength() const; + size_t getBitfieldLength() const; void setAllBitfield(); @@ -192,23 +192,23 @@ public: * operation = 1: set index-th bit to 1 * operation = 0: set index-th bit to 0 */ - void updateBitfield(int32_t index, int32_t operation); + void updateBitfield(size_t index, int operation); void setFastExtensionEnabled(bool enabled); bool isFastExtensionEnabled() const; - void addPeerAllowedIndex(int32_t index); + void addPeerAllowedIndex(size_t index); - bool isInPeerAllowedIndexSet(int32_t index) const; + bool isInPeerAllowedIndexSet(size_t index) const; size_t countPeerAllowedIndexSet() const; - const std::deque& getPeerAllowedIndexSet() const; + const std::deque& getPeerAllowedIndexSet() const; - void addAmAllowedIndex(int32_t index); + void addAmAllowedIndex(size_t index); - bool isInAmAllowedIndexSet(int32_t index) const; + bool isInAmAllowedIndexSet(size_t index) const; void setExtendedMessagingEnabled(bool enabled); @@ -220,11 +220,11 @@ public: bool shouldBeChoking() const; - bool hasPiece(int32_t index) const; + bool hasPiece(size_t index) const; - void updateLatency(int32_t latency); + void updateLatency(unsigned int latency); - int32_t getLatency() const; + unsigned int getLatency() const; uint8_t getExtensionMessageID(const std::string& name) const; diff --git a/src/PeerSessionResource.cc b/src/PeerSessionResource.cc index 9bbdacca..2501041e 100644 --- a/src/PeerSessionResource.cc +++ b/src/PeerSessionResource.cc @@ -39,7 +39,7 @@ namespace aria2 { -PeerSessionResource::PeerSessionResource(int32_t pieceLength, int64_t totalLength): +PeerSessionResource::PeerSessionResource(size_t pieceLength, uint64_t totalLength): _amChoking(true), _amInterested(false), _peerChoking(true), @@ -144,7 +144,7 @@ bool PeerSessionResource::hasAllPieces() const return _bitfieldMan->isAllBitSet(); } -void PeerSessionResource::updateBitfield(int32_t index, int32_t operation) +void PeerSessionResource::updateBitfield(size_t index, int operation) { if(operation == 1) { _bitfieldMan->setBit(index); @@ -168,7 +168,7 @@ size_t PeerSessionResource::getBitfieldLength() const return _bitfieldMan->getBitfieldLength(); } -bool PeerSessionResource::hasPiece(int32_t index) const +bool PeerSessionResource::hasPiece(size_t index) const { return _bitfieldMan->isBitSet(index); } @@ -188,7 +188,7 @@ void PeerSessionResource::fastExtensionEnabled(bool b) _fastExtensionEnabled = b; } -const std::deque& PeerSessionResource::peerAllowedIndexSet() const +const std::deque& PeerSessionResource::peerAllowedIndexSet() const { return _peerAllowedIndexSet; } @@ -199,31 +199,31 @@ bool PeerSessionResource::indexIncluded(const std::deque& c, T index) const return std::find(c.begin(), c.end(), index) != c.end(); } -void PeerSessionResource::addPeerAllowedIndex(int32_t index) +void PeerSessionResource::addPeerAllowedIndex(size_t index) { if(!indexIncluded(_peerAllowedIndexSet, index)) { _peerAllowedIndexSet.push_back(index); } } -bool PeerSessionResource::peerAllowedIndexSetContains(int32_t index) const +bool PeerSessionResource::peerAllowedIndexSetContains(size_t index) const { return indexIncluded(_peerAllowedIndexSet, index); } -const std::deque& PeerSessionResource::amAllowedIndexSet() const +const std::deque& PeerSessionResource::amAllowedIndexSet() const { return _amAllowedIndexSet; } -void PeerSessionResource::addAmAllowedIndex(int32_t index) +void PeerSessionResource::addAmAllowedIndex(size_t index) { if(!indexIncluded(_amAllowedIndexSet, index)) { _amAllowedIndexSet.push_back(index); } } -bool PeerSessionResource::amAllowedIndexSetContains(int32_t index) const +bool PeerSessionResource::amAllowedIndexSetContains(size_t index) const { return indexIncluded(_amAllowedIndexSet, index); } @@ -281,33 +281,33 @@ PeerStat& PeerSessionResource::getPeerStat() return _peerStat; } -int32_t PeerSessionResource::latency() const +unsigned int PeerSessionResource::latency() const { return _latency; } -void PeerSessionResource::updateLatency(int32_t latency) +void PeerSessionResource::updateLatency(unsigned int latency) { _latency = _latency*0.2+latency*0.8; } -int64_t PeerSessionResource::uploadLength() const +uint64_t PeerSessionResource::uploadLength() const { return _uploadLength; } -void PeerSessionResource::updateUploadLength(int32_t bytes) +void PeerSessionResource::updateUploadLength(size_t bytes) { _peerStat.updateUploadLength(bytes); _uploadLength += bytes; } -int64_t PeerSessionResource::downloadLength() const +uint64_t PeerSessionResource::downloadLength() const { return _downloadLength; } -void PeerSessionResource::updateDownloadLength(int32_t bytes) +void PeerSessionResource::updateDownloadLength(size_t bytes) { _peerStat.updateDownloadLength(bytes); _downloadLength += bytes; diff --git a/src/PeerSessionResource.h b/src/PeerSessionResource.h index c4ede68e..465aba80 100644 --- a/src/PeerSessionResource.h +++ b/src/PeerSessionResource.h @@ -65,21 +65,21 @@ private: BitfieldMan* _bitfieldMan; bool _fastExtensionEnabled; // fast index set which a peer has sent to localhost. - std::deque _peerAllowedIndexSet; + std::deque _peerAllowedIndexSet; // fast index set which localhost has sent to a peer. - std::deque _amAllowedIndexSet; + std::deque _amAllowedIndexSet; bool _extendedMessagingEnabled; Extensions _extensions; bool _dhtEnabled; PeerStat _peerStat; - int32_t _latency; - int64_t _uploadLength; - int64_t _downloadLength; + unsigned int _latency; + uint64_t _uploadLength; + uint64_t _downloadLength; template bool indexIncluded(const std::deque& c, T index) const; public: - PeerSessionResource(int32_t pieceLength, int64_t totalLength); + PeerSessionResource(size_t pieceLength, uint64_t totalLength); ~PeerSessionResource(); @@ -122,7 +122,7 @@ public: bool hasAllPieces() const; - void updateBitfield(int32_t index, int32_t operation); + void updateBitfield(size_t index, int operation); void setBitfield(const unsigned char* bitfield, size_t bitfieldLength); @@ -130,7 +130,7 @@ public: size_t getBitfieldLength() const; - bool hasPiece(int32_t index) const; + bool hasPiece(size_t index) const; void markSeeder(); @@ -139,18 +139,18 @@ public: void fastExtensionEnabled(bool b); // fast index set which a peer has sent to localhost. - const std::deque& peerAllowedIndexSet() const; + const std::deque& peerAllowedIndexSet() const; - void addPeerAllowedIndex(int32_t index); + void addPeerAllowedIndex(size_t index); - bool peerAllowedIndexSetContains(int32_t index) const; + bool peerAllowedIndexSetContains(size_t index) const; // fast index set which localhost has sent to a peer. - const std::deque& amAllowedIndexSet() const; + const std::deque& amAllowedIndexSet() const; - void addAmAllowedIndex(int32_t index); + void addAmAllowedIndex(size_t index); - bool amAllowedIndexSetContains(int32_t index) const; + bool amAllowedIndexSetContains(size_t index) const; bool extendedMessagingEnabled() const; @@ -168,17 +168,17 @@ public: PeerStat& getPeerStat(); - int32_t latency() const; + unsigned int latency() const; - void updateLatency(int32_t l); + void updateLatency(unsigned int latency); - int64_t uploadLength() const; + uint64_t uploadLength() const; - void updateUploadLength(int32_t bytes); + void updateUploadLength(size_t bytes); - int64_t downloadLength() const; + uint64_t downloadLength() const; - void updateDownloadLength(int32_t bytes); + void updateDownloadLength(size_t bytes); }; } // namespace aria2 diff --git a/test/PeerSessionResourceTest.cc b/test/PeerSessionResourceTest.cc index 15a33b32..5d39414b 100644 --- a/test/PeerSessionResourceTest.cc +++ b/test/PeerSessionResourceTest.cc @@ -103,29 +103,29 @@ void PeerSessionResourceTest::testUpdateUploadLength() { PeerSessionResource res(1024, 1024*1024); - CPPUNIT_ASSERT_EQUAL((int64_t)0, res.uploadLength()); + CPPUNIT_ASSERT_EQUAL(0ULL, res.uploadLength()); res.updateUploadLength(100); res.updateUploadLength(200); - CPPUNIT_ASSERT_EQUAL((int64_t)300, res.uploadLength()); + CPPUNIT_ASSERT_EQUAL(300ULL, res.uploadLength()); } void PeerSessionResourceTest::testUpdateDownloadLength() { PeerSessionResource res(1024, 1024*1024); - CPPUNIT_ASSERT_EQUAL((int64_t)0, res.downloadLength()); + CPPUNIT_ASSERT_EQUAL(0ULL, res.downloadLength()); res.updateDownloadLength(100); res.updateDownloadLength(200); - CPPUNIT_ASSERT_EQUAL((int64_t)300, res.downloadLength()); + CPPUNIT_ASSERT_EQUAL(300ULL, res.downloadLength()); } void PeerSessionResourceTest::testUpdateLatency() { PeerSessionResource res(1024, 1024*1024); - CPPUNIT_ASSERT_EQUAL(1500, res.latency()); + CPPUNIT_ASSERT_EQUAL((unsigned int)1500, res.latency()); res.updateLatency(1000); - CPPUNIT_ASSERT_EQUAL(1100, res.latency()); + CPPUNIT_ASSERT_EQUAL((unsigned int)1100, res.latency()); } void PeerSessionResourceTest::testExtendedMessageEnabled()