diff --git a/libIRDB/include/core/IRDB_Objects.hpp b/libIRDB/include/core/IRDB_Objects.hpp index 464ab7d796bad6c086ff0b1e25d9d589bf40c1ce..41f59ecf4ec35ffa28361c65d44acd860fcffaff 100644 --- a/libIRDB/include/core/IRDB_Objects.hpp +++ b/libIRDB/include/core/IRDB_Objects.hpp @@ -24,15 +24,16 @@ class IRDBObjects_t ~IRDBObjects_t(); // Add/delete file IRs for a variant. - // Cannot delete a file IR if it is also owned outside this object. + // Step does not have ownership of fileIR (can't make assumptions about its + // lifetime!), and a call to DeleteFileIR will render any pointers to that fileIR dangling. // AddFileIR returns the added IR or the preexistent IR if it was already added. - std::shared_ptr<FileIR_t> addFileIR(const db_id_t variant_id, const db_id_t file_id); // Returns NULL if no such file exists + FileIR_t* addFileIR(const db_id_t variant_id, const db_id_t file_id); // Returns NULL if no such file exists void deleteFileIR(const db_id_t file_id); // Add or delete a variant - // Cannot delete a variant if it or its files are also owned outside this object. + // Step does not have ownership of variant (can't make assumptions about its lifetime!), and a call to DeleteVariant will render any pointers to that variant dangling. // Deleting a variant does NOT write its files' IRs, but DOES delete them! // AddVariant returns the added variant or the preexistent variant if it was already added. - std::shared_ptr<VariantID_t> addVariant(const db_id_t variant_id); + VariantID_t* addVariant(const db_id_t variant_id); void deleteVariant(db_id_t variant_id); // Get DB interface @@ -50,20 +51,25 @@ class IRDBObjects_t std::unique_ptr<pqxxDB_t> pqxx_interface; // type aliases of maps. maps allow speed of finding needed files, file IRs // and/or variants that have already been read from the DB - using IdToVariantMap_t = std::map<db_id_t, std::shared_ptr<VariantID_t>>; + using IdToVariantMap_t = std::map<db_id_t, std::unique_ptr<VariantID_t>>; struct FileIRInfo_t { File_t * file; - std::shared_ptr<FileIR_t> fileIR; + std::unique_ptr<FileIR_t> fileIR; + + FileIRInfo_t() : file(nullptr), fileIR(nullptr) + { + } + }; using IdToFileIRInfoMap_t = std::map<db_id_t, FileIRInfo_t>; IdToVariantMap_t variant_map; IdToFileIRInfoMap_t file_IR_map; + + // minor helper + bool filesAlreadyPresent(const std::set<File_t*>&) const; - // minor helpers (used to check assertions) - bool filesAlreadyPresent(const FileSet_t& the_files) const; - bool filesBeingShared(const VariantID_t* the_variant) const; }; #endif diff --git a/libIRDB/src/core/IRDB_Objects.cpp b/libIRDB/src/core/IRDB_Objects.cpp index 3cbd07edbdbf1654983c48598e6e0b3506502981..30e011df8d68726da4fb8b5b89a625d6bd86d0d9 100644 --- a/libIRDB/src/core/IRDB_Objects.cpp +++ b/libIRDB/src/core/IRDB_Objects.cpp @@ -15,11 +15,11 @@ using namespace std; IRDBObjects_t::~IRDBObjects_t() { // All dynamically allocated DB objects - // are held as shared pointers and don't need to be + // are held as unique pointers and don't need to be // explicitly deleted. } -shared_ptr<FileIR_t> IRDBObjects_t::addFileIR(const db_id_t variant_id, const db_id_t file_id) +FileIR_t* IRDBObjects_t::addFileIR(const db_id_t variant_id, const db_id_t file_id) { const auto it = file_IR_map.find(file_id); @@ -39,13 +39,13 @@ shared_ptr<FileIR_t> IRDBObjects_t::addFileIR(const db_id_t variant_id, const db assert(variant_map.find(variant_id) != variant_map.end()); const auto & the_variant = *(variant_map.at(variant_id).get()); - the_fileIR = make_shared<FileIR_t>(the_variant, the_file); + the_fileIR = unique_ptr<FileIR_t>(new FileIR_t(the_variant, the_file)); assert(the_fileIR != NULL); } // make sure static variable is set in the calling module -- IMPORTANT the_fileIR->SetArchitecture(); - return the_fileIR; + return the_fileIR.get(); } } @@ -92,9 +92,6 @@ void IRDBObjects_t::deleteFileIR(const db_id_t file_id) auto& the_fileIR = (it->second).fileIR; if(the_fileIR != NULL) { - // To prevent reading in the same fileIR again while it is being used - // somewhere else, which could lead to desynchronization - assert(the_fileIR.use_count() <= 2); the_fileIR.reset(); } } @@ -111,46 +108,33 @@ bool IRDBObjects_t::filesAlreadyPresent(const set<File_t*>& the_files) const return missing_file_it==the_files.end(); } -shared_ptr<VariantID_t> IRDBObjects_t::addVariant(const db_id_t variant_id) +VariantID_t* IRDBObjects_t::addVariant(const db_id_t variant_id) { const auto var_it = variant_map.find(variant_id); if(var_it != variant_map.end()) { - return var_it->second; + return (var_it->second).get(); } - auto the_variant = make_shared<VariantID_t>(variant_id); + variant_map[variant_id].reset(new VariantID_t(variant_id)); + auto the_variant = variant_map[variant_id].get(); - assert(the_variant->IsRegistered()==true); - // disallow variants that share shallow copies to both be read in + assert(the_variant->IsRegistered()==true); + // disallow variants that share shallow copies to both be read in // to prevent desynchronization. assert(!filesAlreadyPresent(the_variant->GetFiles())); - variant_map[variant_id]=the_variant; // add files for(auto &curr_file : the_variant->GetFiles()) { - auto curr_file_IR = shared_ptr<FileIR_t>(); - auto file_IR_info = FileIRInfo_t({curr_file, curr_file_IR}); - file_IR_map[curr_file->GetBaseID()]=file_IR_info; + file_IR_map[curr_file->GetBaseID()]=FileIRInfo_t(); + file_IR_map[curr_file->GetBaseID()].file = curr_file; } return the_variant; } -bool IRDBObjects_t::filesBeingShared(const VariantID_t* the_variant) const -{ - - const auto &all_files=the_variant->GetFiles(); - const auto shared_file_it=find_if(ALLOF(all_files), [&](const File_t* file) - { - const auto file_IR_info = file_IR_map.at(file->GetBaseID()); - return (file_IR_info.fileIR.use_count() > 2); - }); - return shared_file_it!=all_files.end(); -} - int IRDBObjects_t::writeBackVariant(const db_id_t variant_id) { @@ -186,11 +170,6 @@ void IRDBObjects_t::deleteVariant(const db_id_t variant_id) if(var_it == variant_map.end()) return; - // To prevent reading in the same files again while they are being used - // somewhere else, which could lead to desynchronization - assert(!filesBeingShared(var_it->second.get())); - assert(var_it->second.use_count() <= 2); - // remove files and file IRs for(const auto file : var_it->second->GetFiles()) { diff --git a/libIRDB/test/fill_in_cfg.cpp b/libIRDB/test/fill_in_cfg.cpp index 291ee5182ffe36ff1aa987f179cc71dce8ef745d..c1f051109beffe2bbd009bcff9b1af147f85bf57 100644 --- a/libIRDB/test/fill_in_cfg.cpp +++ b/libIRDB/test/fill_in_cfg.cpp @@ -619,10 +619,10 @@ int PopulateCFG::executeStep(IRDBObjects_t *const irdb_objects) // now set the DB interface for THIS PLUGIN LIBRARY -- VERY IMPORTANT BaseObj_t::SetInterface(pqxx_interface); - const shared_ptr<VariantID_t> variant = irdb_objects->addVariant(variant_id); + const auto variant = irdb_objects->addVariant(variant_id); for(File_t* file : variant->GetFiles()) { - const shared_ptr<FileIR_t> firp = irdb_objects->addFileIR(variant_id, file->GetBaseID()); + const auto firp = irdb_objects->addFileIR(variant_id, file->GetBaseID()); assert(firp); cout<<"Filling in cfg for "<<firp->GetFile()->GetURL()<<endl; @@ -635,12 +635,12 @@ int PopulateCFG::executeStep(IRDBObjects_t *const irdb_objects) elfiop.reset(new exeio()); elfiop->load(string("readeh_tmp_file.exe")); - fill_in_cfg(firp.get()); - fill_in_scoops(firp.get()); + fill_in_cfg(firp); + fill_in_scoops(firp); if (fix_landing_pads) { - fill_in_landing_pads(firp.get()); + fill_in_landing_pads(firp); } } } diff --git a/libIRDB/test/fill_in_indtargs.cpp b/libIRDB/test/fill_in_indtargs.cpp index 5bc7d18d23350e6972ab43b75d39b043b200fb7e..0334551a1370bebc0e17bd0ff4ea86b4e037d093 100644 --- a/libIRDB/test/fill_in_indtargs.cpp +++ b/libIRDB/test/fill_in_indtargs.cpp @@ -2988,9 +2988,9 @@ int executeStep(IRDBObjects_t *const irdb_objects) elfiop->load(string("readeh_tmp_file.exe")); // find all indirect branch targets - fill_in_indtargs(firp.get(), elfiop.get(), do_unpin_opt); + fill_in_indtargs(firp, elfiop.get(), do_unpin_opt); if(split_eh_frame_opt) - split_eh_frame(firp.get()); + split_eh_frame(firp); } if(getenv("FII_NOUPDATE")!=nullptr) diff --git a/libIRDB/test/fix_calls.cpp b/libIRDB/test/fix_calls.cpp index e76ae7b43fa496f99863584f1334c3acb516545d..6e36305c5361299f5342e1a6a913ea24c454c463 100644 --- a/libIRDB/test/fix_calls.cpp +++ b/libIRDB/test/fix_calls.cpp @@ -1061,10 +1061,10 @@ int executeStep(IRDBObjects_t *const irdb_objects) EXEIO::dump::section_headers(cout,*elfiop); // do eh_frame reading as required. if(do_eh_frame) - read_ehframe(firp.get(), elfiop); + read_ehframe(firp, elfiop); - fix_all_calls(firp.get(),true,fix_all); - fix_other_pcrel(firp.get()); + fix_all_calls(firp,true,fix_all); + fix_other_pcrel(firp); cout<<"Done!"<<endl;