x86: avoid malloc/free MCOperand with new API of MCInst: MCInst_addOperand0, MCInst_CreateReg0, MCInst_CreateImm0
diff --git a/MCInst.c b/MCInst.c
index d58f487..c459171 100644
--- a/MCInst.c
+++ b/MCInst.c
@@ -8,6 +8,8 @@
#include "MCInst.h"
#include "utils.h"
+#define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1)
+
void MCInst_Init(MCInst *inst)
{
memset(inst, 0, sizeof(*inst));
@@ -33,6 +35,18 @@
cs_mem_free(Op);
}
+void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
+{
+ int i;
+
+ for(i = inst->size; i > index; i--)
+ //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
+ inst->Operands[i] = inst->Operands[i-1];
+
+ inst->Operands[index] = *Op;
+ inst->size++;
+}
+
void MCInst_setOpcode(MCInst *inst, unsigned Op)
{
inst->Opcode = Op;
@@ -78,6 +92,19 @@
return 0;
}
+int MCInst_addOperand0(MCInst *inst, MCOperand *Op)
+{
+ if (inst->size == ARR_SIZE(inst->Operands))
+ // full
+ return -1;
+
+ inst->Operands[inst->size] = *Op;
+
+ inst->size++;
+
+ return 0;
+}
+
// This addOperand2 function doesnt free Op
int MCInst_addOperand2(MCInst *inst, MCOperand *Op)
{
@@ -160,6 +187,16 @@
return op;
}
+MCOperand *MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
+{
+ MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
+
+ op->Kind = kRegister;
+ op->RegVal = Reg;
+
+ return op;
+}
+
MCOperand *MCOperand_CreateImm(int64_t Val)
{
MCOperand *op = cs_mem_malloc(sizeof(*op));
@@ -170,6 +207,16 @@
return op;
}
+MCOperand *MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
+{
+ MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
+
+ op->Kind = kImmediate;
+ op->ImmVal = Val;
+
+ return op;
+}
+
MCOperand *MCOperand_CreateFPImm(double Val)
{
MCOperand *op = cs_mem_malloc(sizeof(*op));