Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Unified Diff: courgette/assembly_program.cc

Issue 1629703002: [Courgette] Refactor: Manage AssemblyProgram and EncodedProgram with scoped_ptr. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix courgette_fuzzer in libfuzzer. Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « courgette/assembly_program.h ('k') | courgette/courgette.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: courgette/assembly_program.cc
diff --git a/courgette/assembly_program.cc b/courgette/assembly_program.cc
index 20eee96ff5e04b5e0d1e77642526b6d7c7698a4e..de843f1329050e46d10da2cbb4eef90a74d7711a 100644
--- a/courgette/assembly_program.cc
+++ b/courgette/assembly_program.cc
@@ -7,16 +7,12 @@
#include <memory.h>
#include <stddef.h>
#include <stdint.h>
-#include <algorithm>
-#include <map>
-#include <set>
-#include <sstream>
+
+#include <utility>
#include <vector>
#include "base/logging.h"
#include "base/macros.h"
-#include "base/memory/scoped_ptr.h"
-
#include "courgette/courgette.h"
#include "courgette/encoded_program.h"
@@ -365,13 +361,13 @@ void AssemblyProgram::AssignRemainingIndexes(RVAToLabel* labels) {
<< " infill " << fill_infill_count;
}
-EncodedProgram* AssemblyProgram::Encode() const {
+scoped_ptr<EncodedProgram> AssemblyProgram::Encode() const {
scoped_ptr<EncodedProgram> encoded(new EncodedProgram());
encoded->set_image_base(image_base_);
if (!encoded->DefineLabels(abs32_labels_, rel32_labels_))
- return NULL;
+ return nullptr;
for (size_t i = 0; i < instructions_.size(); ++i) {
Instruction* instruction = instructions_[i];
@@ -380,13 +376,13 @@ EncodedProgram* AssemblyProgram::Encode() const {
case ORIGIN: {
OriginInstruction* org = static_cast<OriginInstruction*>(instruction);
if (!encoded->AddOrigin(org->origin_rva()))
- return NULL;
+ return nullptr;
break;
}
case DEFBYTE: {
uint8_t b = static_cast<ByteInstruction*>(instruction)->byte_value();
if (!encoded->AddCopy(1, &b))
- return NULL;
+ return nullptr;
break;
}
case DEFBYTES: {
@@ -395,13 +391,13 @@ EncodedProgram* AssemblyProgram::Encode() const {
size_t len = static_cast<BytesInstruction*>(instruction)->len();
if (!encoded->AddCopy(len, byte_values))
- return NULL;
+ return nullptr;
break;
}
case REL32: {
Label* label = static_cast<InstructionWithLabel*>(instruction)->label();
if (!encoded->AddRel32(label->index_))
- return NULL;
+ return nullptr;
break;
}
case REL32ARM: {
@@ -410,34 +406,34 @@ EncodedProgram* AssemblyProgram::Encode() const {
uint16_t compressed_op =
static_cast<InstructionWithLabelARM*>(instruction)->compressed_op();
if (!encoded->AddRel32ARM(compressed_op, label->index_))
- return NULL;
+ return nullptr;
break;
}
case ABS32: {
Label* label = static_cast<InstructionWithLabel*>(instruction)->label();
if (!encoded->AddAbs32(label->index_))
- return NULL;
+ return nullptr;
break;
}
case ABS64: {
Label* label = static_cast<InstructionWithLabel*>(instruction)->label();
if (!encoded->AddAbs64(label->index_))
- return NULL;
+ return nullptr;
break;
}
case MAKEPERELOCS: {
if (!encoded->AddPeMakeRelocs(kind_))
- return NULL;
+ return nullptr;
break;
}
case MAKEELFRELOCS: {
if (!encoded->AddElfMakeRelocs())
- return NULL;
+ return nullptr;
break;
}
case MAKEELFARMRELOCS: {
if (!encoded->AddElfARMMakeRelocs())
- return NULL;
+ return nullptr;
break;
}
default: {
@@ -446,7 +442,7 @@ EncodedProgram* AssemblyProgram::Encode() const {
}
}
- return encoded.release();
+ return encoded;
}
Instruction* AssemblyProgram::GetByteInstruction(uint8_t byte) {
@@ -530,15 +526,13 @@ CheckBool AssemblyProgram::TrimLabels() {
////////////////////////////////////////////////////////////////////////////////
-Status Encode(AssemblyProgram* program, EncodedProgram** output) {
- *output = NULL;
- EncodedProgram *encoded = program->Encode();
- if (encoded) {
- *output = encoded;
- return C_OK;
- } else {
- return C_GENERAL_ERROR;
- }
+Status Encode(const AssemblyProgram& program,
+ scoped_ptr<EncodedProgram>* output) {
+ // Explicitly release any memory associated with the output before encoding.
+ output->reset();
+
+ *output = program.Encode();
+ return (*output) ? C_OK : C_GENERAL_ERROR;
}
} // namespace courgette
« no previous file with comments | « courgette/assembly_program.h ('k') | courgette/courgette.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698