Use unique_ptr to track ownership of dex files.
Bug: 18809837
Change-Id: Ie571eae8fc19ee9207390cff5c7e2a38071b126a
diff --git a/runtime/dex_file.cc b/runtime/dex_file.cc
index 3d4184b..3f6175f 100644
--- a/runtime/dex_file.cc
+++ b/runtime/dex_file.cc
@@ -125,7 +125,8 @@
}
bool DexFile::Open(const char* filename, const char* location, std::string* error_msg,
- std::vector<const DexFile*>* dex_files) {
+ std::vector<std::unique_ptr<const DexFile>>* dex_files) {
+ DCHECK(dex_files != nullptr) << "DexFile::Open: out-param is NULL";
uint32_t magic;
ScopedFd fd(OpenAndReadMagic(filename, &magic, error_msg));
if (fd.get() == -1) {
@@ -139,7 +140,7 @@
std::unique_ptr<const DexFile> dex_file(DexFile::OpenFile(fd.release(), location, true,
error_msg));
if (dex_file.get() != nullptr) {
- dex_files->push_back(dex_file.release());
+ dex_files->push_back(std::move(dex_file));
return true;
} else {
return false;
@@ -179,8 +180,8 @@
}
}
-const DexFile* DexFile::OpenFile(int fd, const char* location, bool verify,
- std::string* error_msg) {
+std::unique_ptr<const DexFile> DexFile::OpenFile(int fd, const char* location, bool verify,
+ std::string* error_msg) {
CHECK(location != nullptr);
std::unique_ptr<MemMap> map;
{
@@ -224,13 +225,14 @@
return nullptr;
}
- return dex_file.release();
+ return dex_file;
}
const char* DexFile::kClassesDex = "classes.dex";
bool DexFile::OpenZip(int fd, const std::string& location, std::string* error_msg,
- std::vector<const DexFile*>* dex_files) {
+ std::vector<std::unique_ptr<const DexFile>>* dex_files) {
+ DCHECK(dex_files != nullptr) << "DexFile::OpenZip: out-param is NULL";
std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd, location.c_str(), error_msg));
if (zip_archive.get() == nullptr) {
DCHECK(!error_msg->empty());
@@ -239,10 +241,10 @@
return DexFile::OpenFromZip(*zip_archive, location, error_msg, dex_files);
}
-const DexFile* DexFile::OpenMemory(const std::string& location,
- uint32_t location_checksum,
- MemMap* mem_map,
- std::string* error_msg) {
+std::unique_ptr<const DexFile> DexFile::OpenMemory(const std::string& location,
+ uint32_t location_checksum,
+ MemMap* mem_map,
+ std::string* error_msg) {
return OpenMemory(mem_map->Begin(),
mem_map->Size(),
location,
@@ -251,9 +253,9 @@
error_msg);
}
-const DexFile* DexFile::Open(const ZipArchive& zip_archive, const char* entry_name,
- const std::string& location, std::string* error_msg,
- ZipOpenErrorCode* error_code) {
+std::unique_ptr<const DexFile> DexFile::Open(const ZipArchive& zip_archive, const char* entry_name,
+ const std::string& location, std::string* error_msg,
+ ZipOpenErrorCode* error_code) {
CHECK(!location.empty());
std::unique_ptr<ZipEntry> zip_entry(zip_archive.Find(entry_name, error_msg));
if (zip_entry.get() == NULL) {
@@ -287,11 +289,13 @@
return nullptr;
}
*error_code = ZipOpenErrorCode::kNoError;
- return dex_file.release();
+ return dex_file;
}
bool DexFile::OpenFromZip(const ZipArchive& zip_archive, const std::string& location,
- std::string* error_msg, std::vector<const DexFile*>* dex_files) {
+ std::string* error_msg,
+ std::vector<std::unique_ptr<const DexFile>>* dex_files) {
+ DCHECK(dex_files != nullptr) << "DexFile::OpenFromZip: out-param is NULL";
ZipOpenErrorCode error_code;
std::unique_ptr<const DexFile> dex_file(Open(zip_archive, kClassesDex, location, error_msg,
&error_code));
@@ -299,7 +303,7 @@
return false;
} else {
// Had at least classes.dex.
- dex_files->push_back(dex_file.release());
+ dex_files->push_back(std::move(dex_file));
// Now try some more.
size_t i = 2;
@@ -318,7 +322,7 @@
}
break;
} else {
- dex_files->push_back(next_dex_file.release());
+ dex_files->push_back(std::move(next_dex_file));
}
i++;
@@ -329,18 +333,17 @@
}
-const DexFile* DexFile::OpenMemory(const uint8_t* base,
- size_t size,
- const std::string& location,
- uint32_t location_checksum,
- MemMap* mem_map, std::string* error_msg) {
+std::unique_ptr<const DexFile> DexFile::OpenMemory(const uint8_t* base,
+ size_t size,
+ const std::string& location,
+ uint32_t location_checksum,
+ MemMap* mem_map, std::string* error_msg) {
CHECK_ALIGNED(base, 4); // various dex file structures must be word aligned
std::unique_ptr<DexFile> dex_file(new DexFile(base, size, location, location_checksum, mem_map));
if (!dex_file->Init(error_msg)) {
- return nullptr;
- } else {
- return dex_file.release();
+ dex_file.reset();
}
+ return std::unique_ptr<const DexFile>(dex_file.release());
}
DexFile::DexFile(const uint8_t* base, size_t size,