Skip to content

Commit

Permalink
Unified usage of vector<unique_ptr<T>>
Browse files Browse the repository at this point in the history
Replace occurrences of vector of ptr and ScopedVector (deprecated) by vector<unique_ptr<T>>

BUG=554289

Review-Url: https://codereview.chromium.org/1969543002
Cr-Commit-Position: refs/heads/master@{#393040}
  • Loading branch information
etiennep authored and Commit bot committed May 11, 2016
1 parent 97bb027 commit 7d4e8ee
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 45 deletions.
15 changes: 9 additions & 6 deletions courgette/disassembler_elf_32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "courgette/disassembler_elf_32.h"

#include <algorithm>
#include <utility>

#include "base/logging.h"
#include "courgette/assembly_program.h"
Expand Down Expand Up @@ -273,8 +274,8 @@ CheckBool DisassemblerElf32::RVAsToFileOffsets(
}

CheckBool DisassemblerElf32::RVAsToFileOffsets(
ScopedVector<TypedRVA>* typed_rvas) {
for (TypedRVA* typed_rva : *typed_rvas) {
std::vector<std::unique_ptr<TypedRVA>>* typed_rvas) {
for (auto& typed_rva : *typed_rvas) {
FileOffset file_offset = RVAToFileOffset(typed_rva->rva());
if (file_offset == kNoFileOffset)
return false;
Expand Down Expand Up @@ -305,8 +306,10 @@ CheckBool DisassemblerElf32::ParseFile(AssemblyProgram* program) {
std::vector<FileOffset>::iterator current_abs_offset = abs_offsets.begin();
std::vector<FileOffset>::iterator end_abs_offset = abs_offsets.end();

ScopedVector<TypedRVA>::iterator current_rel = rel32_locations_.begin();
ScopedVector<TypedRVA>::iterator end_rel = rel32_locations_.end();
std::vector<std::unique_ptr<TypedRVA>>::iterator current_rel =
rel32_locations_.begin();
std::vector<std::unique_ptr<TypedRVA>>::iterator end_rel =
rel32_locations_.end();

// Visit section headers ordered by file offset.
for (Elf32_Half section_id : section_header_file_offset_order_) {
Expand Down Expand Up @@ -374,8 +377,8 @@ CheckBool DisassemblerElf32::ParseProgbitsSection(
const Elf32_Shdr* section_header,
std::vector<FileOffset>::iterator* current_abs_offset,
std::vector<FileOffset>::iterator end_abs_offset,
ScopedVector<TypedRVA>::iterator* current_rel,
ScopedVector<TypedRVA>::iterator end_rel,
std::vector<std::unique_ptr<TypedRVA>>::iterator* current_rel,
std::vector<std::unique_ptr<TypedRVA>>::iterator end_rel,
AssemblyProgram* program) {
// Walk all the bytes in the file, whether or not in a section.
FileOffset file_offset = section_header->sh_offset;
Expand Down
27 changes: 15 additions & 12 deletions courgette/disassembler_elf_32.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <vector>

#include "base/macros.h"
#include "base/memory/scoped_vector.h"
#include "courgette/disassembler.h"
#include "courgette/image_utils.h"
#include "courgette/memory_allocator.h"
Expand Down Expand Up @@ -46,9 +46,7 @@ class DisassemblerElf32 : public Disassembler {
void set_relative_target(RVA relative_target) {
relative_target_ = relative_target;
}
void set_file_offset(FileOffset file_offset) {
file_offset_ = file_offset;
}
void set_file_offset(FileOffset file_offset) { file_offset_ = file_offset; }

// Computes the relative jump's offset from the op in p.
virtual CheckBool ComputeRelativeTarget(const uint8_t* op_pointer) = 0;
Expand All @@ -61,12 +59,14 @@ class DisassemblerElf32 : public Disassembler {
virtual uint16_t op_size() const = 0;

// Comparator for sorting, which assumes uniqueness of RVAs.
static bool IsLessThanByRVA(TypedRVA* a, TypedRVA* b) {
static bool IsLessThanByRVA(const std::unique_ptr<TypedRVA>& a,
const std::unique_ptr<TypedRVA>& b) {
return a->rva() < b->rva();
}

// Comparator for sorting, which assumes uniqueness of file offsets.
static bool IsLessThanByFileOffset(TypedRVA* a, TypedRVA* b) {
static bool IsLessThanByFileOffset(const std::unique_ptr<TypedRVA>& a,
const std::unique_ptr<TypedRVA>& b) {
return a->file_offset() < b->file_offset();
}

Expand All @@ -92,8 +92,10 @@ class DisassemblerElf32 : public Disassembler {
virtual e_machine_values ElfEM() const = 0;

// Public for unittests only
std::vector<RVA> &Abs32Locations() { return abs32_locations_; }
ScopedVector<TypedRVA> &Rel32Locations() { return rel32_locations_; }
std::vector<RVA>& Abs32Locations() { return abs32_locations_; }
std::vector<std::unique_ptr<TypedRVA>>& Rel32Locations() {
return rel32_locations_;
}

protected:
bool UpdateLength();
Expand Down Expand Up @@ -139,7 +141,8 @@ class DisassemblerElf32 : public Disassembler {
CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas,
std::vector<FileOffset>* file_offsets);

CheckBool RVAsToFileOffsets(ScopedVector<TypedRVA>* typed_rvas);
CheckBool RVAsToFileOffsets(
std::vector<std::unique_ptr<TypedRVA>>* typed_rvas);

// Parsing code for Disassemble().

Expand All @@ -156,8 +159,8 @@ class DisassemblerElf32 : public Disassembler {
const Elf32_Shdr* section_header,
std::vector<FileOffset>::iterator* current_abs_offset,
std::vector<FileOffset>::iterator end_abs_offset,
ScopedVector<TypedRVA>::iterator* current_rel,
ScopedVector<TypedRVA>::iterator end_rel,
std::vector<std::unique_ptr<TypedRVA>>::iterator* current_rel,
std::vector<std::unique_ptr<TypedRVA>>::iterator end_rel,
AssemblyProgram* program) WARN_UNUSED_RESULT;

CheckBool ParseSimpleRegion(FileOffset start_file_offset,
Expand Down Expand Up @@ -189,7 +192,7 @@ class DisassemblerElf32 : public Disassembler {
size_t default_string_section_size_;

std::vector<RVA> abs32_locations_;
ScopedVector<TypedRVA> rel32_locations_;
std::vector<std::unique_ptr<TypedRVA>> rel32_locations_;

DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32);
};
Expand Down
3 changes: 2 additions & 1 deletion courgette/disassembler_elf_32_arm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "courgette/disassembler_elf_32_arm.h"

#include <memory>
#include <utility>
#include <vector>

#include "base/logging.h"
Expand Down Expand Up @@ -505,7 +506,7 @@ CheckBool DisassemblerElf32ARM::ParseRel32RelocsFromSection(

if (found && IsValidTargetRVA(target_rva)) {
uint16_t op_size = rel32_rva->op_size();
rel32_locations_.push_back(rel32_rva.release());
rel32_locations_.push_back(std::move(rel32_rva));
#if COURGETTE_HISTOGRAM_TARGETS
++rel32_target_rvas_[target_rva];
#endif
Expand Down
3 changes: 2 additions & 1 deletion courgette/disassembler_elf_32_x86.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "courgette/disassembler_elf_32_x86.h"

#include <memory>
#include <utility>
#include <vector>

#include "base/logging.h"
Expand Down Expand Up @@ -185,7 +186,7 @@ CheckBool DisassemblerElf32X86::ParseRel32RelocsFromSection(
RVA target_rva = typed_rel32_rva->rva() +
typed_rel32_rva->relative_target();
if (IsValidTargetRVA(target_rva)) {
rel32_locations_.push_back(typed_rel32_rva.release());
rel32_locations_.push_back(std::move(typed_rel32_rva));
#if COURGETTE_HISTOGRAM_TARGETS
++rel32_target_rvas_[target_rva];
#endif
Expand Down
3 changes: 1 addition & 2 deletions courgette/disassembler_elf_32_x86_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ class DisassemblerElf32X86Test : public BaseTest {
void DisassemblerElf32X86Test::TestExe(const char* file_name,
size_t expected_abs_count,
size_t expected_rel_count) const {
using TypedRVA = DisassemblerElf32::TypedRVA;
std::string file1 = FileContents(file_name);

std::unique_ptr<TestDisassemblerElf32X86> disassembler(
Expand Down Expand Up @@ -98,7 +97,7 @@ void DisassemblerElf32X86Test::TestExe(const char* file_name,
// Flatten the list typed rel32 to a list of rel32 RVAs.
std::vector<RVA> rel32_list;
rel32_list.reserve(disassembler->Rel32Locations().size());
for (TypedRVA* typed_rel32 : disassembler->Rel32Locations())
for (auto& typed_rel32 : disassembler->Rel32Locations())
rel32_list.push_back(typed_rel32->rva());

EXPECT_EQ(expected_abs_count, abs32_list.size());
Expand Down
36 changes: 13 additions & 23 deletions courgette/ensemble_apply.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <utility>

#include "base/files/file_util.h"
#include "base/files/memory_mapped_file.h"
#include "base/logging.h"
Expand All @@ -26,7 +29,7 @@ namespace courgette {
class EnsemblePatchApplication {
public:
EnsemblePatchApplication();
~EnsemblePatchApplication();
~EnsemblePatchApplication() = default;

Status ReadHeader(SourceStream* header_stream);

Expand Down Expand Up @@ -68,7 +71,7 @@ class EnsemblePatchApplication {
uint32_t target_checksum_;
uint32_t final_patch_input_size_prediction_;

std::vector<TransformationPatcher*> patchers_;
std::vector<std::unique_ptr<TransformationPatcher>> patchers_;

SinkStream corrected_parameters_storage_;
SinkStream corrected_elements_storage_;
Expand All @@ -81,12 +84,6 @@ EnsemblePatchApplication::EnsemblePatchApplication()
final_patch_input_size_prediction_(0) {
}

EnsemblePatchApplication::~EnsemblePatchApplication() {
for (size_t i = 0; i < patchers_.size(); ++i) {
delete patchers_[i];
}
}

Status EnsemblePatchApplication::ReadHeader(SourceStream* header_stream) {
uint32_t magic;
if (!header_stream->ReadVarint32(&magic))
Expand Down Expand Up @@ -138,28 +135,21 @@ Status EnsemblePatchApplication::ReadInitialParameters(
if (!transformation_parameters->ReadVarint32(&kind))
return C_BAD_ENSEMBLE_HEADER;

TransformationPatcher* patcher = NULL;
std::unique_ptr<TransformationPatcher> patcher;

switch (kind)
{
case EXE_WIN_32_X86:
patcher = new PatcherX86_32(base_region_);
break;
switch (kind) {
case EXE_WIN_32_X86: // Fall through.
case EXE_ELF_32_X86:
patcher = new PatcherX86_32(base_region_);
break;
case EXE_ELF_32_ARM:
patcher = new PatcherX86_32(base_region_);
break;
case EXE_WIN_32_X64:
patcher = new PatcherX86_32(base_region_);
patcher.reset(new PatcherX86_32(base_region_));
break;
default:
return C_BAD_ENSEMBLE_HEADER;
}

if (patcher)
patchers_.push_back(patcher);
else
return C_BAD_ENSEMBLE_HEADER;
DCHECK(patcher);
patchers_.push_back(std::move(patcher));
}

for (size_t i = 0; i < patchers_.size(); ++i) {
Expand Down

0 comments on commit 7d4e8ee

Please sign in to comment.