Skip to content

Commit

Permalink
Add ht_pp_new_s2p() and ht_pp_new_s2s()
Browse files Browse the repository at this point in the history
  • Loading branch information
pelijah committed Apr 4, 2024
1 parent eeda6b1 commit 73d4b0b
Show file tree
Hide file tree
Showing 64 changed files with 190 additions and 287 deletions.
7 changes: 1 addition & 6 deletions binrz/rz-test/rz-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,6 @@ static int help(bool verbose) {
return 1;
}

static void path_left_free_kv(HtPPKv *kv) {
free(kv->key);
free(kv->value);
}

static bool rz_test_chdir(const char *argv0) {
#if __UNIX__
if (rz_file_is_directory("db")) {
Expand Down Expand Up @@ -517,7 +512,7 @@ int rz_test_main(int argc, const char **argv) {
if (log_mode) {
// Log mode prints the state after every completed file.
// The count of tests left per file is stored in a ht.
state.path_left = ht_pp_new(NULL, path_left_free_kv, NULL);
state.path_left = ht_pp_new_s2p(HT_STR_DUP, NULL, &free);
if (state.path_left) {
void **it;
rz_pvector_foreach (&state.queue, it) {
Expand Down
9 changes: 2 additions & 7 deletions librz/arch/analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ static void meta_item_free(void *item) {
free(it);
}

static void global_kv_free(HtPPKv *kv) {
free(kv->key);
rz_analysis_var_global_free(kv->value);
}

RZ_API RzAnalysis *rz_analysis_new(void) {
RzAnalysis *analysis = RZ_NEW0(RzAnalysis);
if (!analysis) {
Expand All @@ -87,7 +82,7 @@ RZ_API RzAnalysis *rz_analysis_new(void) {
}
analysis->bb_tree = NULL;
analysis->ht_addr_fun = ht_up_new0();
analysis->ht_name_fun = ht_pp_new0();
analysis->ht_name_fun = ht_pp_new_s2p(HT_STR_DUP, NULL, NULL);
analysis->os = strdup(RZ_SYS_OS);
analysis->esil_goto_limit = RZ_ANALYSIS_ESIL_GOTO_LIMIT;
analysis->opt.nopskip = true; // skip nops in code analysis
Expand Down Expand Up @@ -133,7 +128,7 @@ RZ_API RzAnalysis *rz_analysis_new(void) {
rz_analysis_plugin_add(analysis, plugin);
}
}
analysis->ht_global_var = ht_pp_new(NULL, global_kv_free, NULL);
analysis->ht_global_var = ht_pp_new_s2p(HT_STR_DUP, NULL, (HtPPFreeValue)rz_analysis_var_global_free);
analysis->global_var_tree = NULL;
analysis->il_vm = NULL;
analysis->hash = rz_hash_new();
Expand Down
11 changes: 1 addition & 10 deletions librz/arch/asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -835,15 +835,6 @@ RZ_API RzAsmCode *rz_asm_mdisassemble_hexstr(RzAsm *a, RzParse *p, const char *h
return ret;
}

static void __flag_free_kv(HtPPKv *kv) {
free(kv->key);
free(kv->value);
}

static void *__dup_val(const void *v) {
return (void *)strdup((char *)v);
}

RZ_API RzAsmCode *rz_asm_massemble(RzAsm *a, const char *assembly) {
int num, stage, ret, idx, ctr, i, linenum = 0;
char *lbuf = NULL, *ptr2, *ptr = NULL, *ptr_start = NULL;
Expand All @@ -863,7 +854,7 @@ RZ_API RzAsmCode *rz_asm_massemble(RzAsm *a, const char *assembly) {
return NULL;
}
ht_pp_free(a->flags);
if (!(a->flags = ht_pp_new(__dup_val, __flag_free_kv, NULL))) {
if (!(a->flags = ht_pp_new_s2s(HT_STR_DUP, HT_STR_DUP))) {
free(tokens);
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion librz/arch/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ typedef struct {
bool reachable;
} NoreturnSuccessor;

static void noreturn_successor_free(HtUPKv *kv) {
static void noreturn_successor_free(HtUPKv *kv, RZ_UNUSED void *user) {
NoreturnSuccessor *succ = kv->value;
rz_analysis_block_unref(succ->block);
free(succ);
Expand Down
2 changes: 1 addition & 1 deletion librz/arch/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@ RZ_API RzGraph /*<RzGraphNodeInfo *>*/ *rz_analysis_class_get_inheritance_graph(
rz_graph_free(class_graph);
return NULL;
}
HtPP /*<char *name, RzGraphNode *node>*/ *hashmap = ht_pp_new0();
HtPP /*<char *name, RzGraphNode *node>*/ *hashmap = ht_pp_new_s2p(HT_STR_DUP, NULL, NULL);
if (!hashmap) {
rz_graph_free(class_graph);
ls_free(classes);
Expand Down
12 changes: 2 additions & 10 deletions librz/arch/dwarf_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <string.h>

#define Ht_FREE_IMPL(V, T, f) \
static void Ht##V##_##T##_free(Ht##V##Kv *kv) { \
static void Ht##V##_##T##_free(Ht##V##Kv *kv, RZ_UNUSED void *user) { \
if (!kv) \
return; \
f(kv->value); \
Expand Down Expand Up @@ -2077,14 +2077,6 @@ Ht_FREE_IMPL(UP, RzBaseType, rz_type_base_type_free);
Ht_FREE_IMPL(UP, RzAnalysisDwarfFunction, function_free);
Ht_FREE_IMPL(UP, RzCallable, rz_type_callable_free);

static void HtPP_RzPVector_free(HtPPKv *kv) {
if (!kv) {
return;
}
free(kv->key);
rz_pvector_free(kv->value);
}

/**
* \brief Create a new debug info
* \return RzAnalysisDebugInfo pointer
Expand All @@ -2100,7 +2092,7 @@ RZ_API RzAnalysisDebugInfo *rz_analysis_debug_info_new() {
debug_info->type_by_offset = ht_up_new(NULL, HtUP_RzType_free, NULL);
debug_info->callable_by_offset = ht_up_new(NULL, HtUP_RzCallable_free, NULL);
debug_info->base_type_by_offset = ht_up_new(NULL, HtUP_RzBaseType_free, NULL);
debug_info->base_types_by_name = ht_pp_new(NULL, HtPP_RzPVector_free, NULL);
debug_info->base_types_by_name = ht_pp_new_s2p(HT_STR_DUP, NULL, (HtPPFreeValue)rz_pvector_free);
debug_info->visited = set_u_new();
return debug_info;
}
Expand Down
7 changes: 1 addition & 6 deletions librz/arch/esil/esil.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ static bool popRN(RzAnalysisEsil *esil, ut64 *n) {

/* RZ_ANALYSIS_ESIL API */

static void esil_ops_free(HtPPKv *kv) {
free(kv->key);
free(kv->value);
}

RZ_API RzAnalysisEsil *rz_analysis_esil_new(int stacksize, int iotrap, unsigned int addrsize) {
RzAnalysisEsil *esil = RZ_NEW0(RzAnalysisEsil);
if (!esil) {
Expand All @@ -98,7 +93,7 @@ RZ_API RzAnalysisEsil *rz_analysis_esil_new(int stacksize, int iotrap, unsigned
esil->verbose = false;
esil->stacksize = stacksize;
esil->parse_goto_count = RZ_ANALYSIS_ESIL_GOTO_LIMIT;
esil->ops = ht_pp_new(NULL, esil_ops_free, NULL);
esil->ops = ht_pp_new_s2p(HT_STR_DUP, NULL, free);
esil->iotrap = iotrap;
esil->in_cmd_step = false;
rz_analysis_esil_sources_init(esil);
Expand Down
2 changes: 1 addition & 1 deletion librz/arch/esil/esil_interrupt.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <rz_util.h>
#include <rz_lib.h>

static void interrupt_free(HtUPKv *kv) {
static void interrupt_free(HtUPKv *kv, RZ_UNUSED void *user) {
RzAnalysisEsilInterrupt *i = (RzAnalysisEsilInterrupt *)kv->value;
rz_analysis_esil_interrupt_free(i->esil, i);
}
Expand Down
2 changes: 1 addition & 1 deletion librz/arch/esil/esil_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static inline bool esil_add_reg_trace(RzAnalysisEsilTrace *etrace, RzILTraceRegO
return rz_analysis_il_trace_add_reg(instr_trace, reg);
}

static void htup_vector_free(HtUPKv *kv) {
static void htup_vector_free(HtUPKv *kv, RZ_UNUSED void *user) {
rz_vector_free(kv->value);
}

Expand Down
2 changes: 1 addition & 1 deletion librz/arch/fcn.c
Original file line number Diff line number Diff line change
Expand Up @@ -2265,7 +2265,7 @@ static bool analize_descendents(RzAnalysisBlock *bb, void *user) {
return rz_analysis_block_successor_addrs_foreach(bb, analize_addr_cb, user);
}

static void free_ht_up(HtUPKv *kv) {
static void free_ht_up(HtUPKv *kv, RZ_UNUSED void *user) {
ht_up_free((HtUP *)kv->value);
}

Expand Down
11 changes: 3 additions & 8 deletions librz/arch/function.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,11 @@ static bool function_already_defined_at(RzAnalysis *analysis, const char *name,
return false;
}

static void inst_vars_kv_free(HtUPKv *kv) {
static void inst_vars_kv_free(HtUPKv *kv, RZ_UNUSED void *user) {
rz_pvector_free(kv->value);
}

static void labels_kv_free(HtUPKv *kv) {
free(kv->value);
}

static void label_addrs_kv_free(HtPPKv *kv) {
free(kv->key);
static void labels_kv_free(HtUPKv *kv, RZ_UNUSED void *user) {
free(kv->value);
}

Expand All @@ -110,7 +105,7 @@ RZ_API RzAnalysisFunction *rz_analysis_function_new(RzAnalysis *analysis) {
rz_pvector_init(&fcn->vars, (RzPVectorFree)rz_analysis_var_free);
fcn->inst_vars = ht_up_new(NULL, inst_vars_kv_free, NULL);
fcn->labels = ht_up_new(NULL, labels_kv_free, NULL);
fcn->label_addrs = ht_pp_new(NULL, label_addrs_kv_free, NULL);
fcn->label_addrs = ht_pp_new_s2p(HT_STR_DUP, NULL, free);
return fcn;
}

Expand Down
2 changes: 1 addition & 1 deletion librz/arch/hint.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static void addr_hint_record_fini(void *element, void *user) {
}
}

static void addr_hint_record_ht_free(HtUPKv *kv) {
static void addr_hint_record_ht_free(HtUPKv *kv, RZ_UNUSED void *user) {
rz_vector_free(kv->value);
}

Expand Down
2 changes: 1 addition & 1 deletion librz/arch/il/analysis_il_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* 4. reg.write name & data
**/

static void htup_vector_free(HtUPKv *kv) {
static void htup_vector_free(HtUPKv *kv, RZ_UNUSED void *user) {
rz_vector_free(kv->value);
}

Expand Down
2 changes: 1 addition & 1 deletion librz/arch/platform_profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RZ_API void rz_platform_profile_free(RzPlatformProfile *p) {
free(p);
}

static void free_mmio_kv(HtUPKv *kv) {
static void free_mmio_kv(HtUPKv *kv, RZ_UNUSED void *user) {
free(kv->value);
}

Expand Down
2 changes: 1 addition & 1 deletion librz/arch/rtti_msvc.c
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ static const char *recovery_apply_type_descriptor(RRTTIMSVCAnalContext *context,
return name;
}

void str_value_free(HtUPKv *kv) {
void str_value_free(HtUPKv *kv, RZ_UNUSED void *user) {
free(kv->value);
}

Expand Down
2 changes: 1 addition & 1 deletion librz/arch/serialize_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -1759,7 +1759,7 @@ typedef struct {
bool bits_set;
} HintsAtAddr;

static void hints_at_addr_kv_free(HtUPKv *kv) {
static void hints_at_addr_kv_free(HtUPKv *kv, RZ_UNUSED void *user) {
free(kv->value);
}

Expand Down
4 changes: 2 additions & 2 deletions librz/arch/xrefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ RZ_API RZ_OWN RzList /*<RzAnalysisXRef *>*/ *rz_analysis_xref_list_new() {
return rz_list_newf((RzListFree)free);
}

static void xrefs_ht_free(HtUPKv *kv) {
static void xrefs_ht_free(HtUPKv *kv, RZ_UNUSED void *user) {
ht_up_free(kv->value);
}

static void xrefs_ref_free(HtUPKv *kv) {
static void xrefs_ref_free(HtUPKv *kv, RZ_UNUSED void *user) {
rz_analysis_xref_free(kv->value);
}

Expand Down
6 changes: 3 additions & 3 deletions librz/bin/bobj_process_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ RZ_IPI void rz_bin_set_and_process_classes(RzBinFile *bf, RzBinObject *o) {
ht_pp_free(o->glue_to_class_field);
ht_up_free(o->vaddr_to_class_method);

o->name_to_class_object = ht_pp_new0();
o->glue_to_class_method = ht_pp_new0();
o->glue_to_class_field = ht_pp_new0();
o->name_to_class_object = ht_pp_new_s2p(HT_STR_DUP, NULL, NULL);
o->glue_to_class_method = ht_pp_new_s2p(HT_STR_DUP, NULL, NULL);
o->glue_to_class_field = ht_pp_new_s2p(HT_STR_DUP, NULL, NULL);
o->vaddr_to_class_method = ht_up_new0();

void **it;
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/bobj_process_section.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ RZ_IPI void rz_bin_set_and_process_sections(RzBinFile *bf, RzBinObject *o) {
o->sections = rz_pvector_new((RzPVectorFree)rz_bin_section_free);
}

HtPP *filter_db = bin->filter ? ht_pp_new0() : NULL;
HtPP *filter_db = bin->filter ? ht_pp_new_s2p(HT_STR_DUP, NULL, NULL) : NULL;

void **it;
RzBinSection *element;
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/bobj_process_symbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ RZ_IPI void rz_bin_process_symbols(RzBinFile *bf, RzBinObject *o, const RzDemang
}

ht_pp_free(o->import_name_symbols);
o->import_name_symbols = ht_pp_new0();
o->import_name_symbols = ht_pp_new_s2p(HT_STR_DUP, NULL, NULL);

RzBinProcessLanguage language_cb = rz_bin_process_language_symbol(o);

Expand Down
2 changes: 1 addition & 1 deletion librz/bin/dwarf/abbrev.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static void RzBinDwarfAbbrevTable_free(RzBinDwarfAbbrevTable *table) {
free(table);
}

static void htup_RzBinDwarfAbbrevTable_free(HtUPKv *kv) {
static void htup_RzBinDwarfAbbrevTable_free(HtUPKv *kv, RZ_UNUSED void *user) {
if (!kv) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/dwarf/macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
#define SLE128_OR_GOTO(out, label) SLE128_OR(st64, out, goto label)

#define Ht_FREE_IMPL(V, T, f) \
static void Ht##V##_##T##_free(Ht##V##Kv *kv) { \
static void Ht##V##_##T##_free(Ht##V##Kv *kv, RZ_UNUSED void *user) { \
f(kv->value); \
}

Expand Down
2 changes: 1 addition & 1 deletion librz/bin/format/elf/elf_symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ static bool get_gnu_debugdata_elf_symbols(ELFOBJ *bin, RzVector /*<RzBinElfSymbo
goto debug_data_err;
}

HtPP *name_set = ht_pp_new0();
HtPP *name_set = ht_pp_new_s2p(HT_STR_DUP, NULL, NULL);
if (!name_set) {
goto debug_symbols_err;
}
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/format/mach0/mach0.c
Original file line number Diff line number Diff line change
Expand Up @@ -2521,7 +2521,7 @@ const struct symbol_t *MACH0_(get_symbols)(struct MACH0_(obj_t) * bin) {
return bin->symbols;
}

HtPP *hash = ht_pp_new0();
HtPP *hash = ht_pp_new_s2p(HT_STR_DUP, NULL, NULL);
if (!hash) {
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion librz/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ RZ_API RzConfig *rz_config_new(void *user) {
if (!cfg) {
return NULL;
}
cfg->ht = ht_pp_new0();
cfg->ht = ht_pp_new_s2p(HT_STR_DUP, NULL, NULL);
cfg->nodes = rz_list_newf((RzListFree)rz_config_node_free);
if (!cfg->nodes) {
RZ_FREE(cfg);
Expand Down
2 changes: 1 addition & 1 deletion librz/config/serialize_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ RZ_API bool rz_serialize_config_load(RZ_NONNULL Sdb *db, RZ_NONNULL RzConfig *co
RZ_NULLABLE const char *const *exclude, RZ_NULLABLE RzSerializeResultInfo *res) {
LoadConfigCtx ctx = { config, NULL };
if (exclude) {
ctx.exclude = ht_pp_new0();
ctx.exclude = ht_pp_new_s2p(HT_STR_DUP, NULL, NULL);
if (!ctx.exclude) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion librz/cons/canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static int __getAnsiPiece(const char *p, char *chr) {
return p - q;
}

static void attribute_free_kv(HtUPKv *kv) {
static void attribute_free_kv(HtUPKv *kv, RZ_UNUSED void *user) {
free(kv->value);
}

Expand Down
7 changes: 1 addition & 6 deletions librz/cons/hud.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,6 @@ static RzList /*<char *>*/ *hud_filter(RzList /*<char *>*/ *list, char *user_inp
return res;
}

static void mht_free_kv(HtPPKv *kv) {
free(kv->key);
rz_list_free(kv->value);
}

// Display a list of entries in the hud, filtered and emphasized based on the user input.

#define HUD_CACHE 0
Expand All @@ -204,7 +199,7 @@ RZ_API char *rz_cons_hud(RzList /*<char *>*/ *list, const char *prompt) {
char *selected_entry = NULL;
RzListIter *iter;

HtPP *ht = ht_pp_new(NULL, (HtPPKvFreeFunc)mht_free_kv, (HtPPCalcSizeV)strlen);
HtPP *ht = ht_pp_new_s2p(HT_STR_DUP, NULL, (HtPPFreeValue)rz_list_free);
RzLineHud *hud = (RzLineHud *)RZ_NEW(RzLineHud);
hud->activate = 0;
hud->vi = 0;
Expand Down
4 changes: 2 additions & 2 deletions librz/core/agraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ static int is_valid_pos(const RzAGraph *g, int l, int pos) {
return pos >= 0 && pos < g->layers[l].n_nodes;
}

static void free_vertical_nodes_kv(HtPPKv *kv) {
static void free_vertical_nodes_kv(HtPPKv *kv, RZ_UNUSED void *user) {
rz_list_free(kv->value);
}

Expand Down Expand Up @@ -3572,7 +3572,7 @@ static void agraph_toggle_speed(RzAGraph *g, RzCore *core) {
g->movspeed = g->movspeed == DEFAULT_SPEED ? alt : DEFAULT_SPEED;
}

static void free_nodes_kv(HtPPKv *kv) {
static void free_nodes_kv(HtPPKv *kv, RZ_UNUSED void *user) {
RzANode *n = (RzANode *)kv->value;
if (!n->is_dummy) {
agraph_node_free(n);
Expand Down
Loading

0 comments on commit 73d4b0b

Please sign in to comment.