From b351cd8211aadf7bed91446d5157a43471d08c06 Mon Sep 17 00:00:00 2001 From: condret Date: Mon, 25 Nov 2024 20:50:25 +0100 Subject: [PATCH] Minor r_io optimization ##io --- libr/core/cbin.c | 2 +- libr/core/cfile.c | 6 +-- libr/core/cmd_info.inc.c | 4 +- libr/core/cmd_open.inc.c | 50 ++++++++++++------------- libr/core/numvars.inc.c | 2 +- libr/core/p/core_prj.c | 4 +- libr/core/panels.c | 4 +- libr/core/project.c | 4 +- libr/include/r_io.h | 6 +-- libr/io/io.c | 4 +- libr/io/io_bank.c | 30 +++++++-------- libr/io/io_desc.c | 71 +++++++++++++++++------------------ libr/io/io_fd.c | 30 +++++++-------- libr/io/io_map.c | 80 +++++++++++++++++++--------------------- libr/io/p_cache.c | 7 ++-- 15 files changed, 145 insertions(+), 159 deletions(-) diff --git a/libr/core/cbin.c b/libr/core/cbin.c index a8639edea2db2..56c526bf21a07 100644 --- a/libr/core/cbin.c +++ b/libr/core/cbin.c @@ -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; } diff --git a/libr/core/cfile.c b/libr/core/cfile.c index 2b938412cd1b4..6d666ccce7217 100644 --- a/libr/core/cfile.c +++ b/libr/core/cfile.c @@ -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; } @@ -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; } @@ -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; diff --git a/libr/core/cmd_info.inc.c b/libr/core/cmd_info.inc.c index 4fb26137fc0bc..95165cbd5bad6 100644 --- a/libr/core/cmd_info.inc.c +++ b/libr/core/cmd_info.inc.c @@ -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); } diff --git a/libr/core/cmd_open.inc.c b/libr/core/cmd_open.inc.c index b0be2193422dd..7e78aad1d6be7 100644 --- a/libr/core/cmd_open.inc.c +++ b/libr/core/cmd_open.inc.c @@ -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); @@ -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) { @@ -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; } } @@ -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; @@ -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); @@ -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" @@ -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; @@ -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]" @@ -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); } } @@ -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; @@ -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" @@ -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" @@ -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); diff --git a/libr/core/numvars.inc.c b/libr/core/numvars.inc.c index 15c1db1588952..224dce72a0ebc 100644 --- a/libr/core/numvars.inc.c +++ b/libr/core/numvars.inc.c @@ -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); diff --git a/libr/core/p/core_prj.c b/libr/core/p/core_prj.c index c4bd4d59df738..16233e5c41e55 100644 --- a/libr/core/p/core_prj.c +++ b/libr/core/p/core_prj.c @@ -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); @@ -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); diff --git a/libr/core/panels.c b/libr/core/panels.c index a800e688eec0c..c0a5024e6fb96 100644 --- a/libr/core/panels.c +++ b/libr/core/panels.c @@ -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; @@ -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; diff --git a/libr/core/project.c b/libr/core/project.c index 4c7f51b78de39..ab908126bfaf7 100644 --- a/libr/core/project.c +++ b/libr/core/project.c @@ -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 diff --git a/libr/include/r_io.h b/libr/include/r_io.h index 7624c5786aef1..1ffca063343a8 100644 --- a/libr/include/r_io.h +++ b/libr/include/r_io.h @@ -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; diff --git a/libr/io/io.c b/libr/io/io.c index 7b7a60cfca641..a7a51543b7a7a 100644 --- a/libr/io/io.c +++ b/libr/io/io.c @@ -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; @@ -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) { diff --git a/libr/io/io_bank.c b/libr/io/io_bank.c index 54b569d84421c..868b955c49111 100644 --- a/libr/io/io_bank.c +++ b/libr/io/io_bank.c @@ -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) { @@ -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 { @@ -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; } @@ -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) { @@ -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; } @@ -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); } diff --git a/libr/io/io_desc.c b/libr/io/io_desc.c index f2447ac0aa0cb..c55982b0df612 100644 --- a/libr/io/io_desc.c +++ b/libr/io/io_desc.c @@ -1,4 +1,4 @@ -/* radare2 - LGPL - Copyright 2017-2023 - condret, pancake, alvaro */ +/* radare2 - LGPL - Copyright 2017-2024 - condret, pancake, alvaro */ #include @@ -6,12 +6,10 @@ R_API RIODesc* r_io_desc_new(RIO* io, RIOPlugin* plugin, const char* uri, int perm, int mode, void* data) { R_RETURN_VAL_IF_FAIL (io && plugin && uri, NULL); ut32 fd32 = 0; - if (io->files) { - if (!r_id_pool_grab_id (io->files->pool, &fd32)) { - return NULL; - } + if (!r_id_pool_grab_id (io->files.pool, &fd32)) { + return NULL; } - RIODesc* desc = R_NEW0 (RIODesc); + RIODesc *desc = R_NEW0 (RIODesc); if (desc) { desc->fd = fd32; desc->io = io; @@ -31,8 +29,8 @@ R_API void r_io_desc_free(RIODesc* desc) { free (desc->referer); free (desc->name); r_io_desc_cache_fini (desc); - if (desc->io && desc->io->files) { - r_id_storage_delete (desc->io->files, desc->fd); + if (desc->io) { + r_id_storage_delete (&desc->io->files, desc->fd); } // free (desc->plugin); free (desc); @@ -41,7 +39,7 @@ R_API void r_io_desc_free(RIODesc* desc) { R_API bool r_io_desc_add(RIO* io, RIODesc* desc) { R_RETURN_VAL_IF_FAIL (io && desc && desc->io, false); - if (!r_id_storage_set (io->files, desc, desc->fd)) { + if (!r_id_storage_set (&io->files, desc, desc->fd)) { // TODO: use assert here R_LOG_ERROR ("Wrong API usage. fd %d wasnt generated by this IO instance", desc->fd); r_sys_backtrace (); @@ -52,8 +50,11 @@ R_API bool r_io_desc_add(RIO* io, RIODesc* desc) { // can we pass this a riodesc and check if it belongs to the desc->io ? R_API bool r_io_desc_del(RIO* io, int fd) { - R_RETURN_VAL_IF_FAIL (io && io->files, false); - RIODesc* desc = r_id_storage_get (io->files, fd); + R_RETURN_VAL_IF_FAIL (io, false); + RIODesc *desc = r_id_storage_get (&io->files, fd); + if (!desc) { + return false; + } if (desc == io->desc) { io->desc = NULL; } @@ -64,12 +65,12 @@ R_API bool r_io_desc_del(RIO* io, int fd) { } R_API RIODesc* r_io_desc_get(RIO* io, int fd) { - R_RETURN_VAL_IF_FAIL (io && io->files, NULL); - return (RIODesc*) r_id_storage_get (io->files, fd); + R_RETURN_VAL_IF_FAIL (io, NULL); + return (RIODesc *)r_id_storage_get (&io->files, fd); } R_API RIODesc *r_io_desc_get_byuri(RIO *io, const char *uri) { - R_RETURN_VAL_IF_FAIL (io && io->files, NULL); + R_RETURN_VAL_IF_FAIL (io, NULL); RIODesc *d = r_io_desc_get_lowest (io); while (d) { if (!strcmp (d->uri, uri)) { @@ -81,21 +82,21 @@ R_API RIODesc *r_io_desc_get_byuri(RIO *io, const char *uri) { } R_API RIODesc *r_io_desc_get_next(RIO *io, RIODesc *desc) { - R_RETURN_VAL_IF_FAIL (desc && io && io->files, NULL); + R_RETURN_VAL_IF_FAIL (desc && io, NULL); const int next_fd = r_io_fd_get_next (io, desc->fd); if (next_fd == -1) { return NULL; } - return (RIODesc*) r_id_storage_get (io->files, next_fd); + return (RIODesc *)r_id_storage_get (&io->files, next_fd); } R_API RIODesc *r_io_desc_get_prev(RIO *io, RIODesc *desc) { - R_RETURN_VAL_IF_FAIL (desc && io && io->files, NULL); + R_RETURN_VAL_IF_FAIL (desc && io, NULL); const int prev_fd = r_io_fd_get_prev (io, desc->fd); if (prev_fd == -1) { return NULL; } - return (RIODesc*) r_id_storage_get (io->files, prev_fd); + return (RIODesc *)r_id_storage_get (&io->files, prev_fd); } R_API RIODesc *r_io_desc_get_highest(RIO *io) { @@ -142,7 +143,7 @@ R_API RIODesc *r_io_desc_open(RIO *io, const char *uri, int perm, int mode) { } R_API RIODesc *r_io_desc_open_plugin(RIO *io, RIOPlugin *plugin, const char *uri, int perm, int mode) { - R_RETURN_VAL_IF_FAIL (io && io->files && uri && plugin, NULL); + R_RETURN_VAL_IF_FAIL (io && uri && plugin, NULL); if (!plugin->open || !plugin->check || !plugin->check (io, uri, false)) { return NULL; } @@ -266,8 +267,8 @@ R_API bool r_io_desc_resize(RIODesc *desc, ut64 newsize) { r_io_desc_cache_cleanup (desc); } DescMapResize dmr = {desc->io, r_queue_new (1), osize, newsize, desc->fd}; - if (desc->io->maps) { - r_id_storage_foreach (desc->io->maps, _resize_affected_maps, &dmr); + if (desc->io) { + r_id_storage_foreach (&desc->io->maps, _resize_affected_maps, &dmr); while (!r_queue_is_empty (dmr.del)) { r_io_map_del (desc->io, ((RIOMap *)r_queue_dequeue (dmr.del))->id); } @@ -306,8 +307,8 @@ R_API bool r_io_desc_exchange(RIO* io, int fd, int fdx) { } desc->fd = fdx; descx->fd = fd; - r_id_storage_set (io->files, desc, fdx); - r_id_storage_set (io->files, descx, fd); + r_id_storage_set (&io->files, desc, fdx); + r_id_storage_set (&io->files, descx, fd); if (io->p_cache) { HtUP* cache = desc->cache; desc->cache = descx->cache; @@ -316,15 +317,15 @@ R_API bool r_io_desc_exchange(RIO* io, int fd, int fdx) { r_io_desc_cache_cleanup (descx); } ut32 map_id; - if (r_id_storage_get_lowest (io->maps, &map_id)) { + if (r_id_storage_get_lowest (&io->maps, &map_id)) { do { - RIOMap *map = r_id_storage_get (io->maps, map_id); + RIOMap *map = r_id_storage_get (&io->maps, map_id); if (map->fd == fdx) { map->perm &= (desc->perm | R_PERM_X); } else if (map->fd == fd) { map->perm &= (descx->perm | R_PERM_X); } - } while (r_id_storage_get_next (io->maps, &map_id)); + } while (r_id_storage_get_next (&io->maps, &map_id)); } return true; } @@ -401,19 +402,15 @@ R_API bool r_io_desc_extend(RIODesc *desc, ut64 size) { /* lifecycle */ // TODO: move into io.c : r_io_init -R_IPI bool r_io_desc_init(RIO* io) { +R_IPI bool r_io_desc_init(RIO *io) { R_RETURN_VAL_IF_FAIL (io, false); r_io_desc_fini (io); // TODO: it leaks if called twice //fd is signed - io->files = r_id_storage_new (3, 0x80000000); - if (!io->files) { - return false; - } - return true; + return r_id_storage_init (&io->files, 3, 0x80000000); } -static bool desc_fini_cb(void* user, void* data, ut32 id) { +static bool desc_fini_cb(void *user, void *data, ut32 id) { RIODesc* desc = (RIODesc*) data; if (desc && desc->plugin && desc->plugin->close) { desc->plugin->close (desc); @@ -425,11 +422,9 @@ static bool desc_fini_cb(void* user, void* data, ut32 id) { //closes all descs and frees all descs and io->files R_IPI void r_io_desc_fini(RIO* io) { R_RETURN_IF_FAIL (io); - if (io->files) { - r_id_storage_foreach (io->files, desc_fini_cb, io); - r_id_storage_free (io->files); - io->files = NULL; - } + r_id_storage_foreach (&io->files, desc_fini_cb, io); + r_id_storage_fini (&io->files); + io->files = (const RIDStorage){0}; //no map-cleanup here, to keep it modular useable io->desc = NULL; } diff --git a/libr/io/io_fd.c b/libr/io/io_fd.c index ccde8c77faaff..3517942902c00 100644 --- a/libr/io/io_fd.c +++ b/libr/io/io_fd.c @@ -1,10 +1,10 @@ -/* radare2 - LGPL - Copyright 2017-2023 - condret */ +/* radare2 - LGPL - Copyright 2017-2024 - condret */ #include R_API int r_io_fd_open(RIO *io, const char *uri, int flags, int mode) { RIODesc *desc = r_io_desc_open (io, uri, flags, mode); - return desc ? desc->fd : -1; + return desc? desc->fd: -1; } R_API bool r_io_fd_close(RIO *io, int fd) { @@ -73,39 +73,37 @@ R_API int r_io_fd_read_at(RIO *io, int fd, ut64 addr, ut8 *buf, int len) { //returns length of written bytes R_API int r_io_fd_write_at(RIO *io, int fd, ut64 addr, const ut8 *buf, int len) { - R_RETURN_VAL_IF_FAIL (io && buf, false); + R_RETURN_VAL_IF_FAIL (io && buf, -1); RIODesc *desc = r_io_desc_get (io, fd); return desc? r_io_desc_write_at (desc, addr, buf, len): -1; } R_API bool r_io_fd_is_dbg(RIO *io, int fd) { - R_RETURN_VAL_IF_FAIL (io && io->files, false); + R_RETURN_VAL_IF_FAIL (io, false); RIODesc *desc = r_io_desc_get (io, fd); return desc? r_io_desc_is_dbg (desc): false; } R_API int r_io_fd_get_pid(RIO *io, int fd) { - if (!io || !io->files) { - return -2; - } + R_RETURN_VAL_IF_FAIL (io, -1); RIODesc *desc = r_io_desc_get (io, fd); - return r_io_desc_get_pid (desc); + return desc? r_io_desc_get_pid (desc): -2; } R_API int r_io_fd_get_tid(RIO *io, int fd) { - R_RETURN_VAL_IF_FAIL (io && io->files, -2); + R_RETURN_VAL_IF_FAIL (io, -1); RIODesc *desc = r_io_desc_get (io, fd); - return r_io_desc_get_tid (desc); + return desc? r_io_desc_get_tid (desc): -2; } R_API bool r_io_fd_get_base(RIO *io, int fd, ut64 *base) { - R_RETURN_VAL_IF_FAIL (io && io->files && base, false); + R_RETURN_VAL_IF_FAIL (io && io->files.data && base, false); RIODesc *desc = r_io_desc_get (io, fd); return r_io_desc_get_base (desc, base); } R_API const char *r_io_fd_get_name(RIO *io, int fd) { - R_RETURN_VAL_IF_FAIL (io && io->files, NULL); + R_RETURN_VAL_IF_FAIL (io && io->files.data, NULL); RIODesc *desc = r_io_desc_get (io, fd); return desc? desc->name: NULL; } @@ -138,7 +136,7 @@ R_API int r_io_fd_get_current(RIO *io) { R_API int r_io_fd_get_next(RIO *io, int fd) { R_RETURN_VAL_IF_FAIL (io, -1); int ret = fd; - if (!r_id_storage_get_next (io->files, (ut32 *)&ret)) { + if (!r_id_storage_get_next (&io->files, (ut32 *)&ret)) { return -1; } return ret; @@ -147,7 +145,7 @@ R_API int r_io_fd_get_next(RIO *io, int fd) { R_API int r_io_fd_get_prev(RIO *io, int fd) { R_RETURN_VAL_IF_FAIL (io, -1); int ret = fd; - if (!r_id_storage_get_prev (io->files, (ut32 *)&ret)) { + if (!r_id_storage_get_prev (&io->files, (ut32 *)&ret)) { return -1; } return ret; @@ -156,7 +154,7 @@ R_API int r_io_fd_get_prev(RIO *io, int fd) { R_API int r_io_fd_get_highest(RIO *io) { R_RETURN_VAL_IF_FAIL (io, -1); int fd = -1; - if (!r_id_storage_get_highest (io->files, (ut32 *)&fd)) { + if (!r_id_storage_get_highest (&io->files, (ut32 *)&fd)) { return -1; } return fd; @@ -165,7 +163,7 @@ R_API int r_io_fd_get_highest(RIO *io) { R_API int r_io_fd_get_lowest(RIO *io) { R_RETURN_VAL_IF_FAIL (io, -1); int fd = -1; - if (!r_id_storage_get_lowest (io->files, (ut32 *)&fd)) { + if (!r_id_storage_get_lowest (&io->files, (ut32 *)&fd)) { return -1; } return fd; diff --git a/libr/io/io_map.c b/libr/io/io_map.c index 61b7fcc626973..01dc239fe2db2 100644 --- a/libr/io/io_map.c +++ b/libr/io/io_map.c @@ -9,13 +9,13 @@ R_IPI bool io_bank_has_map(RIO *io, const ut32 bankid, const ut32 mapid); static RIOMap *io_map_new(RIO* io, int fd, int perm, ut64 delta, ut64 addr, ut64 size) { - R_RETURN_VAL_IF_FAIL (io && io->maps, NULL); + R_RETURN_VAL_IF_FAIL (io, NULL); const ut64 fd_size = r_io_fd_size (io, fd); if ((!size) || (fd_size <= delta)) { return NULL; } RIOMap* map = R_NEW0 (RIOMap); - if (!map || !r_id_storage_add (io->maps, map, &map->id)) { + if (!map || !r_id_storage_add (&io->maps, map, &map->id)) { free (map); return NULL; } @@ -55,13 +55,13 @@ R_API bool r_io_map_remap(RIO *io, ut32 id, ut64 addr) { r_io_bank_del_map (io, io->bank, newmap->id); } ut32 bankid; - r_id_storage_get_lowest (io->banks, &bankid); + r_id_storage_get_lowest (&io->banks, &bankid); do { if (bankid != io->bank && io_bank_has_map (io, bankid, id)) { // TODO: use threads here r_io_bank_map_add_top (io, bankid, newmap->id); } - } while (r_id_storage_get_next (io->banks, &bankid)); + } while (r_id_storage_get_next (&io->banks, &bankid)); } else { // restore previous location and size if creation of newmap failed r_io_map_set_begin (map, ofrom); @@ -70,11 +70,11 @@ R_API bool r_io_map_remap(RIO *io, ut32 id, ut64 addr) { } } ut32 bankid; - r_id_storage_get_lowest (io->banks, &bankid); + r_id_storage_get_lowest (&io->banks, &bankid); do { // TODO: use threads here r_io_bank_update_map_boundaries (io, bankid, id, ofrom, oto); - } while (r_id_storage_get_next (io->banks, &bankid)); + } while (r_id_storage_get_next (&io->banks, &bankid)); return true; } @@ -104,11 +104,9 @@ static bool _map_free_cb(void *user, void *data, ut32 id) { R_API void r_io_map_init(RIO* io) { R_RETURN_IF_FAIL (io); - if (io->maps) { - r_id_storage_foreach (io->maps, _map_free_cb, NULL); - r_id_storage_free (io->maps); - } - io->maps = r_id_storage_new (1, END_OF_MAP_IDS); + r_id_storage_foreach (&io->maps, _map_free_cb, NULL); + r_id_storage_fini (&io->maps); + r_id_storage_init (&io->maps, 1, END_OF_MAP_IDS); } // check if a map with exact the same properties exists @@ -123,13 +121,13 @@ R_API bool r_io_map_exists(RIO *io, RIOMap *map) { // check if a map with specified id exists R_API bool r_io_map_exists_for_id(RIO *io, ut32 id) { - R_RETURN_VAL_IF_FAIL (io && io->maps, false); + R_RETURN_VAL_IF_FAIL (io, false); return r_io_map_get (io, id); } R_API RIOMap* r_io_map_get(RIO *io, ut32 id) { R_RETURN_VAL_IF_FAIL (io, false); - return r_id_storage_get (io->maps, id); + return r_id_storage_get (&io->maps, id); } R_API RIOMap *r_io_map_add(RIO *io, int fd, int perm, ut64 delta, ut64 addr, ut64 size) { @@ -150,7 +148,7 @@ R_API RIOMap *r_io_map_add(RIO *io, int fd, int perm, ut64 delta, ut64 addr, ut6 return NULL; } if (!r_io_bank_map_add_top (io, io->bank, map[0]->id)) { - r_id_storage_delete (io->maps, map[0]->id); + r_id_storage_delete (&io->maps, map[0]->id); free (map[0]); return NULL; } @@ -159,7 +157,7 @@ R_API RIOMap *r_io_map_add(RIO *io, int fd, int perm, ut64 delta, ut64 addr, ut6 map[1] = io_map_new (io, fd, perm, delta, addr, size); if (!map[1]) { if (map[0]) { - r_id_storage_delete (io->maps, map[0]->id); + r_id_storage_delete (&io->maps, map[0]->id); free (map[0]); } free (map[1]); @@ -167,10 +165,10 @@ R_API RIOMap *r_io_map_add(RIO *io, int fd, int perm, ut64 delta, ut64 addr, ut6 } if (!r_io_bank_map_add_top (io, io->bank, map[1]->id)) { if (map[0]) { - r_id_storage_delete (io->maps, map[0]->id); + r_id_storage_delete (&io->maps, map[0]->id); free (map[0]); } - r_id_storage_delete (io->maps, map[1]->id); + r_id_storage_delete (&io->maps, map[1]->id); free (map[1]); return NULL; } @@ -197,7 +195,7 @@ R_API RIOMap *r_io_map_add_bottom(RIO *io, int fd, int perm, ut64 delta, ut64 ad return NULL; } if (!r_io_bank_map_add_bottom (io, io->bank, map[0]->id)) { - r_id_storage_delete (io->maps, map[0]->id); + r_id_storage_delete (&io->maps, map[0]->id); free (map[0]); return NULL; } @@ -206,7 +204,7 @@ R_API RIOMap *r_io_map_add_bottom(RIO *io, int fd, int perm, ut64 delta, ut64 ad map[1] = io_map_new (io, fd, perm, delta, addr, size); if (!map[1]) { if (map[0]) { - r_id_storage_delete (io->maps, map[0]->id); + r_id_storage_delete (&io->maps, map[0]->id); free (map[0]); } free (map[1]); @@ -214,10 +212,10 @@ R_API RIOMap *r_io_map_add_bottom(RIO *io, int fd, int perm, ut64 delta, ut64 ad } if (!r_io_bank_map_add_bottom (io, io->bank, map[1]->id)) { if (map[0]) { - r_id_storage_delete (io->maps, map[0]->id); + r_id_storage_delete (&io->maps, map[0]->id); free (map[0]); } - r_id_storage_delete (io->maps, map[1]->id); + r_id_storage_delete (&io->maps, map[1]->id); free (map[1]); return NULL; } @@ -259,29 +257,29 @@ R_API void r_io_map_reset(RIO* io) { } R_API void r_io_map_del(RIO *io, ut32 id) { - R_RETURN_IF_FAIL (io && io->maps); - RIOMap *map = (RIOMap *)r_id_storage_get (io->maps, id); + R_RETURN_IF_FAIL (io); + RIOMap *map = (RIOMap *)r_id_storage_get (&io->maps, id); if (!map) { return; } ut32 bankid = 0; - if (!r_id_storage_get_lowest (io->banks, &bankid)) { + if (!r_id_storage_get_lowest (&io->banks, &bankid)) { R_LOG_ERROR ("Cannot get the lowest bankid"); return; } do { // TODO: use threads for every bank, except the current bank (io->bank) r_io_bank_del_map (io, bankid, id); - } while (r_id_storage_get_next (io->banks, &bankid)); - r_id_storage_delete (io->maps, id); + } while (r_id_storage_get_next (&io->banks, &bankid)); + r_id_storage_delete (&io->maps, id); _map_free_cb (NULL, map, id); } //delete all maps with specified fd R_API bool r_io_map_del_for_fd(RIO* io, int fd) { - R_RETURN_VAL_IF_FAIL (io && io->maps, false); + R_RETURN_VAL_IF_FAIL (io, false); ut32 map_id; - if (!r_id_storage_get_lowest (io->maps, &map_id)) { + if (!r_id_storage_get_lowest (&io->maps, &map_id)) { return false; } @@ -289,7 +287,7 @@ R_API bool r_io_map_del_for_fd(RIO* io, int fd) { bool cont; do { ut32 next = map_id; // is this actually needed? - cont = r_id_storage_get_next (io->maps, &next); + cont = r_id_storage_get_next (&io->maps, &next); RIOMap *map = r_io_map_get (io, map_id); if (map->fd == fd) { ret = true; @@ -331,7 +329,7 @@ R_API bool r_io_map_priorize_for_fd(RIO *io, int fd) { R_API void r_io_map_cleanup(RIO* io) { R_RETURN_IF_FAIL (io); //remove all maps if no descs exist - if (!io->files) { + if (!io->files.data) { r_io_map_fini (io); r_io_map_init (io); return; @@ -346,14 +344,10 @@ static bool _clear_banks_cb(void *user, void *data, ut32 id) { R_API void r_io_map_fini(RIO* io) { R_RETURN_IF_FAIL (io); - if (io->banks) { - r_id_storage_foreach (io->banks, _clear_banks_cb, NULL); - } - if (io->maps) { - r_id_storage_foreach (io->maps, _map_free_cb, NULL); - r_id_storage_free (io->maps); - io->maps = NULL; - } + r_id_storage_foreach (&io->banks, _clear_banks_cb, NULL); + r_id_storage_foreach (&io->maps, _map_free_cb, NULL); + r_id_storage_fini (&io->maps); + io->maps = (const RIDStorage){0}; } R_API void r_io_map_set_name(RIOMap* map, const char* name) { @@ -389,7 +383,7 @@ R_API RList* r_io_map_get_by_fd(RIO* io, int fd) { RListIter *iter; RIOMapRef *mapref; r_list_foreach_prev (bank->maprefs, iter, mapref) { - RIOMap *map = (RIOMap *)r_id_storage_get (io->maps, mapref->id); + RIOMap *map = (RIOMap *)r_id_storage_get (&io->maps, mapref->id); if (map->fd == fd) { r_list_append (map_list, map); } @@ -417,14 +411,14 @@ R_IPI bool io_map_resize(RIO *io, ut32 id, ut64 newsize) { r_io_bank_update_map_boundaries (io, io->bank, id, r_io_map_from (map), oto); } ut32 bankid; - r_id_storage_get_lowest (io->banks, &bankid); + r_id_storage_get_lowest (&io->banks, &bankid); do { if (bankid != io->bank && io_bank_has_map (io, bankid, id)) { // TODO: use threads here r_io_bank_update_map_boundaries (io, io->bank, id, r_io_map_from (map), oto); r_io_bank_map_add_top (io, bankid, newmap->id); } - } while (r_id_storage_get_next (io->banks, &bankid)); + } while (r_id_storage_get_next (&io->banks, &bankid)); } else { // restore previous size if creating newmap failed r_io_map_set_size (map, osize); @@ -434,13 +428,13 @@ R_IPI bool io_map_resize(RIO *io, ut32 id, ut64 newsize) { } r_io_map_set_size (map, newsize); ut32 bankid; - r_id_storage_get_lowest (io->banks, &bankid); + r_id_storage_get_lowest (&io->banks, &bankid); do { if (io_bank_has_map (io, bankid, id)) { // TODO: use threads here r_io_bank_update_map_boundaries (io, bankid, id, r_io_map_from (map), oto); } - } while (r_id_storage_get_next (io->banks, &bankid)); + } while (r_id_storage_get_next (&io->banks, &bankid)); return true; } diff --git a/libr/io/p_cache.c b/libr/io/p_cache.c index 9829367c1f2ad..c302eb40c0702 100644 --- a/libr/io/p_cache.c +++ b/libr/io/p_cache.c @@ -296,7 +296,8 @@ static bool __desc_cache_commit_cb(void *user, const ut64 k, const void *v) { R_API bool r_io_desc_cache_commit(RIODesc *desc) { RIODesc *current; - if (!desc || !(desc->perm & R_PERM_W) || !desc->io || !desc->io->files || !desc->io->p_cache) { + if (!desc || !(desc->perm & R_PERM_W) || !desc->io || + !desc->io->files.data || !desc->io->p_cache) { return false; } if (!desc->cache) { @@ -355,7 +356,7 @@ R_API void r_io_desc_cache_fini(RIODesc *desc) { } R_API void r_io_desc_cache_fini_all(RIO *io) { - if (io && io->files) { - r_id_storage_foreach (io->files, __desc_fini_cb, NULL); + if (io && io->files.data) { + r_id_storage_foreach (&io->files, __desc_fini_cb, NULL); } }