Skip to content

Commit

Permalink
introduce user pointer in comparator to remove globals in table.c
Browse files Browse the repository at this point in the history
  • Loading branch information
wargio authored and XVilka committed Jan 6, 2024
1 parent a9131ce commit f7589fe
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 86 deletions.
4 changes: 2 additions & 2 deletions librz/asm/asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ static bool overlaps_with_token(RZ_BORROW RzVector /*<RzAsmTokenString>*/ *toks,
* \return 1 If a.start > b.start
* \return 0 If a.start == b.start
*/
static int cmp_tokens(const RzAsmToken *a, const RzAsmToken *b) {
static int cmp_tokens(const RzAsmToken *a, const RzAsmToken *b, void *user) {
rz_return_val_if_fail(a && b, 0);
if (a->start < b->start) {
return -1;
Expand Down Expand Up @@ -1591,7 +1591,7 @@ RZ_API RZ_OWN RzAsmTokenString *rz_asm_tokenize_asm_regex(RZ_BORROW RzStrBuf *as
}
}

rz_vector_sort(toks->tokens, (RzVectorComparator)cmp_tokens, false);
rz_vector_sort(toks->tokens, (RzVectorComparator)cmp_tokens, false, NULL);
check_token_coverage(toks);

return toks;
Expand Down
4 changes: 2 additions & 2 deletions librz/bin/bfile_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ static bool create_string_search_thread(RzThreadPool *pool, size_t min_length, R
return true;
}

static int string_compare_sort(const RzBinString *a, const RzBinString *b) {
static int string_compare_sort(const RzBinString *a, const RzBinString *b, void *user) {
if (b->paddr > a->paddr) {
return -1;
} else if (b->paddr < a->paddr) {
Expand Down Expand Up @@ -493,7 +493,7 @@ RZ_API RZ_OWN RzPVector /*<RzBinString *>*/ *rz_bin_file_strings(RZ_NONNULL RzBi
if (!raw_strings) {
scan_cfstring_table(bf, strings_db, results, max_interval);
}
rz_pvector_sort(results, (RzPVectorComparator)string_compare_sort);
rz_pvector_sort(results, (RzPVectorComparator)string_compare_sort, NULL);

{
void **it;
Expand Down
14 changes: 9 additions & 5 deletions librz/bin/bobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ RZ_API ut64 rz_bin_reloc_size(RzBinReloc *reloc) {
} \
} while (0);

static int reloc_cmp(const void *a, const void *b) {
static int reloc_cmp(const void *a, const void *b, void *user) {
const RzBinReloc *ar = a;
const RzBinReloc *br = b;
CMP_CHECK(vaddr);
Expand All @@ -83,7 +83,7 @@ static int reloc_cmp(const void *a, const void *b) {
return 0;
}

static int reloc_target_cmp(const void *a, const void *b) {
static int reloc_target_cmp(const void *a, const void *b, void *user) {
const RzBinReloc *ar = a;
const RzBinReloc *br = b;
CMP_CHECK(target_vaddr);
Expand Down Expand Up @@ -117,11 +117,11 @@ RZ_API RzBinRelocStorage *rz_bin_reloc_storage_new(RZ_OWN RzPVector /*<RzBinRelo
}
relocs->v.free = NULL; // ownership of relocs transferred
rz_pvector_free(relocs);
rz_pvector_sort(&sorter, reloc_cmp);
rz_pvector_sort(&sorter, reloc_cmp, NULL);
ret->relocs_count = rz_pvector_len(&sorter);
ret->relocs = (RzBinReloc **)rz_pvector_flush(&sorter);
rz_pvector_fini(&sorter);
rz_pvector_sort(&target_sorter, reloc_target_cmp);
rz_pvector_sort(&target_sorter, reloc_target_cmp, NULL);
ret->target_relocs_count = rz_pvector_len(&target_sorter);
ret->target_relocs = (RzBinReloc **)rz_pvector_flush(&target_sorter);
rz_pvector_fini(&target_sorter);
Expand Down Expand Up @@ -273,6 +273,10 @@ RZ_IPI int rz_bin_compare_class_field(RzBinClassField *a, RzBinClassField *b) {
return 0;
}

static int bin_compare_class(RzBinClass *a, RzBinClass *b, void *user) {
return rz_bin_compare_class(a, b);
}

/**
* \brief Tries to add a new class unless its name is found and returns it.
*
Expand Down Expand Up @@ -303,7 +307,7 @@ RZ_API RZ_BORROW RzBinClass *rz_bin_object_add_class(RZ_NONNULL RzBinObject *o,
}

rz_pvector_push(o->classes, oclass);
rz_pvector_sort(o->classes, (RzPVectorComparator)rz_bin_compare_class);
rz_pvector_sort(o->classes, (RzPVectorComparator)bin_compare_class, NULL);
ht_pp_insert(o->name_to_class_object, name, oclass);
return oclass;
}
Expand Down
4 changes: 2 additions & 2 deletions librz/bin/dbginfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ RZ_API void rz_bin_source_line_info_builder_push_sample(RzBinSourceLineInfoBuild
sample->file = file ? rz_str_constpool_get(&builder->filename_pool, file) : NULL;
}

static int line_sample_cmp(const void *a, const void *b) {
static int line_sample_cmp(const void *a, const void *b, void *user) {
const RzBinSourceLineSample *sa = a;
const RzBinSourceLineSample *sb = b;
// first, sort by addr
Expand Down Expand Up @@ -106,7 +106,7 @@ RZ_API RzBinSourceLineInfo *rz_bin_source_line_info_builder_build_and_fini(RzBin
for (size_t i = 0; i < initial_samples_count; i++) {
rz_pvector_push(&sorter, &initial_samples[i]);
}
rz_pvector_sort(&sorter, line_sample_cmp);
rz_pvector_sort(&sorter, line_sample_cmp, NULL);

r->samples_count = 0;
for (size_t i = 0; i < initial_samples_count; i++) {
Expand Down
4 changes: 2 additions & 2 deletions librz/bin/format/dex/dex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ RZ_API RZ_OWN RzPVector /*<RzBinImport *>*/ *rz_bin_dex_imports(RZ_NONNULL RzBin
return imports;
}

static int compare_strings(const void *a, const void *b) {
static int compare_strings(const void *a, const void *b, void *user) {
return strcmp((const char *)a, (const char *)b);
}

Expand Down Expand Up @@ -1574,7 +1574,7 @@ RZ_API RZ_OWN RzPVector /*<char *>*/ *rz_bin_dex_libraries(RZ_NONNULL RzBinDex *
object = p;
}

if (rz_pvector_find(libraries, object, compare_strings)) {
if (rz_pvector_find(libraries, object, compare_strings, NULL)) {
free(object);
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions librz/bin/format/java/class_bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -1884,7 +1884,7 @@ RZ_API RZ_OWN RzList /*<RzBinSection *>*/ *rz_bin_java_class_as_sections(RZ_NONN
return sections;
}

static int compare_strings(const void *a, const void *b) {
static int compare_strings(const void *a, const void *b, void *user) {
return strcmp((const char *)a, (const char *)b);
}

Expand Down Expand Up @@ -1930,7 +1930,7 @@ RZ_API RZ_OWN RzPVector /*<char *>*/ *rz_bin_java_class_as_libraries(RZ_NONNULL
// arg0 is name_index
tmp = java_class_constant_pool_stringify_at(bin, arg0);
}
if (tmp && !rz_pvector_find(vec, tmp, compare_strings)) {
if (tmp && !rz_pvector_find(vec, tmp, compare_strings, NULL)) {
rz_pvector_push(vec, tmp);
} else {
free(tmp);
Expand Down
4 changes: 2 additions & 2 deletions librz/bin/format/ne/ne.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ RzList /*<RzBinSection *>*/ *rz_bin_ne_get_segments(rz_bin_ne_obj_t *bin) {
return segments;
}

static int __find_symbol_by_paddr(const void *paddr, const void *sym) {
static int __find_symbol_by_paddr(const void *paddr, const void *sym, void *user) {
return (int)!(*(ut64 *)paddr == ((RzBinSymbol *)sym)->paddr);
}

Expand Down Expand Up @@ -184,7 +184,7 @@ RzPVector /*<RzBinSymbol *>*/ *rz_bin_ne_get_symbols(rz_bin_ne_obj_t *bin) {
RzBinAddr *en;
int i = 1;
rz_list_foreach (entries, it, en) {
if (!rz_pvector_find(symbols, &en->paddr, __find_symbol_by_paddr)) {
if (!rz_pvector_find(symbols, &en->paddr, __find_symbol_by_paddr, NULL)) {
sym = RZ_NEW0(RzBinSymbol);
if (!sym) {
break;
Expand Down
6 changes: 5 additions & 1 deletion librz/cons/pal.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ static inline ut8 rgbnum(const char ch1, const char ch2) {
return r << 4 | r2;
}

static int compare_strings(const char *s1, const char *s2, void *user) {
return strcmp(s1, s2);
}

static void __cons_pal_update_event(RzConsContext *ctx) {
RzPVector sorter;
rz_pvector_init(&sorter, NULL);
Expand All @@ -139,7 +143,7 @@ static void __cons_pal_update_event(RzConsContext *ctx) {
char *rgb = rz_str_newf("rgb:%02x%02x%02x", rcolor->r, rcolor->g, rcolor->b);
rz_pvector_push(&sorter, rgb);
}
rz_pvector_sort(&sorter, (RzPVectorComparator)strcmp);
rz_pvector_sort(&sorter, (RzPVectorComparator)compare_strings, NULL);
rz_cons_rainbow_free(ctx);
rz_cons_rainbow_new(ctx, rz_pvector_len(&sorter));
int n = 0;
Expand Down
4 changes: 2 additions & 2 deletions librz/core/cmd/cmd_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -2776,7 +2776,7 @@ RZ_IPI RzCmdStatus rz_analysis_function_vars_display_handler(RzCore *core, int a
return RZ_CMD_STATUS_OK;
}

static int delta_cmp(const void *a, const void *b) {
static int delta_cmp(const void *a, const void *b, void *user) {
const RzAnalysisVar *va = a;
const RzAnalysisVar *vb = b;
if (va->storage.type != vb->storage.type) {
Expand All @@ -2797,7 +2797,7 @@ RZ_IPI RzCmdStatus rz_analysis_function_vars_stackframe_handler(RzCore *core, in
if (!vars) {
return RZ_CMD_STATUS_ERROR;
}
rz_pvector_sort(vars, delta_cmp);
rz_pvector_sort(vars, delta_cmp, NULL);
void **it;
rz_pvector_foreach (vars, it) {
RzAnalysisVar *p = *it;
Expand Down
6 changes: 3 additions & 3 deletions librz/core/cmd/cmd_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ RZ_LIB_VERSION(rz_cmd);

static void fill_details(RzCmd *cmd, RzCmdDesc *cd, RzStrBuf *sb, bool use_color);

static int cd_sort(const void *a, const void *b) {
static int cd_sort(const void *a, const void *b, void *user) {
RzCmdDesc *ca = (RzCmdDesc *)a;
RzCmdDesc *cb = (RzCmdDesc *)b;
return rz_str_casecmp(ca->name, cb->name);
Expand All @@ -120,7 +120,7 @@ static bool cmd_desc_set_parent(RzCmd *cmd, RzCmdDesc *cd, RzCmdDesc *parent) {
cd->parent = parent;
rz_pvector_push(&parent->children, cd);
if (!cmd->batch && parent->help->sort_subcommands) {
rz_pvector_sort(&parent->children, cd_sort);
rz_pvector_sort(&parent->children, cd_sort, NULL);
}
parent->n_children++;
}
Expand Down Expand Up @@ -271,7 +271,7 @@ static void sort_groups(RzCmdDesc *group) {
void **it_cd;

if (group->help->sort_subcommands) {
rz_pvector_sort(&group->children, cd_sort);
rz_pvector_sort(&group->children, cd_sort, NULL);
}
rz_cmd_desc_children_foreach(group, it_cd) {
RzCmdDesc *cd = *(RzCmdDesc **)it_cd;
Expand Down
6 changes: 5 additions & 1 deletion librz/core/cmd/cmd_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ typedef enum {
PRINT_SOURCE_INFO_FILES
} PrintSourceInfoType;

static int compare_string(const char *s1, const char *s2, void *user) {
return strcmp(s1, s2);
}

static bool print_source_info(RzCore *core, PrintSourceInfoType type, RzCmdStateOutput *state) {
RzBinFile *binfile = core->bin->cur;
if (!binfile || !binfile->o) {
Expand Down Expand Up @@ -94,7 +98,7 @@ static bool print_source_info(RzCore *core, PrintSourceInfoType type, RzCmdState
RzPVector sorter;
rz_pvector_init(&sorter, free);
ht_pp_foreach(files, source_file_collect_cb, &sorter);
rz_pvector_sort(&sorter, (RzPVectorComparator)strcmp);
rz_pvector_sort(&sorter, (RzPVectorComparator)compare_string, NULL);
ht_pp_free(files);
// print them!
if (state->mode == RZ_OUTPUT_MODE_JSON) {
Expand Down
10 changes: 5 additions & 5 deletions librz/include/rz_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ extern "C" {
*/

// RzPVectorComparator should return negative, 0, positive to indicate "value < vec_data", "value == vec_data", "value > vec_data".
typedef int (*RzPVectorComparator)(const void *value, const void *vec_data);
typedef int (*RzVectorComparator)(const void *a, const void *b);
typedef int (*RzPVectorComparator)(const void *value, const void *vec_data, void *user);
typedef int (*RzVectorComparator)(const void *a, const void *b, void *user);
typedef void (*RzVectorFree)(void *e, void *user);
typedef void (*RzPVectorFree)(void *e);

Expand Down Expand Up @@ -176,7 +176,7 @@ RZ_API void *rz_vector_shrink(RzVector *vec);
RZ_API void *rz_vector_flush(RzVector *vec);

// sort vector
RZ_API void rz_vector_sort(RzVector *vec, RzVectorComparator cmp, bool reverse);
RZ_API void rz_vector_sort(RzVector *vec, RzVectorComparator cmp, bool reverse, void *user);

/*
* example:
Expand Down Expand Up @@ -310,7 +310,7 @@ static inline void *rz_pvector_tail(RzPVector *vec) {
RZ_API void **rz_pvector_contains(RzPVector *vec, const void *x);

// find the element in the vec based on cmparator
RZ_API RZ_BORROW void **rz_pvector_find(RZ_NONNULL const RzPVector *vec, RZ_NONNULL const void *element, RZ_NONNULL RzPVectorComparator cmp);
RZ_API RZ_BORROW void **rz_pvector_find(RZ_NONNULL const RzPVector *vec, RZ_NONNULL const void *element, RZ_NONNULL RzPVectorComparator cmp, void *user);

// removes and returns the pointer at the given index. Does not call free.
RZ_API void *rz_pvector_remove_at(RzPVector *vec, size_t index);
Expand Down Expand Up @@ -345,7 +345,7 @@ static inline void **rz_pvector_push_front(RzPVector *vec, void *x) {
}

// sort vec using quick sort.
RZ_API void rz_pvector_sort(RzPVector *vec, RzPVectorComparator cmp);
RZ_API void rz_pvector_sort(RzPVector *vec, RzPVectorComparator cmp, void *user);

static inline void **rz_pvector_reserve(RzPVector *vec, size_t capacity) {
return (void **)rz_vector_reserve(&vec->v, capacity);
Expand Down
Loading

0 comments on commit f7589fe

Please sign in to comment.