From 11a2ce8da33e0ec4be250fd85c47f706d6c91a10 Mon Sep 17 00:00:00 2001 From: Chih-Hung Hsieh Date: Wed, 27 Jul 2016 14:11:02 -0700 Subject: [PATCH] Fix clang-tidy performance warnings in system/vold. * Use const reference type for for-loop index variables to avoid unnecessary copy. Bug: 30413223 Change-Id: Id4d980ae8afec1374fc3be0b23f1c6a39bff86e0 Test: build with WITH_TIDY=1 --- Disk.cpp | 8 ++++---- TrimTask.cpp | 4 ++-- Utils.cpp | 2 +- VolumeBase.cpp | 2 +- VolumeManager.cpp | 18 +++++++++--------- 5 files changed, 17 insertions(+), 17 deletions(-) mode change 100755 => 100644 VolumeManager.cpp diff --git a/Disk.cpp b/Disk.cpp index 920edab..f03f93d 100644 --- a/Disk.cpp +++ b/Disk.cpp @@ -106,7 +106,7 @@ std::shared_ptr Disk::findVolume(const std::string& id) { } void Disk::listVolumes(VolumeBase::Type type, std::list& list) { - for (auto vol : mVolumes) { + for (const auto& vol : mVolumes) { if (vol->getType() == type) { list.push_back(vol->getId()); } @@ -179,7 +179,7 @@ void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) { } void Disk::destroyAllVolumes() { - for (auto vol : mVolumes) { + for (const auto& vol : mVolumes) { vol->destroy(); } mVolumes.clear(); @@ -268,7 +268,7 @@ status_t Disk::readPartitions() { Table table = Table::kUnknown; bool foundParts = false; - for (auto line : output) { + for (const auto& line : output) { char* cline = (char*) line.c_str(); char* token = strtok(cline, kSgdiskToken); if (token == nullptr) continue; @@ -333,7 +333,7 @@ status_t Disk::readPartitions() { } status_t Disk::unmountAll() { - for (auto vol : mVolumes) { + for (const auto& vol : mVolumes) { vol->unmount(); } return OK; diff --git a/TrimTask.cpp b/TrimTask.cpp index d7bfda7..6c141f2 100644 --- a/TrimTask.cpp +++ b/TrimTask.cpp @@ -53,7 +53,7 @@ TrimTask::TrimTask(int flags) : mFlags(flags) { VolumeManager* vm = VolumeManager::Instance(); std::list privateIds; vm->listVolumes(VolumeBase::Type::kPrivate, privateIds); - for (auto id : privateIds) { + for (const auto& id : privateIds) { auto vol = vm->findVolume(id); if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) { mPaths.push_back(vol->getPath()); @@ -114,7 +114,7 @@ static void notifyResult(const std::string& path, int64_t bytes, int64_t delta) void TrimTask::run() { acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock); - for (auto path : mPaths) { + for (const auto& path : mPaths) { LOG(DEBUG) << "Starting trim of " << path; int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW); diff --git a/Utils.cpp b/Utils.cpp index a9bddfa..97dea79 100644 --- a/Utils.cpp +++ b/Utils.cpp @@ -208,7 +208,7 @@ static status_t readMetadata(const std::string& path, std::string& fsType, } char value[128]; - for (auto line : output) { + for (const auto& line : output) { // Extract values from blkid output, if defined const char* cline = line.c_str(); const char* start = strstr(cline, "TYPE="); diff --git a/VolumeBase.cpp b/VolumeBase.cpp index ea4d372..627feba 100644 --- a/VolumeBase.cpp +++ b/VolumeBase.cpp @@ -219,7 +219,7 @@ status_t VolumeBase::unmount() { } setState(State::kEjecting); - for (auto vol : mVolumes) { + for (const auto& vol : mVolumes) { if (vol->destroy()) { LOG(WARNING) << getId() << " failed to destroy " << vol->getId() << " stacked above"; diff --git a/VolumeManager.cpp b/VolumeManager.cpp old mode 100755 new mode 100644 index 57b8753..001583f --- a/VolumeManager.cpp +++ b/VolumeManager.cpp @@ -294,7 +294,7 @@ void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { switch (evt->getAction()) { case NetlinkEvent::Action::kAdd: { - for (auto source : mDiskSources) { + for (const auto& source : mDiskSources) { if (source->matches(eventPath)) { // For now, assume that MMC devices are SD, and that // everything else is USB @@ -316,7 +316,7 @@ void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { } case NetlinkEvent::Action::kChange: { LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed"; - for (auto disk : mDisks) { + for (const auto& disk : mDisks) { if (disk->getDevice() == device) { disk->readMetadata(); disk->readPartitions(); @@ -360,7 +360,7 @@ std::shared_ptr VolumeManager::findVolume(const std:: if (mInternalEmulated->getId() == id) { return mInternalEmulated; } - for (auto disk : mDisks) { + for (const auto& disk : mDisks) { auto vol = disk->findVolume(id); if (vol != nullptr) { return vol; @@ -372,7 +372,7 @@ std::shared_ptr VolumeManager::findVolume(const std:: void VolumeManager::listVolumes(android::vold::VolumeBase::Type type, std::list& list) { list.clear(); - for (auto disk : mDisks) { + for (const auto& disk : mDisks) { disk->listVolumes(type, list); } } @@ -503,7 +503,7 @@ static int unmount_tree(const char* path) { } endmntent(fp); - for (auto path : toUnmount) { + for (const auto& path : toUnmount) { if (umount2(path.c_str(), MNT_DETACH)) { ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno)); } @@ -629,7 +629,7 @@ int VolumeManager::reset() { // newly connected framework hears all events. mInternalEmulated->destroy(); mInternalEmulated->create(); - for (auto disk : mDisks) { + for (const auto& disk : mDisks) { disk->destroy(); disk->create(); } @@ -640,7 +640,7 @@ int VolumeManager::reset() { int VolumeManager::shutdown() { mInternalEmulated->destroy(); - for (auto disk : mDisks) { + for (const auto& disk : mDisks) { disk->destroy(); } mDisks.clear(); @@ -654,7 +654,7 @@ int VolumeManager::unmountAll() { if (mInternalEmulated != nullptr) { mInternalEmulated->unmount(); } - for (auto disk : mDisks) { + for (const auto& disk : mDisks) { disk->unmountAll(); } @@ -678,7 +678,7 @@ int VolumeManager::unmountAll() { } endmntent(fp); - for (auto path : toUnmount) { + for (const auto& path : toUnmount) { SLOGW("Tearing down stale mount %s", path.c_str()); android::vold::ForceUnmount(path); }