Skip to content

Commit

Permalink
Minor r_io optimization ##io
Browse files Browse the repository at this point in the history
  • Loading branch information
condret committed Nov 26, 2024
1 parent e8158ca commit a58cb44
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 159 deletions.
2 changes: 1 addition & 1 deletion libr/core/cbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2825,7 +2825,7 @@ static RIODesc *findReusableFile(RIO *io, const char *uri, int perm) {
.perm = perm,
.desc = NULL,
};
r_id_storage_foreach (io->files, findFile, &arg);
r_id_storage_foreach (&io->files, findFile, &arg);
return arg.desc;
}

Expand Down
6 changes: 3 additions & 3 deletions libr/core/cfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static bool close_but_cb(void *user, void *data, ut32 id) {

// TODO: move to IO as a helper?
R_API bool r_core_file_close_all_but(RCore *core) {
r_id_storage_foreach (core->io->files, close_but_cb, core);
r_id_storage_foreach (&core->io->files, close_but_cb, core);
return true;
}

Expand Down Expand Up @@ -616,7 +616,7 @@ static bool filecb(void *user, void *data, ut32 id) {

static bool file_is_loaded(RCore *core, const char *lib) {
MyFileData filedata = {lib, false};
r_id_storage_foreach (core->io->files, filecb, &filedata);
r_id_storage_foreach (&core->io->files, filecb, &filedata);
return filedata.found;
}

Expand Down Expand Up @@ -797,7 +797,7 @@ R_API bool r_core_bin_load(RCore *r, const char *filenameuri, ut64 baddr) {
const char *imp_name = r_bin_name_tostring2 (imp->name, 'o');
eprintf ("Resolving %s... ", imp_name);
RCoreLinkData linkdata = {imp_name, UT64_MAX, r->bin};
r_id_storage_foreach (r->io->files, linkcb, &linkdata);
r_id_storage_foreach (&r->io->files, linkcb, &linkdata);
if (linkdata.addr != UT64_MAX) {
eprintf ("0x%08"PFMT64x, linkdata.addr);
ut64 a = linkdata.addr;
Expand Down
4 changes: 2 additions & 2 deletions libr/core/cmd_info.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2189,14 +2189,14 @@ static int cmd_info(void *data, const char *input) {
const char *fn = (input[1] == ' ')
? r_str_trim_head_ro (input + 2): desc->name;
struct fdof_t fof = { core, fn, -1 };
r_id_storage_foreach (core->io->files, fdof_cb, &fof);
r_id_storage_foreach (&core->io->files, fdof_cb, &fof);
if (fof.fd != -1) {
oldfd = fof.fd;
}
ut64 baddr = r_config_get_i (core->config, "bin.baddr");
fof.fd = -1;
r_core_bin_load (core, fn, baddr);
r_id_storage_foreach (core->io->files, fdof_cb, &fof);
r_id_storage_foreach (&core->io->files, fdof_cb, &fof);
if (fof.fd != oldfd) {
r_core_cmdf (core, "o-%d", fof.fd);
}
Expand Down
50 changes: 25 additions & 25 deletions libr/core/cmd_open.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ static void cmd_open_bin(RCore *core, const char *input) {
}
free (arg);
} else {
RList *ofiles = r_id_storage_list (core->io->files);
RList *ofiles = r_id_storage_list (&core->io->files);
RIODesc *desc;
RListIter *iter;
RList *files = r_list_newf (NULL);
Expand Down Expand Up @@ -573,7 +573,7 @@ static void cmd_omfg(RCore *core, const char *input) {
return;
}
ut32 mapid;
if (!r_id_storage_get_lowest (core->io->maps, &mapid)) {
if (!r_id_storage_get_lowest (&core->io->maps, &mapid)) {
ut32 fd = r_io_fd_get_current (core->io);
RIODesc *desc = r_io_desc_get (core->io, fd);
if (desc) {
Expand All @@ -586,19 +586,19 @@ static void cmd_omfg(RCore *core, const char *input) {
do {
RIOMap *map = r_io_map_get (core->io, mapid);
map->perm |= perm;
} while (r_id_storage_get_next (core->io->maps, &mapid));
} while (r_id_storage_get_next (&core->io->maps, &mapid));
break;
case '-':
do {
RIOMap *map = r_io_map_get (core->io, mapid);
map->perm &= ~perm;
} while (r_id_storage_get_next (core->io->maps, &mapid));
} while (r_id_storage_get_next (&core->io->maps, &mapid));
break;
default:
do {
RIOMap *map = r_io_map_get (core->io, mapid);
map->perm = perm;
} while (r_id_storage_get_next (core->io->maps, &mapid));
} while (r_id_storage_get_next (&core->io->maps, &mapid));
break;
}
}
Expand Down Expand Up @@ -670,9 +670,9 @@ static void r_core_cmd_om_tab(RCore *core, const char *arg) {
}
r_table_set_columnsf (t, "nnnnnnnss", "id", "fd", "pa", "pa_end", "size", "va", "va_end", "perm", "name", NULL);
ut32 mapid = 0;
r_id_storage_get_lowest (core->io->maps, &mapid);
r_id_storage_get_lowest (&core->io->maps, &mapid);
do {
RIOMap *m = r_id_storage_get (core->io->maps, mapid);
RIOMap *m = r_id_storage_get (&core->io->maps, mapid);
if (!m) {
R_LOG_WARN ("Cannot find mapid %d", mapid);
break;
Expand All @@ -686,7 +686,7 @@ static void r_core_cmd_om_tab(RCore *core, const char *arg) {
r_table_add_rowf (t, "ddxxxxxss",
m->id, m->fd, pa, pa_end, pa_size,
va, va_end, r_str_rwx_i (m->perm), name);
} while (r_id_storage_get_next (core->io->maps, &mapid));
} while (r_id_storage_get_next (&core->io->maps, &mapid));
if (r_table_query (t, arg)) {
char *ts = strchr (arg, ':')? r_table_tostring (t) : r_table_tofancystring (t);
r_cons_printf ("%s", ts);
Expand Down Expand Up @@ -831,11 +831,11 @@ static void cmd_open_banks(RCore *core, int argc, char *argv[]) {
case 'g': // "ombg"
{
ut32 mapid;
r_id_storage_get_lowest (core->io->maps, &mapid);
r_id_storage_get_lowest (&core->io->maps, &mapid);
do {
RIOMap *map = r_id_storage_get (core->io->maps, mapid);
RIOMap *map = r_id_storage_get (&core->io->maps, mapid);
r_io_bank_map_add_top (core->io, core->io->bank, map->id);
} while (r_id_storage_get_next (core->io->maps, &mapid));
} while (r_id_storage_get_next (&core->io->maps, &mapid));
}
break;
case 'q': // "ombq"
Expand All @@ -844,11 +844,11 @@ static void cmd_open_banks(RCore *core, int argc, char *argv[]) {
case 0: // "omb"
{
ut32 bank_id = 0;
if (!r_id_storage_get_lowest (core->io->banks, &bank_id)) {
if (!r_id_storage_get_lowest (&core->io->banks, &bank_id)) {
break;
}
do {
RIOBank *bank = r_id_storage_get (core->io->banks, bank_id);
RIOBank *bank = r_id_storage_get (&core->io->banks, bank_id);
const char ch = core->io->bank == bank_id? '*': '-';
r_cons_printf ("%c %d %s [", ch, bank->id, bank->name);
RIOMapRef *mapref;
Expand All @@ -858,7 +858,7 @@ static void cmd_open_banks(RCore *core, int argc, char *argv[]) {
}
r_cons_printf (" ]\n");
// list all the associated maps
} while (r_id_storage_get_next (core->io->banks, &bank_id));
} while (r_id_storage_get_next (&core->io->banks, &bank_id));
}
break;
case '+': // "omb+ [name]"
Expand Down Expand Up @@ -1318,8 +1318,8 @@ static bool reopen_in_malloc_cb(void *user, void *data, ut32 id) {
}

R_API void r_core_file_reopen_in_malloc(RCore *core) {
if (core && core->io && core->io->files) {
r_id_storage_foreach (core->io->files, reopen_in_malloc_cb, core->io);
if (core && core->io) {
r_id_storage_foreach (&core->io->files, reopen_in_malloc_cb, core->io);
}
}

Expand Down Expand Up @@ -1788,7 +1788,7 @@ static bool cmd_onn(RCore *core, const char* input) {
ut64 addr = 0LL;
// check if file is opened already
if (r_str_startswith (input, "nnu")) {
r_id_storage_foreach (core->io->files, find_desc_by_name, &on);
r_id_storage_foreach (&core->io->files, find_desc_by_name, &on);
if (on.desc) {
core->io->desc = on.desc;
return true;
Expand Down Expand Up @@ -2159,8 +2159,8 @@ static int cmd_open(void *data, const char *input) {
}
} else { // "o="
fdsz = 0;
r_id_storage_foreach (core->io->files, init_desc_list_visual_cb, core->print);
r_id_storage_foreach (core->io->files, desc_list_visual_cb, core->print);
r_id_storage_foreach (&core->io->files, init_desc_list_visual_cb, core->print);
r_id_storage_foreach (&core->io->files, desc_list_visual_cb, core->print);
}
break;
case 'q': // "oq"
Expand All @@ -2170,21 +2170,21 @@ static int cmd_open(void *data, const char *input) {
r_cons_printf ("%d\n", fd);
}
} else if (input[1] == '.') { // "oq."
r_id_storage_foreach (core->io->files, desc_list_quiet2_cb, core->print);
r_id_storage_foreach (&core->io->files, desc_list_quiet2_cb, core->print);
} else {
r_id_storage_foreach (core->io->files, desc_list_quiet_cb, core->print);
r_id_storage_foreach (&core->io->files, desc_list_quiet_cb, core->print);
}
break;
case '\0': // "o"
r_id_storage_foreach (core->io->files, desc_list_cb, core->print);
r_id_storage_foreach (&core->io->files, desc_list_cb, core->print);
break;
case '*': // "o*"
if (input[1] == '?') {
r_core_cmd_help_match (core, help_msg_o, "o*");
} else if (input[1] == '*') {
r_id_storage_foreach (core->io->files, desc_list_cmds_cb2, core);
r_id_storage_foreach (&core->io->files, desc_list_cmds_cb2, core);
} else {
r_id_storage_foreach (core->io->files, desc_list_cmds_cb, core);
r_id_storage_foreach (&core->io->files, desc_list_cmds_cb, core);
}
break;
case 'j': // "oj"
Expand All @@ -2194,7 +2194,7 @@ static int cmd_open(void *data, const char *input) {
}
PJ *pj = r_core_pj_new (core);
pj_a (pj);
r_id_storage_foreach (core->io->files, desc_list_json_cb, pj);
r_id_storage_foreach (&core->io->files, desc_list_json_cb, pj);
pj_end (pj);
core->print->cb_printf ("%s\n", pj_string (pj));
pj_free (pj);
Expand Down
2 changes: 1 addition & 1 deletion libr/core/numvars.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ static ut64 numvar_maps(RCore *core, const char *str, int *ok) {
map = r_io_map_get_at (core->io, at);
} else {
MapLoopData mld = { .name = name };
r_id_storage_foreach (core->io->maps, mapscb, &mld);
r_id_storage_foreach (&core->io->maps, mapscb, &mld);
map = mld.map;
}
R_FREE (name);
Expand Down
4 changes: 2 additions & 2 deletions libr/core/p/core_prj.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ static void rprj_mods_write_one(RBuffer *b, R2ProjectMod *mod) {
static RIOMap *coremod(Cursor *cur, R2ProjectMod *mod) {
// iterate over current maps and write
RBuffer *b = cur->b;
RIDStorage *maps = cur->core->io->maps;
RIDStorage *maps = &cur->core->io->maps;
ut32 mapid;
r_id_storage_get_lowest (maps, &mapid);
ut64 at = r_buf_at (b);
Expand Down Expand Up @@ -359,7 +359,7 @@ static RIOMap *coremod(Cursor *cur, R2ProjectMod *mod) {
static void rprj_mods_write(Cursor *cur) {
// iterate over current maps and write
RBuffer *b = cur->b;
RIDStorage *maps = cur->core->io->maps;
RIDStorage *maps = &cur->core->io->maps;
ut32 mapid;
r_id_storage_get_lowest (maps, &mapid);
ut64 at = r_buf_at (b);
Expand Down
4 changes: 2 additions & 2 deletions libr/core/panels.c
Original file line number Diff line number Diff line change
Expand Up @@ -5003,7 +5003,7 @@ static void __set_pcb(RPanel *p) {

static int __file_history_up(RLine *line) {
RCore *core = line->user;
RList *files = r_id_storage_list (core->io->files);
RList *files = r_id_storage_list (&core->io->files);
int num_files = r_list_length (files);
if (line->file_hist_index >= num_files || line->file_hist_index < 0) {
return false;
Expand All @@ -5020,7 +5020,7 @@ static int __file_history_up(RLine *line) {

static int __file_history_down(RLine *line) {
RCore *core = line->user;
RList *files = r_id_storage_list (core->io->files);
RList *files = r_id_storage_list (&core->io->files);
int num_files = r_list_length (files);
if (line->file_hist_index <= 0 || line->file_hist_index > num_files) {
return false;
Expand Down
4 changes: 2 additions & 2 deletions libr/core/project.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,9 @@ R_API bool r_core_project_save_script(RCore *core, const char *file, int opts) {
flush (sb);
}
#if PROJECT_EXPERIMENTAL
if (opts & R_CORE_PRJ_IO_MAPS && core->io && core->io->files) {
if (opts & R_CORE_PRJ_IO_MAPS && core->io) {
fdc = 3;
r_id_storage_foreach (core->io->files, (RIDStorageForeachCb)store_files_and_maps, core);
r_id_storage_foreach (&core->io->files, (RIDStorageForeachCb)store_files_and_maps, core);
flush (sb);
}
#endif
Expand Down
6 changes: 3 additions & 3 deletions libr/include/r_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ typedef struct r_io_t {
bool cachemode; // write in cache all the read operations (EXPERIMENTAL)
ut32 p_cache; // uses 1, 2, 4.. probably R_PERM_RWX :D
ut64 mts; // map "timestamps", this sucks somehow
RIDStorage *files; // RIODescs accessible by their fd
RIDStorage *maps; // RIOMaps accessible by their id
RIDStorage *banks; // RIOBanks accessible by their id
RIDStorage files; // RIODescs accessible by their fd
RIDStorage maps; // RIOMaps accessible by their id
RIDStorage banks; // RIOBanks accessible by their id
RIOCache cache;
ut8 *write_mask;
int write_mask_len;
Expand Down
4 changes: 2 additions & 2 deletions libr/io/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ R_API RList* r_io_open_many(RIO* io, const char* uri, int perm, int mode) {
RList* desc_list;
RListIter* iter;
RIODesc* desc;
R_RETURN_VAL_IF_FAIL (io && io->files && uri, NULL);
R_RETURN_VAL_IF_FAIL (io && io->files.pool && uri, NULL);
RIOPlugin* plugin = r_io_plugin_resolve (io, uri, 1);
if (!plugin || !plugin->open_many || !plugin->close) {
return NULL;
Expand Down Expand Up @@ -573,7 +573,7 @@ static bool drain_cb(void *user, void *data, ut32 id) {

R_API void r_io_drain_overlay(RIO *io) {
R_RETURN_IF_FAIL (io);
r_id_storage_foreach (io->maps, drain_cb, NULL);
r_id_storage_foreach (&io->maps, drain_cb, NULL);
}

R_API bool r_io_get_region_at(RIO *io, RIORegion *region, ut64 addr) {
Expand Down
30 changes: 14 additions & 16 deletions libr/io/io_bank.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ R_API void r_io_bank_free(RIOBank *bank) {
R_API void r_io_bank_init(RIO *io) {
R_RETURN_IF_FAIL (io);
r_io_bank_fini (io);
io->banks = r_id_storage_new (0, UT32_MAX);
r_id_storage_init (&io->banks, 0, UT32_MAX);
}

static bool _bank_free_cb(void *user, void *data, ut32 id) {
Expand All @@ -63,16 +63,14 @@ static bool _bank_free_cb(void *user, void *data, ut32 id) {

R_API void r_io_bank_fini(RIO *io) {
R_RETURN_IF_FAIL (io);
if (io->banks) {
r_id_storage_foreach (io->banks, _bank_free_cb, NULL);
r_id_storage_free (io->banks);
io->banks = NULL;
}
r_id_storage_foreach (&io->banks, _bank_free_cb, NULL);
r_id_storage_fini (&io->banks);
io->banks = (const RIDStorage){0};
}

R_API RIOBank *r_io_bank_get(RIO *io, const ut32 bankid) {
R_RETURN_VAL_IF_FAIL (io && io->banks, NULL);
return (RIOBank *)r_id_storage_get (io->banks, bankid);
R_RETURN_VAL_IF_FAIL (io, NULL);
return (RIOBank *)r_id_storage_get (&io->banks, bankid);
}

typedef struct {
Expand All @@ -92,17 +90,17 @@ static bool find_bank(void *data, void *user, ut32 id) {
}

R_API RIOBank *r_io_bank_get_byname(RIO *io, const char *bankname) {
R_RETURN_VAL_IF_FAIL (io && io->banks && bankname, NULL);
R_RETURN_VAL_IF_FAIL (io && bankname, NULL);
Boring boo = { .io = io, .name = bankname, .bank = NULL };
eprintf ("ooME (%s)\n", boo.name);
r_id_storage_foreach (io->banks, &find_bank, &boo);
r_id_storage_foreach (&io->banks, &find_bank, &boo);
return boo.bank;
}

R_API ut32 r_io_bank_first(RIO *io) {
R_RETURN_VAL_IF_FAIL (io, UT32_MAX);
ut32 bankid = -1;
r_id_storage_get_lowest (io->banks, &bankid);
ut32 bankid = UT32_MAX;
r_id_storage_get_lowest (&io->banks, &bankid);
return bankid;
}

Expand All @@ -117,8 +115,8 @@ R_API bool r_io_bank_use(RIO *io, ut32 bankid) {
}

R_API bool r_io_bank_add(RIO *io, RIOBank *bank) {
R_RETURN_VAL_IF_FAIL (io && io->banks && bank, false);
return r_id_storage_add (io->banks, bank, &bank->id);
R_RETURN_VAL_IF_FAIL (io && bank, false);
return r_id_storage_add (&io->banks, bank, &bank->id);
}

static RIOMapRef *_mapref_from_map(RIOMap *map) {
Expand Down Expand Up @@ -922,7 +920,7 @@ R_API bool r_io_bank_write_to_overlay_at(RIO *io, const ut32 bankid, ut64 addr,
r_io_submap_to (sm)) - (addr + buf_off) + 1;
ret &= r_io_map_write_to_overlay (map, addr + buf_off, &buf[buf_off], write_len);
node = r_rbnode_next (node);
sm = node ? (RIOSubMap *)node->data : NULL;
sm = node? (RIOSubMap *)node->data: NULL;
}
return ret;
}
Expand Down Expand Up @@ -1079,7 +1077,7 @@ R_API void r_io_bank_del_map(RIO *io, const ut32 bankid, const ut32 mapid) {

R_API void r_io_bank_del(RIO *io, const ut32 bankid) {
R_RETURN_IF_FAIL (io);
r_id_storage_delete (io->banks, bankid);
r_id_storage_delete (&io->banks, bankid);
if (io->bank == bankid) {
io->bank = r_io_bank_first (io);
}
Expand Down
Loading

0 comments on commit a58cb44

Please sign in to comment.