summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Rowe <mark@vector35.com>2025-12-10 15:20:25 -0800
committerMark Rowe <mark@vector35.com>2025-12-15 20:25:23 -0800
commit95b849d05d05ae9e55eb839508536d5af314d331 (patch)
treef06fc6ea5c13cc4972fe8b9fdd5c30d94779b045
parent2f1150703636b2b06d84bb66853fce7382bf6f29 (diff)
Use bn::base::function_ref instead of std::function for parameters that are not stored
This avoids the overhead of constructing a `std::function`, which may require a heap allocation, when calling functions that immediately invokes a function object.
-rw-r--r--genericrange.h15
-rw-r--r--highlevelilinstruction.cpp8
-rw-r--r--highlevelilinstruction.h19
-rw-r--r--lowlevelilinstruction.cpp4
-rw-r--r--lowlevelilinstruction.h12
-rw-r--r--mediumlevelilinstruction.cpp4
-rw-r--r--mediumlevelilinstruction.h12
7 files changed, 40 insertions, 34 deletions
diff --git a/genericrange.h b/genericrange.h
index 91854d16..413955a8 100644
--- a/genericrange.h
+++ b/genericrange.h
@@ -21,6 +21,8 @@
#pragma once
+#include "base/function_ref.h"
+
#ifdef BINARYNINJACORE_LIBRARY
#include "binaryninjacore_global.h"
namespace BinaryNinjaCore
@@ -157,16 +159,13 @@ using namespace std;
populateRangeMap();
}
- GenericRangeMap(const vector<GenericRange<T>>& ranges, std::function<void(vector<T>&)> orderingStrategy)
+ GenericRangeMap(const vector<GenericRange<T>>& ranges, bn::base::function_ref<void(vector<T>&)> orderingStrategy)
{
m_sourceRanges = ranges;
m_flattenedRanges = ranges;
flatten(m_flattenedRanges);
- if (orderingStrategy)
- {
- for (auto& i : m_flattenedRanges)
- orderingStrategy(i.GetMutableItems());
- }
+ for (auto& i : m_flattenedRanges)
+ orderingStrategy(i.GetMutableItems());
populateRangeMap();
}
@@ -206,7 +205,7 @@ using namespace std;
throw std::out_of_range("GenericRangeMap::GetMutableGenericRangeAt - Address not found in any range!");
}
- std::optional<std::pair<uint64_t, uint64_t>> GetNextValidRange(uint64_t addr, std::function<bool(const GenericRange<T>&)> predicate) const
+ std::optional<std::pair<uint64_t, uint64_t>> GetNextValidRange(uint64_t addr, bn::base::function_ref<bool(const GenericRange<T>&)> predicate) const
{
auto itr = m_rangeMap.upper_bound(addr);
if (itr != m_rangeMap.begin())
@@ -222,7 +221,7 @@ using namespace std;
return std::nullopt;
}
- std::optional<std::pair<uint64_t, uint64_t>> GetPreviousValidRange(uint64_t addr, std::function<bool(const GenericRange<T>&)> predicate) const
+ std::optional<std::pair<uint64_t, uint64_t>> GetPreviousValidRange(uint64_t addr, bn::base::function_ref<bool(const GenericRange<T>&)> predicate) const
{
auto itr = m_rangeMap.upper_bound(addr);
if (itr != m_rangeMap.begin())
diff --git a/highlevelilinstruction.cpp b/highlevelilinstruction.cpp
index 55df8d21..37504253 100644
--- a/highlevelilinstruction.cpp
+++ b/highlevelilinstruction.cpp
@@ -1342,7 +1342,7 @@ void HighLevelILInstruction::CollectSubExprs(stack<size_t>& toProcess) const
}
-void HighLevelILInstruction::VisitExprs(const std::function<bool(const HighLevelILInstruction& expr)>& func) const
+void HighLevelILInstruction::VisitExprs(bn::base::function_ref<bool(const HighLevelILInstruction& expr)> func) const
{
stack<size_t> toProcess;
toProcess.push(exprIndex);
@@ -1357,8 +1357,8 @@ void HighLevelILInstruction::VisitExprs(const std::function<bool(const HighLevel
}
-void HighLevelILInstruction::VisitExprs(const std::function<bool(const HighLevelILInstruction& expr)>& preFunc,
- const std::function<void(const HighLevelILInstruction& expr)>& postFunc) const
+void HighLevelILInstruction::VisitExprs(bn::base::function_ref<bool(const HighLevelILInstruction& expr)> preFunc,
+ bn::base::function_ref<void(const HighLevelILInstruction& expr)> postFunc) const
{
stack<std::pair<HighLevelILInstruction, stack<size_t>>> toProcess;
HighLevelILInstruction cur = *this;
@@ -1404,7 +1404,7 @@ ExprId HighLevelILInstruction::CopyTo(HighLevelILFunction* dest, const ILSourceL
ExprId HighLevelILInstruction::CopyTo(
- HighLevelILFunction* dest, const std::function<ExprId(const HighLevelILInstruction& subExpr)>& subExprHandler, const ILSourceLocation& sourceLocation) const
+ HighLevelILFunction* dest, bn::base::function_ref<ExprId(const HighLevelILInstruction& subExpr)> subExprHandler, const ILSourceLocation& sourceLocation) const
{
vector<ExprId> output, params;
diff --git a/highlevelilinstruction.h b/highlevelilinstruction.h
index 84afeb98..083fed5d 100644
--- a/highlevelilinstruction.h
+++ b/highlevelilinstruction.h
@@ -20,16 +20,19 @@
#pragma once
-#include <functional>
-#include <unordered_map>
-#include <vector>
+#include "base/function_ref.h"
+#include "mediumlevelilinstruction.h"
+
#ifdef BINARYNINJACORE_LIBRARY
#include "variable.h"
#include "ilsourcelocation.h"
#else
#include "binaryninjaapi.h"
#endif
-#include "mediumlevelilinstruction.h"
+
+#include <unordered_map>
+#include <vector>
+
#include <fmt/core.h>
#ifdef BINARYNINJACORE_LIBRARY
@@ -480,13 +483,13 @@ namespace BinaryNinja
HighLevelILInstruction(const HighLevelILInstructionBase& instr);
void CollectSubExprs(_STD_STACK<size_t>& toProcess) const;
- void VisitExprs(const std::function<bool(const HighLevelILInstruction& expr)>& func) const;
- void VisitExprs(const std::function<bool(const HighLevelILInstruction& expr)>& preFunc,
- const std::function<void(const HighLevelILInstruction& expr)>& postFunc) const;
+ void VisitExprs(bn::base::function_ref<bool(const HighLevelILInstruction& expr)> func) const;
+ void VisitExprs(bn::base::function_ref<bool(const HighLevelILInstruction& expr)> preFunc,
+ bn::base::function_ref<void(const HighLevelILInstruction& expr)> postFunc) const;
ExprId CopyTo(HighLevelILFunction* dest, const ILSourceLocation& sourceLocation = {}) const;
ExprId CopyTo(HighLevelILFunction* dest,
- const std::function<ExprId(const HighLevelILInstruction& subExpr)>& subExprHandler,
+ bn::base::function_ref<ExprId(const HighLevelILInstruction& subExpr)> subExprHandler,
const ILSourceLocation& sourceLocation = {}) const;
bool operator<(const HighLevelILInstruction& other) const;
diff --git a/lowlevelilinstruction.cpp b/lowlevelilinstruction.cpp
index 036ef093..975720c1 100644
--- a/lowlevelilinstruction.cpp
+++ b/lowlevelilinstruction.cpp
@@ -1885,7 +1885,7 @@ void LowLevelILInstructionBase::ClearAttribute(BNILInstructionAttribute attribut
}
-void LowLevelILInstruction::VisitExprs(const std::function<bool(const LowLevelILInstruction& expr)>& func) const
+void LowLevelILInstruction::VisitExprs(bn::base::function_ref<bool(const LowLevelILInstruction& expr)> func) const
{
if (!func(*this))
return;
@@ -2096,7 +2096,7 @@ ExprId LowLevelILInstruction::CopyTo(LowLevelILFunction* dest, const ILSourceLoc
ExprId LowLevelILInstruction::CopyTo(
- LowLevelILFunction* dest, const std::function<ExprId(const LowLevelILInstruction& subExpr)>& subExprHandler, const ILSourceLocation& sourceLocation) const
+ LowLevelILFunction* dest, bn::base::function_ref<ExprId(const LowLevelILInstruction& subExpr)> subExprHandler, const ILSourceLocation& sourceLocation) const
{
vector<ExprId> params;
BNLowLevelILLabel* labelA;
diff --git a/lowlevelilinstruction.h b/lowlevelilinstruction.h
index 37d0d30a..da2de8dc 100644
--- a/lowlevelilinstruction.h
+++ b/lowlevelilinstruction.h
@@ -20,9 +20,6 @@
#pragma once
-#include <functional>
-#include <unordered_map>
-#include <vector>
#ifdef BINARYNINJACORE_LIBRARY
#include "ilsourcelocation.h"
#include "type.h"
@@ -30,6 +27,11 @@
#include "binaryninjaapi.h"
#endif
+#include "base/function_ref.h"
+
+#include <unordered_map>
+#include <vector>
+
#ifdef BINARYNINJACORE_LIBRARY
namespace BinaryNinjaCore
#else
@@ -866,11 +868,11 @@ namespace BinaryNinja
LowLevelILFunction* func, const BNLowLevelILInstruction& instr, size_t expr, size_t instrIdx);
LowLevelILInstruction(const LowLevelILInstructionBase& instr);
- void VisitExprs(const std::function<bool(const LowLevelILInstruction& expr)>& func) const;
+ void VisitExprs(bn::base::function_ref<bool(const LowLevelILInstruction& expr)> func) const;
ExprId CopyTo(LowLevelILFunction* dest, const ILSourceLocation& sourceLocation = {}) const;
ExprId CopyTo(LowLevelILFunction* dest,
- const std::function<ExprId(const LowLevelILInstruction& subExpr)>& subExprHandler,
+ bn::base::function_ref<ExprId(const LowLevelILInstruction& subExpr)> subExprHandler,
const ILSourceLocation& sourceLocation = {}) const;
// Templated accessors for instruction operands, use these for efficient access to a known instruction
diff --git a/mediumlevelilinstruction.cpp b/mediumlevelilinstruction.cpp
index b1b9d7d6..00189313 100644
--- a/mediumlevelilinstruction.cpp
+++ b/mediumlevelilinstruction.cpp
@@ -1350,7 +1350,7 @@ void MediumLevelILInstructionBase::ClearAttribute(BNILInstructionAttribute attri
}
-void MediumLevelILInstruction::VisitExprs(const std::function<bool(const MediumLevelILInstruction& expr)>& func) const
+void MediumLevelILInstruction::VisitExprs(bn::base::function_ref<bool(const MediumLevelILInstruction& expr)> func) const
{
if (!func(*this))
return;
@@ -1569,7 +1569,7 @@ ExprId MediumLevelILInstruction::CopyTo(MediumLevelILFunction* dest, const ILSou
ExprId MediumLevelILInstruction::CopyTo(MediumLevelILFunction* dest,
- const std::function<ExprId(const MediumLevelILInstruction& subExpr)>& subExprHandler,
+ bn::base::function_ref<ExprId(const MediumLevelILInstruction& subExpr)> subExprHandler,
const ILSourceLocation& sourceLocation) const
{
vector<ExprId> params;
diff --git a/mediumlevelilinstruction.h b/mediumlevelilinstruction.h
index b3965a63..97e44a81 100644
--- a/mediumlevelilinstruction.h
+++ b/mediumlevelilinstruction.h
@@ -20,9 +20,6 @@
#pragma once
-#include <functional>
-#include <unordered_map>
-#include <vector>
#ifdef BINARYNINJACORE_LIBRARY
#include "constantdata.h"
#include "variable.h"
@@ -31,6 +28,11 @@
#include "binaryninjaapi.h"
#endif
+#include "base/function_ref.h"
+
+#include <unordered_map>
+#include <vector>
+
#ifdef BINARYNINJACORE_LIBRARY
namespace BinaryNinjaCore
#else
@@ -617,11 +619,11 @@ namespace BinaryNinja
MediumLevelILFunction* func, const BNMediumLevelILInstruction& instr, size_t expr, size_t instrIdx);
MediumLevelILInstruction(const MediumLevelILInstructionBase& instr);
- void VisitExprs(const std::function<bool(const MediumLevelILInstruction& expr)>& func) const;
+ void VisitExprs(bn::base::function_ref<bool(const MediumLevelILInstruction& expr)> func) const;
ExprId CopyTo(MediumLevelILFunction* dest, const ILSourceLocation& sourceLocation = {}) const;
ExprId CopyTo(MediumLevelILFunction* dest,
- const std::function<ExprId(const MediumLevelILInstruction& subExpr)>& subExprHandler,
+ bn::base::function_ref<ExprId(const MediumLevelILInstruction& subExpr)> subExprHandler,
const ILSourceLocation& sourceLocation = {}) const;
// Templated accessors for instruction operands, use these for efficient access to a known instruction