Skip to content

Commit 24bf0d9

Browse files
committed
add new option CS_OPT_MEM for cs_option(): this enable user-defined dynamic memory management. idea proposed by Pancake
1 parent 2b14fcd commit 24bf0d9

26 files changed

+86
-45
lines changed

MCInst.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void MCInst_insert(MCInst *inst, int index, MCOperand *Op)
3030
inst->Operands[index] = *Op;
3131
inst->size++;
3232

33-
free(Op);
33+
my_free(Op);
3434
}
3535

3636
void MCInst_setOpcode(MCInst *inst, unsigned Op)
@@ -71,7 +71,7 @@ int MCInst_addOperand(MCInst *inst, MCOperand *Op)
7171
return -1;
7272

7373
inst->Operands[inst->size] = *Op;
74-
free(Op);
74+
my_free(Op);
7575

7676
inst->size++;
7777

@@ -152,7 +152,7 @@ void MCOperand_setFPImm(MCOperand *op, double Val)
152152

153153
MCOperand *MCOperand_CreateReg(unsigned Reg)
154154
{
155-
MCOperand *op = malloc(sizeof(*op));
155+
MCOperand *op = my_malloc(sizeof(*op));
156156

157157
op->Kind = kRegister;
158158
op->RegVal = Reg;
@@ -162,7 +162,7 @@ MCOperand *MCOperand_CreateReg(unsigned Reg)
162162

163163
MCOperand *MCOperand_CreateImm(int64_t Val)
164164
{
165-
MCOperand *op = malloc(sizeof(*op));
165+
MCOperand *op = my_malloc(sizeof(*op));
166166

167167
op->Kind = kImmediate;
168168
op->ImmVal = Val;
@@ -172,7 +172,7 @@ MCOperand *MCOperand_CreateImm(int64_t Val)
172172

173173
MCOperand *MCOperand_CreateFPImm(double Val)
174174
{
175-
MCOperand *op = malloc(sizeof(*op));
175+
MCOperand *op = my_malloc(sizeof(*op));
176176

177177
op->Kind = kFPImmediate;
178178
op->FPImmVal = Val;

arch/AArch64/AArch64BaseInfo.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static bool compare_lower_str(char *s1, char *s2)
4444
*c = tolower((int) *c);
4545

4646
bool res = (strcmp(s1, lower) == 0);
47-
free(lower);
47+
my_free(lower);
4848

4949
return res;
5050
}
@@ -615,10 +615,10 @@ void SysRegMapper_toString(SysRegMapper *S, uint32_t Bits, bool *Valid, char *re
615615
int dummy = sprintf(result, "s3_%s_c%s_c%s_%s", Op1S, CRnS, CRmS, Op2S);
616616
(void)dummy;
617617

618-
free(Op1S);
619-
free(CRnS);
620-
free(CRmS);
621-
free(Op2S);
618+
my_free(Op1S);
619+
my_free(CRnS);
620+
my_free(CRmS);
621+
my_free(Op2S);
622622
}
623623

624624
static NamedImmMapper_Mapping TLBIPairs[] = {

arch/AArch64/AArch64GenAsmWriter.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -11759,7 +11759,7 @@ static bool printAliasInstr(MCInst *MI, SStream *OS, void *info)
1175911759
}
1176011760
}
1176111761

11762-
free(tmp);
11762+
my_free(tmp);
1176311763

1176411764
return true;
1176511765
}

arch/AArch64/AArch64InstPrinter.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ static void printVPRRegister(MCInst *MI, unsigned OpNo, SStream *O)
572572
char *Name = strdup(getRegisterName(Reg));
573573
Name[0] = 'v';
574574
SStream_concat(O, "%s", Name);
575-
free(Name);
575+
my_free(Name);
576576
if (MI->csh->detail) {
577577
MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].type = ARM64_OP_REG;
578578
MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].reg = Reg;
@@ -787,13 +787,13 @@ static void printVectorList(MCInst *MI, unsigned OpNum,
787787
SStream_concat(O, "%s%s", Name, LayoutStr);
788788
if (I != Count - 1)
789789
SStream_concat(O, ", ");
790-
free(Name);
790+
my_free(Name);
791791
}
792792
} else { // Print the register directly when NumVecs is 1.
793793
char *Name = strdup(getRegisterName(Reg));
794794
Name[0] = 'v';
795795
SStream_concat(O, "%s%s", Name, LayoutStr);
796-
free(Name);
796+
my_free(Name);
797797
}
798798
SStream_concat(O, "}");
799799
}
@@ -823,7 +823,7 @@ void AArch64_printInst(MCInst *MI, SStream *O, void *Info)
823823
unsigned int id = AArch64_map_insn(mnem);
824824
MCInst_setOpcode(MI, AArch64_get_insn_id2(id));
825825
MCInst_setOpcodePub(MI, id);
826-
free(mnem);
826+
my_free(mnem);
827827
} else
828828
AArch64InstPrinter_printInstruction(MI, O, Info);
829829
}

arch/AArch64/mapping.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3530,7 +3530,7 @@ arm64_reg AArch64_map_insn(const char *name)
35303530

35313531
void AArch64_free_cache(void)
35323532
{
3533-
free(insn_cache);
3533+
my_free(insn_cache);
35343534

35353535
insn_cache = NULL;
35363536
}

arch/AArch64/module.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* By Dang Hoang Vu <danghvu@gmail.com> 2013 */
33

44
#include "../../cs_priv.h"
5+
#include "../../utils.h"
56
#include "../../MCRegisterInfo.h"
67
#include "AArch64Disassembler.h"
78
#include "AArch64InstPrinter.h"
@@ -10,7 +11,7 @@
1011

1112
static cs_err init(cs_struct *ud)
1213
{
13-
MCRegisterInfo *mri = malloc(sizeof(*mri));
14+
MCRegisterInfo *mri = my_malloc(sizeof(*mri));
1415

1516
AArch64_init(mri);
1617
ud->printer = AArch64_printInst;

arch/ARM/ARMGenAsmWriter.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -8714,7 +8714,7 @@ static bool printAliasInstr(MCInst *MI, SStream *OS, void *info)
87148714
}
87158715
}
87168716

8717-
free(tmp);
8717+
my_free(tmp);
87188718

87198719
return true;
87208720
}

arch/ARM/ARMInstPrinter.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ void ARM_printInst(MCInst *MI, SStream *O, void *Info)
520520
NewReg = MCOperand_CreateReg(MCRegisterInfo_getMatchingSuperReg(MRI, Reg, ARM_gsub_0,
521521
MCRegisterInfo_getRegClass(MRI, ARM_GPRPairRegClassID)));
522522
MCInst_addOperand2(&NewMI, NewReg);
523-
free(NewReg);
523+
my_free(NewReg);
524524

525525
// Copy the rest operands into NewMI.
526526
unsigned i;

arch/ARM/mapping.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2804,7 +2804,7 @@ bool ARM_rel_branch(unsigned int id)
28042804

28052805
void ARM_free_cache(void)
28062806
{
2807-
free(insn_cache);
2807+
my_free(insn_cache);
28082808

28092809
insn_cache = NULL;
28102810
}

arch/ARM/module.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
static cs_err init(cs_struct *ud)
1212
{
13-
MCRegisterInfo *mri = malloc(sizeof(*mri));
13+
MCRegisterInfo *mri = my_malloc(sizeof(*mri));
1414

1515
ARM_init(mri);
1616

arch/Mips/MipsGenAsmWriter.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -4470,7 +4470,7 @@ static bool printAliasInstr(MCInst *MI, SStream *OS, void *info)
44704470
}
44714471
}
44724472

4473-
free(tmp);
4473+
my_free(tmp);
44744474

44754475
return true;
44764476
}

arch/Mips/MipsInstPrinter.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ void Mips_printInst(MCInst *MI, SStream *O, void *info)
175175
unsigned id = Mips_map_insn(mnem);
176176
MCInst_setOpcode(MI, Mips_get_insn_id2(id));
177177
MCInst_setOpcodePub(MI, id);
178-
free(mnem);
178+
my_free(mnem);
179179
}
180180

181181
switch (MCInst_getOpcode(MI)) {

arch/Mips/mapping.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2037,7 +2037,7 @@ mips_reg Mips_map_register(unsigned int r)
20372037

20382038
void Mips_free_cache(void)
20392039
{
2040-
free(insn_cache);
2040+
my_free(insn_cache);
20412041

20422042
insn_cache = NULL;
20432043
}

arch/Mips/module.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* By Dang Hoang Vu <danghvu@gmail.com> 2013 */
33

44
#include "../../cs_priv.h"
5+
#include "../../utils.h"
56
#include "../../MCRegisterInfo.h"
67
#include "MipsDisassembler.h"
78
#include "MipsInstPrinter.h"
@@ -10,7 +11,7 @@
1011

1112
static cs_err init(cs_struct *ud)
1213
{
13-
MCRegisterInfo *mri = malloc(sizeof(*mri));
14+
MCRegisterInfo *mri = my_malloc(sizeof(*mri));
1415

1516
Mips_init(mri);
1617
ud->printer = Mips_printInst;

arch/PowerPC/PPCGenAsmWriter.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -3479,7 +3479,7 @@ static bool printAliasInstr(MCInst *MI, SStream *OS, void *info)
34793479
}
34803480
}
34813481

3482-
free(tmp);
3482+
my_free(tmp);
34833483

34843484
return true;
34853485
}

arch/PowerPC/mapping.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,7 @@ ppc_reg PPC_map_register(unsigned int r)
15191519

15201520
void PPC_free_cache(void)
15211521
{
1522-
free(insn_cache);
1522+
my_free(insn_cache);
15231523

15241524
insn_cache = NULL;
15251525
}

arch/PowerPC/module.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013 */
33

44
#include "../../cs_priv.h"
5+
#include "../../utils.h"
56
#include "../../MCRegisterInfo.h"
67
#include "PPCDisassembler.h"
78
#include "PPCInstPrinter.h"
@@ -10,7 +11,7 @@
1011

1112
static cs_err init(cs_struct *ud)
1213
{
13-
MCRegisterInfo *mri = malloc(sizeof(*mri));
14+
MCRegisterInfo *mri = my_malloc(sizeof(*mri));
1415

1516
PPC_init(mri);
1617
ud->printer = PPC_printInst;

arch/X86/X86ATTInstPrinter.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ void X86_ATT_printInst(MCInst *MI, SStream *OS, void *info)
454454
*tab = '\0';
455455
// reflect the new insn name (alias) in the opcode
456456
MCInst_setOpcode(MI, X86_get_insn_id2(X86_map_insn(mnem)));
457-
free(mnem);
457+
my_free(mnem);
458458
} else
459459
printInstruction(MI, OS);
460460

arch/X86/X86GenAsmWriter.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -13397,7 +13397,7 @@ static bool printAliasInstr(MCInst *MI, SStream *OS)
1339713397
}
1339813398
}
1339913399

13400-
free(tmp);
13400+
my_free(tmp);
1340113401

1340213402
return true;
1340313403
}

arch/X86/X86GenAsmWriter1.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -12886,7 +12886,7 @@ static bool printAliasInstr(MCInst *MI, SStream *OS)
1288612886
}
1288712887
}
1288812888

12889-
free(tmp);
12889+
my_free(tmp);
1289012890

1289112891
return true;
1289212892
}

arch/X86/X86IntelInstPrinter.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ void X86_Intel_printInst(MCInst *MI, SStream *O, void *Info)
230230
*tab = '\0';
231231
// reflect the new insn name (alias) in the opcode
232232
MCInst_setOpcode(MI, X86_get_insn_id2(X86_map_insn(mnem)));
233-
free(mnem);
233+
my_free(mnem);
234234
} else
235235
printInstruction(MI, O);
236236

arch/X86/mapping.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -6642,6 +6642,6 @@ unsigned int X86_get_insn_id2(unsigned int id)
66426642

66436643
void X86_free_cache(void)
66446644
{
6645-
free(insn_cache);
6645+
my_free(insn_cache);
66466646
insn_cache = NULL;
66476647
}

0 commit comments

Comments
 (0)