Skip to content

Commit

Permalink
rename memory function pointer types to have cs_ prefix. also rename …
Browse files Browse the repository at this point in the history
…internal function pointers my_* to have cs_mem_ prefix - suggested by Pancake
  • Loading branch information
aquynh committed Jan 11, 2014
1 parent d0f201c commit a8eb7a5
Show file tree
Hide file tree
Showing 19 changed files with 63 additions and 63 deletions.
10 changes: 5 additions & 5 deletions MCInst.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void MCInst_insert(MCInst *inst, int index, MCOperand *Op)
inst->Operands[index] = *Op;
inst->size++;

my_free(Op);
cs_mem_free(Op);
}

void MCInst_setOpcode(MCInst *inst, unsigned Op)
Expand Down Expand Up @@ -71,7 +71,7 @@ int MCInst_addOperand(MCInst *inst, MCOperand *Op)
return -1;

inst->Operands[inst->size] = *Op;
my_free(Op);
cs_mem_free(Op);

inst->size++;

Expand Down Expand Up @@ -152,7 +152,7 @@ void MCOperand_setFPImm(MCOperand *op, double Val)

MCOperand *MCOperand_CreateReg(unsigned Reg)
{
MCOperand *op = my_malloc(sizeof(*op));
MCOperand *op = cs_mem_malloc(sizeof(*op));

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

MCOperand *MCOperand_CreateImm(int64_t Val)
{
MCOperand *op = my_malloc(sizeof(*op));
MCOperand *op = cs_mem_malloc(sizeof(*op));

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

MCOperand *MCOperand_CreateFPImm(double Val)
{
MCOperand *op = my_malloc(sizeof(*op));
MCOperand *op = cs_mem_malloc(sizeof(*op));

op->Kind = kFPImmediate;
op->FPImmVal = Val;
Expand Down
10 changes: 5 additions & 5 deletions arch/AArch64/AArch64BaseInfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static bool compare_lower_str(char *s1, char *s2)
*c = tolower((int) *c);

bool res = (strcmp(s1, lower) == 0);
my_free(lower);
cs_mem_free(lower);

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

my_free(Op1S);
my_free(CRnS);
my_free(CRmS);
my_free(Op2S);
cs_mem_free(Op1S);
cs_mem_free(CRnS);
cs_mem_free(CRmS);
cs_mem_free(Op2S);
}

static NamedImmMapper_Mapping TLBIPairs[] = {
Expand Down
2 changes: 1 addition & 1 deletion arch/AArch64/AArch64GenAsmWriter.inc
Original file line number Diff line number Diff line change
Expand Up @@ -11759,7 +11759,7 @@ static bool printAliasInstr(MCInst *MI, SStream *OS, void *info)
}
}

my_free(tmp);
cs_mem_free(tmp);

return true;
}
Expand Down
8 changes: 4 additions & 4 deletions arch/AArch64/AArch64InstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ static void printVPRRegister(MCInst *MI, unsigned OpNo, SStream *O)
char *Name = strdup(getRegisterName(Reg));
Name[0] = 'v';
SStream_concat(O, "%s", Name);
my_free(Name);
cs_mem_free(Name);
if (MI->csh->detail) {
MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].type = ARM64_OP_REG;
MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].reg = Reg;
Expand Down Expand Up @@ -786,13 +786,13 @@ static void printVectorList(MCInst *MI, unsigned OpNum,
SStream_concat(O, "%s%s", Name, LayoutStr);
if (I != Count - 1)
SStream_concat(O, ", ");
my_free(Name);
cs_mem_free(Name);
}
} else { // Print the register directly when NumVecs is 1.
char *Name = strdup(getRegisterName(Reg));
Name[0] = 'v';
SStream_concat(O, "%s%s", Name, LayoutStr);
my_free(Name);
cs_mem_free(Name);
}
SStream_concat(O, "}");
}
Expand Down Expand Up @@ -822,7 +822,7 @@ void AArch64_printInst(MCInst *MI, SStream *O, void *Info)
unsigned int id = AArch64_map_insn(mnem);
MCInst_setOpcode(MI, AArch64_get_insn_id2(id));
MCInst_setOpcodePub(MI, id);
my_free(mnem);
cs_mem_free(mnem);
} else
AArch64InstPrinter_printInstruction(MI, O, Info);
}
Expand Down
2 changes: 1 addition & 1 deletion arch/AArch64/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void enable_arm64() {}

static cs_err init(cs_struct *ud)
{
MCRegisterInfo *mri = my_malloc(sizeof(*mri));
MCRegisterInfo *mri = cs_mem_malloc(sizeof(*mri));

AArch64_init(mri);
ud->printer = AArch64_printInst;
Expand Down
2 changes: 1 addition & 1 deletion arch/ARM/ARMInstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ void ARM_printInst(MCInst *MI, SStream *O, void *Info)
NewReg = MCOperand_CreateReg(MCRegisterInfo_getMatchingSuperReg(MRI, Reg, ARM_gsub_0,
MCRegisterInfo_getRegClass(MRI, ARM_GPRPairRegClassID)));
MCInst_addOperand2(&NewMI, NewReg);
my_free(NewReg);
cs_mem_free(NewReg);

// Copy the rest operands into NewMI.
unsigned i;
Expand Down
2 changes: 1 addition & 1 deletion arch/ARM/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void enable_arm() {};

static cs_err init(cs_struct *ud)
{
MCRegisterInfo *mri = my_malloc(sizeof(*mri));
MCRegisterInfo *mri = cs_mem_malloc(sizeof(*mri));

ARM_init(mri);

Expand Down
2 changes: 1 addition & 1 deletion arch/Mips/MipsGenAsmWriter.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4470,7 +4470,7 @@ static bool printAliasInstr(MCInst *MI, SStream *OS, void *info)
}
}

my_free(tmp);
cs_mem_free(tmp);

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion arch/Mips/MipsInstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void Mips_printInst(MCInst *MI, SStream *O, void *info)
unsigned id = Mips_map_insn(mnem);
MCInst_setOpcode(MI, Mips_get_insn_id2(id));
MCInst_setOpcodePub(MI, id);
my_free(mnem);
cs_mem_free(mnem);
}

switch (MCInst_getOpcode(MI)) {
Expand Down
2 changes: 1 addition & 1 deletion arch/Mips/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void enable_mips() {};

static cs_err init(cs_struct *ud)
{
MCRegisterInfo *mri = my_malloc(sizeof(*mri));
MCRegisterInfo *mri = cs_mem_malloc(sizeof(*mri));

Mips_init(mri);
ud->printer = Mips_printInst;
Expand Down
2 changes: 1 addition & 1 deletion arch/PowerPC/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void enable_powerpc() {};

static cs_err init(cs_struct *ud)
{
MCRegisterInfo *mri = my_malloc(sizeof(*mri));
MCRegisterInfo *mri = cs_mem_malloc(sizeof(*mri));

PPC_init(mri);
ud->printer = PPC_printInst;
Expand Down
2 changes: 1 addition & 1 deletion arch/X86/X86ATTInstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ void X86_ATT_printInst(MCInst *MI, SStream *OS, void *info)
*tab = '\0';
// reflect the new insn name (alias) in the opcode
MCInst_setOpcode(MI, X86_get_insn_id2(X86_map_insn(mnem)));
my_free(mnem);
cs_mem_free(mnem);
} else
printInstruction(MI, OS);

Expand Down
2 changes: 1 addition & 1 deletion arch/X86/X86GenAsmWriter.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13397,7 +13397,7 @@ static bool printAliasInstr(MCInst *MI, SStream *OS)
}
}

my_free(tmp);
cs_mem_free(tmp);

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion arch/X86/X86GenAsmWriter1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12886,7 +12886,7 @@ static bool printAliasInstr(MCInst *MI, SStream *OS)
}
}

my_free(tmp);
cs_mem_free(tmp);

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion arch/X86/X86IntelInstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ void X86_Intel_printInst(MCInst *MI, SStream *O, void *Info)
*tab = '\0';
// reflect the new insn name (alias) in the opcode
MCInst_setOpcode(MI, X86_get_insn_id2(X86_map_insn(mnem)));
my_free(mnem);
cs_mem_free(mnem);
} else
printInstruction(MI, O);

Expand Down
48 changes: 24 additions & 24 deletions cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ void enable_construct()
unsigned int all_arch = 0;

#ifdef USE_SYS_DYN_MEM
malloc_t my_malloc = malloc;
calloc_t my_calloc = calloc;
realloc_t my_realloc = realloc;
free_t my_free = free;
cs_malloc_t cs_mem_malloc = malloc;
cs_calloc_t cs_mem_calloc = calloc;
cs_realloc_t cs_mem_realloc = realloc;
cs_free_t cs_mem_free = free;
#else
malloc_t my_malloc = NULL;
calloc_t my_calloc = NULL;
realloc_t my_realloc = NULL;
free_t my_free = NULL;
cs_malloc_t cs_mem_malloc = NULL;
cs_calloc_t cs_mem_calloc = NULL;
cs_realloc_t cs_mem_realloc = NULL;
cs_free_t cs_mem_free = NULL;
#endif

unsigned int cs_version(int *major, int *minor)
Expand Down Expand Up @@ -112,15 +112,15 @@ const char *cs_strerror(cs_err code)

cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
{
if (!my_malloc || !my_calloc || !my_realloc || !my_free)
if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free)
// Error: before cs_open(), dynamic memory management must be initialized
// with cs_option(CS_OPT_MEM)
return CS_ERR_MEMSETUP;

if (arch < CS_ARCH_MAX && arch_init[arch]) {
cs_struct *ud;

ud = my_calloc(1, sizeof(*ud));
ud = cs_mem_calloc(1, sizeof(*ud));
if (!ud) {
// memory insufficient
return CS_ERR_MEM;
Expand Down Expand Up @@ -158,17 +158,17 @@ cs_err cs_close(csh handle)
case CS_ARCH_MIPS:
case CS_ARCH_ARM64:
case CS_ARCH_PPC:
my_free(ud->printer_info);
cs_mem_free(ud->printer_info);
break;
default: // unsupported architecture
return CS_ERR_HANDLE;
}

// arch_destroy[ud->arch](ud);

my_free(ud->insn_cache);
cs_mem_free(ud->insn_cache);
memset(ud, 0, sizeof(*ud));
my_free(ud);
cs_mem_free(ud);

return CS_ERR_OK;
}
Expand Down Expand Up @@ -237,10 +237,10 @@ cs_err cs_option(csh ud, cs_opt_type type, size_t value)
if (type == CS_OPT_MEM) {
cs_opt_mem *mem = (cs_opt_mem *)value;

my_malloc = mem->malloc;
my_calloc = mem->calloc;
my_realloc = mem->realloc;
my_free = mem->free;
cs_mem_malloc = mem->malloc;
cs_mem_calloc = mem->calloc;
cs_mem_realloc = mem->realloc;
cs_mem_free = mem->free;

return CS_ERR_OK;
}
Expand Down Expand Up @@ -297,7 +297,7 @@ size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset,
mci.flat_insn.address = offset;
mci.flat_insn.size = insn_size;
// allocate memory for @detail pointer
insn_cache[f].detail = my_calloc(1, sizeof(cs_detail));
insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
}

handle->printer(&mci, &ss, handle->printer_info);
Expand All @@ -309,9 +309,9 @@ size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset,
if (f == ARR_SIZE(insn_cache)) {
// resize total to contain newly disasm insns
total_size += sizeof(insn_cache);
void *tmp = my_realloc(total, total_size);
void *tmp = cs_mem_realloc(total, total_size);
if (tmp == NULL) { // insufficient memory
my_free(total);
cs_mem_free(total);
handle->errnum = CS_ERR_MEM;
return 0;
}
Expand All @@ -338,9 +338,9 @@ size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset,

if (f) {
// resize total to contain newly disasm insns
void *tmp = my_realloc(total, total_size + f * sizeof(insn_cache[0]));
void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
if (tmp == NULL) { // insufficient memory
my_free(total);
cs_mem_free(total);
handle->errnum = CS_ERR_MEM;
return 0;
}
Expand All @@ -360,10 +360,10 @@ void cs_free(cs_insn *insn, size_t count)

// free all detail pointers
for (i = 0; i < count; i++)
my_free(insn[i].detail);
cs_mem_free(insn[i].detail);

// then free pointer to cs_insn array
my_free(insn);
cs_mem_free(insn);
}

// return friendly name of regiser in a string
Expand Down
8 changes: 4 additions & 4 deletions cs_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ extern void (*arch_destroy[MAX_ARCH]) (cs_struct*);

extern unsigned int all_arch;

extern malloc_t my_malloc;
extern calloc_t my_calloc;
extern realloc_t my_realloc;
extern free_t my_free;
extern cs_malloc_t cs_mem_malloc;
extern cs_calloc_t cs_mem_calloc;
extern cs_realloc_t cs_mem_realloc;
extern cs_free_t cs_mem_free;

#endif
16 changes: 8 additions & 8 deletions include/capstone.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ typedef enum cs_mode {
CS_MODE_BIG_ENDIAN = 1 << 31 // big endian mode
} cs_mode;

typedef void* (*malloc_t)(size_t size);
typedef void* (*calloc_t)(size_t nmemb, size_t size);
typedef void* (*realloc_t)(void *ptr, size_t size);
typedef void (*free_t)(void *ptr);
typedef void* (*cs_malloc_t)(size_t size);
typedef void* (*cs_calloc_t)(size_t nmemb, size_t size);
typedef void* (*cs_realloc_t)(void *ptr, size_t size);
typedef void (*cs_free_t)(void *ptr);

// User-defined memory malloc/calloc/realloc/free functions
// These functions will be used internally to dynamically manage memory.
// By default, Capstone uses system's malloc(), calloc(), realloc() & free().
typedef struct cs_opt_mem {
malloc_t malloc;
calloc_t calloc;
realloc_t realloc;
free_t free;
cs_malloc_t malloc;
cs_calloc_t calloc;
cs_realloc_t realloc;
cs_free_t free;
} cs_opt_mem;

// Runtime option for the disassembled engine
Expand Down
2 changes: 1 addition & 1 deletion utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static unsigned short *make_id2insn(insn_map *insns, unsigned int size)
unsigned short max_id = insns[size - 1].id;
unsigned int i;

unsigned short *cache = (unsigned short *)my_calloc(sizeof(*cache), max_id + 1);
unsigned short *cache = (unsigned short *)cs_mem_calloc(sizeof(*cache), max_id + 1);

for (i = 1; i < size; i++)
cache[insns[i].id] = i;
Expand Down

0 comments on commit a8eb7a5

Please sign in to comment.