From 1df49afa5522d73d509d20db9588294fe259085f Mon Sep 17 00:00:00 2001 From: Brian Potchik Date: Thu, 11 Jan 2018 17:10:34 -0500 Subject: API Temporary Object Elimination. --- architecture.cpp | 4 ++-- binaryninjaapi.h | 4 ++++ binaryview.cpp | 22 +++++----------------- function.cpp | 49 ++++++++++++++++++++++++------------------------- lowlevelil.cpp | 24 ++++-------------------- mediumlevelil.cpp | 24 ++++-------------------- plugin.cpp | 2 +- settings.cpp | 4 ++-- type.cpp | 36 ++++++------------------------------ 9 files changed, 52 insertions(+), 117 deletions(-) diff --git a/architecture.cpp b/architecture.cpp index 66372bcb..821f4c7b 100644 --- a/architecture.cpp +++ b/architecture.cpp @@ -959,8 +959,8 @@ bool CoreArchitecture::GetInstructionText(const uint8_t* data, uint64_t addr, si for (size_t i = 0; i < count; i++) { - result.push_back(InstructionTextToken(tokens[i].type, tokens[i].context, tokens[i].text, tokens[i].address, - tokens[i].value, tokens[i].size, tokens[i].operand, tokens[i].confidence)); + result.emplace_back(tokens[i].type, tokens[i].context, tokens[i].text, tokens[i].address, + tokens[i].value, tokens[i].size, tokens[i].operand, tokens[i].confidence); } BNFreeInstructionText(tokens, count); diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 72f74c5a..31bd2316 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -1017,6 +1017,9 @@ namespace BinaryNinja struct DataVariable { + DataVariable() { } + DataVariable(uint64_t a, Type* t, bool d) : address(a), type(t), autoDiscovered(d) { } + uint64_t address; Confidence> type; bool autoDiscovered; @@ -1832,6 +1835,7 @@ namespace BinaryNinja { Variable(); Variable(BNVariableSourceType type, uint32_t index, uint64_t storage); + Variable(BNVariableSourceType type, uint64_t storage); Variable(const BNVariable& var); Variable& operator=(const Variable& var); diff --git a/binaryview.cpp b/binaryview.cpp index 50011a0f..00cd294f 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -82,10 +82,7 @@ void BinaryDataNotification::DataVariableAddedCallback(void* ctxt, BNBinaryView* { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); - DataVariable varObj; - varObj.address = var->address; - varObj.type = Confidence>(new Type(BNNewTypeReference(var->type)), var->typeConfidence); - varObj.autoDiscovered = var->autoDiscovered; + DataVariable varObj(var->address, Confidence>(new Type(BNNewTypeReference(var->type)), var->typeConfidence), var->autoDiscovered); notify->OnDataVariableAdded(view, varObj); } @@ -94,10 +91,7 @@ void BinaryDataNotification::DataVariableRemovedCallback(void* ctxt, BNBinaryVie { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); - DataVariable varObj; - varObj.address = var->address; - varObj.type = Confidence>(new Type(BNNewTypeReference(var->type)), var->typeConfidence); - varObj.autoDiscovered = var->autoDiscovered; + DataVariable varObj(var->address, Confidence>(new Type(BNNewTypeReference(var->type)), var->typeConfidence), var->autoDiscovered); notify->OnDataVariableRemoved(view, varObj); } @@ -106,10 +100,7 @@ void BinaryDataNotification::DataVariableUpdatedCallback(void* ctxt, BNBinaryVie { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); - DataVariable varObj; - varObj.address = var->address; - varObj.type = Confidence>(new Type(BNNewTypeReference(var->type)), var->typeConfidence); - varObj.autoDiscovered = var->autoDiscovered; + DataVariable varObj(var->address, Confidence>(new Type(BNNewTypeReference(var->type)), var->typeConfidence), var->autoDiscovered); notify->OnDataVariableUpdated(view, varObj); } @@ -965,11 +956,8 @@ map BinaryView::GetDataVariables() map result; for (size_t i = 0; i < count; i++) { - DataVariable var; - var.address = vars[i].address; - var.type = Confidence>(new Type(BNNewTypeReference(vars[i].type)), vars[i].typeConfidence); - var.autoDiscovered = vars[i].autoDiscovered; - result[var.address] = var; + result.emplace(piecewise_construct, forward_as_tuple(vars[i].address), + forward_as_tuple(vars[i].address, Confidence>(new Type(BNNewTypeReference(vars[i].type)), vars[i].typeConfidence), vars[i].autoDiscovered)); } BNFreeDataVariables(vars, count); diff --git a/function.cpp b/function.cpp index 2d8db04c..1226e399 100644 --- a/function.cpp +++ b/function.cpp @@ -504,11 +504,7 @@ Confidence> Function::GetParameterVariables() const vector varList; for (size_t i = 0; i < vars.count; i++) { - Variable var; - var.type = vars.vars[i].type; - var.index = vars.vars[i].index; - var.storage = vars.vars[i].storage; - varList.push_back(var); + varList.emplace_back(vars.vars[i].type, vars.vars[i].index, vars.vars[i].storage); } Confidence> result(varList, vars.confidence); BNFreeParameterVariables(&vars); @@ -569,13 +565,14 @@ void Function::SetAutoCallingConvention(const Confidence> void Function::SetAutoParameterVariables(const Confidence>& vars) { BNParameterVariablesWithConfidence varConf; - varConf.vars = new BNVariable[vars.GetValue().size()]; - varConf.count = vars.GetValue().size(); - for (size_t i = 0; i < vars.GetValue().size(); i++) + varConf.vars = new BNVariable[vars->size()]; + varConf.count = vars->size(); + size_t i = 0; + for (auto it = vars->begin(); it != vars->end(); ++it, ++i) { - varConf.vars[i].type = vars.GetValue()[i].type; - varConf.vars[i].index = vars.GetValue()[i].index; - varConf.vars[i].storage = vars.GetValue()[i].storage; + varConf.vars[i].type = it->type; + varConf.vars[i].index = it->index; + varConf.vars[i].storage = it->storage; } varConf.confidence = vars.GetConfidence(); @@ -614,11 +611,12 @@ void Function::SetAutoStackAdjustment(const Confidence& stackAdjust) void Function::SetAutoClobberedRegisters(const Confidence>& clobbered) { BNRegisterSetWithConfidence regs; - regs.regs = new uint32_t[clobbered.GetValue().size()]; - regs.count = clobbered.GetValue().size(); + regs.regs = new uint32_t[clobbered->size()]; + regs.count = clobbered->size(); + size_t i = 0; - for (auto reg : clobbered.GetValue()) - regs.regs[i++] = reg; + for (auto it = clobbered->begin(); it != clobbered->end(); ++it, ++i) + regs.regs[i] = *it; regs.confidence = clobbered.GetConfidence(); BNSetAutoFunctionClobberedRegisters(m_object, ®s); delete[] regs.regs; @@ -652,13 +650,14 @@ void Function::SetCallingConvention(const Confidence>& co void Function::SetParameterVariables(const Confidence>& vars) { BNParameterVariablesWithConfidence varConf; - varConf.vars = new BNVariable[vars.GetValue().size()]; - varConf.count = vars.GetValue().size(); - for (size_t i = 0; i < vars.GetValue().size(); i++) + varConf.vars = new BNVariable[vars->size()]; + varConf.count = vars->size(); + size_t i = 0; + for (auto it = vars->begin(); it != vars->end(); ++it, ++i) { - varConf.vars[i].type = vars.GetValue()[i].type; - varConf.vars[i].index = vars.GetValue()[i].index; - varConf.vars[i].storage = vars.GetValue()[i].storage; + varConf.vars[i].type = it->type; + varConf.vars[i].index = it->index; + varConf.vars[i].storage = it->storage; } varConf.confidence = vars.GetConfidence(); @@ -697,11 +696,11 @@ void Function::SetStackAdjustment(const Confidence& stackAdjust) void Function::SetClobberedRegisters(const Confidence>& clobbered) { BNRegisterSetWithConfidence regs; - regs.regs = new uint32_t[clobbered.GetValue().size()]; - regs.count = clobbered.GetValue().size(); + regs.regs = new uint32_t[clobbered->size()]; + regs.count = clobbered->size(); size_t i = 0; - for (auto reg : clobbered.GetValue()) - regs.regs[i++] = reg; + for (auto it = clobbered->begin(); it != clobbered->end(); ++it, ++i) + regs.regs[i] = *it; regs.confidence = clobbered.GetConfidence(); BNSetUserFunctionClobberedRegisters(m_object, ®s); delete[] regs.regs; diff --git a/lowlevelil.cpp b/lowlevelil.cpp index 690f0b41..6c59b704 100644 --- a/lowlevelil.cpp +++ b/lowlevelil.cpp @@ -392,16 +392,8 @@ bool LowLevelILFunction::GetExprText(Architecture* arch, ExprId expr, vector PluginCommand::GetList() size_t count; BNPluginCommand* commands = BNGetAllPluginCommands(&count); for (size_t i = 0; i < count; i++) - result.push_back(PluginCommand(commands[i])); + result.emplace_back(commands[i]); BNFreePluginCommandList(commands); return result; } diff --git a/settings.cpp b/settings.cpp index 328cf0e3..6b9c4e3a 100644 --- a/settings.cpp +++ b/settings.cpp @@ -52,7 +52,7 @@ std::vector Setting::GetStringList(const std::string& pluginName, vector result; for (size_t i = 0; i < size; i++) - result.push_back(string(outBuffer[i])); + result.emplace_back(outBuffer[i]); for (size_t i = 0; i < defaultValue.size(); i++) BNFreeString(buffer[i]); @@ -171,4 +171,4 @@ bool Setting::RemoveSetting(const std::string& settingGroup, const std::string& bool Setting::FlushSettings() { return BNSettingFlushSettings(); -} \ No newline at end of file +} diff --git a/type.cpp b/type.cpp index 40a69ae2..c158f53e 100644 --- a/type.cpp +++ b/type.cpp @@ -476,16 +476,8 @@ vector Type::GetTokens(Platform* platform, uint8_t baseCon vector result; for (size_t i = 0; i < count; i++) { - InstructionTextToken token; - token.type = tokens[i].type; - token.text = tokens[i].text; - token.value = tokens[i].value; - token.size = tokens[i].size; - token.operand = tokens[i].operand; - token.context = tokens[i].context; - token.confidence = tokens[i].confidence; - token.address = tokens[i].address; - result.push_back(token); + result.emplace_back(tokens[i].type, tokens[i].context, tokens[i].text, tokens[i].address, tokens[i].value, tokens[i].size, + tokens[i].operand, tokens[i].confidence); } BNFreeTokenList(tokens, count); @@ -502,16 +494,8 @@ vector Type::GetTokensBeforeName(Platform* platform, uint8 vector result; for (size_t i = 0; i < count; i++) { - InstructionTextToken token; - token.type = tokens[i].type; - token.text = tokens[i].text; - token.value = tokens[i].value; - token.size = tokens[i].size; - token.operand = tokens[i].operand; - token.context = tokens[i].context; - token.confidence = tokens[i].confidence; - token.address = tokens[i].address; - result.push_back(token); + result.emplace_back(tokens[i].type, tokens[i].context, tokens[i].text, tokens[i].address, tokens[i].value, tokens[i].size, + tokens[i].operand, tokens[i].confidence); } BNFreeTokenList(tokens, count); @@ -528,16 +512,8 @@ vector Type::GetTokensAfterName(Platform* platform, uint8_ vector result; for (size_t i = 0; i < count; i++) { - InstructionTextToken token; - token.type = tokens[i].type; - token.text = tokens[i].text; - token.value = tokens[i].value; - token.size = tokens[i].size; - token.operand = tokens[i].operand; - token.context = tokens[i].context; - token.confidence = tokens[i].confidence; - token.address = tokens[i].address; - result.push_back(token); + result.emplace_back(tokens[i].type, tokens[i].context, tokens[i].text, tokens[i].address, tokens[i].value, tokens[i].size, + tokens[i].operand, tokens[i].confidence); } BNFreeTokenList(tokens, count); -- cgit v1.3.1