From: Frederic Suter Date: Thu, 16 Mar 2017 16:48:11 +0000 (+0100) Subject: better than static casting, use a container X-Git-Tag: v3_15~86 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/dd0269d32f2c27ff9b74ec7890caea1fdff55b50?hp=d50966bb4d6140a3cffa65cb45812a1f67c3bcf7 better than static casting, use a container --- diff --git a/include/xbt/replay.hpp b/include/xbt/replay.hpp index b7986e39d9..a317f34c92 100644 --- a/include/xbt/replay.hpp +++ b/include/xbt/replay.hpp @@ -12,10 +12,15 @@ #include "xbt/dict.h" #ifdef __cplusplus #include +#include +#include namespace simgrid { namespace xbt { /* To split the file if a unique one is given (specific variable for the other case live in runner()) */ +typedef std::vector ReplayAction; +static std::unordered_map*> action_queues; + XBT_PUBLIC_DATA(std::ifstream*) action_fs; XBT_PUBLIC(bool) replay_is_active(); XBT_PUBLIC(void) replay_init(); @@ -28,7 +33,6 @@ XBT_PUBLIC(int) replay_runner(int argc, char* argv[]); SG_BEGIN_DECL() typedef void (*action_fun)(const char* const* args); -XBT_PUBLIC_DATA(xbt_dict_t) xbt_action_queues; XBT_PUBLIC(void) xbt_replay_action_register(const char* action_name, action_fun function); SG_END_DECL() diff --git a/src/msg/msg_actions.cpp b/src/msg/msg_actions.cpp index 567209f6b7..916aa87422 100644 --- a/src/msg/msg_actions.cpp +++ b/src/msg/msg_actions.cpp @@ -31,22 +31,18 @@ void MSG_action_exit() */ msg_error_t MSG_action_trace_run(char *path) { - msg_error_t res; - char *name; - xbt_dynar_t todo; - xbt_dict_cursor_t cursor; - if (path) { simgrid::xbt::action_fs = new std::ifstream(path, std::ifstream::in); } - res = MSG_main(); - if (!xbt_dict_is_empty(xbt_action_queues)) { + msg_error_t res = MSG_main(); + + if (!simgrid::xbt::action_queues.empty()) { XBT_WARN("Not all actions got consumed. If the simulation ended successfully (without deadlock)," " you may want to add new processes to your deployment file."); - xbt_dict_foreach(xbt_action_queues, cursor, name, todo) { - XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name); + for (auto actions_of : simgrid::xbt::action_queues) { + XBT_WARN("Still %zu actions for %s", actions_of.second->size(), actions_of.first.c_str()); } } @@ -55,9 +51,6 @@ msg_error_t MSG_action_trace_run(char *path) simgrid::xbt::action_fs = nullptr; } - xbt_dict_free(&xbt_action_queues); - xbt_action_queues = xbt_dict_new_homogeneous(nullptr); - return res; } diff --git a/src/xbt/xbt_replay.cpp b/src/xbt/xbt_replay.cpp index f2c6ae26d6..40b5cee45a 100644 --- a/src/xbt/xbt_replay.cpp +++ b/src/xbt/xbt_replay.cpp @@ -14,14 +14,10 @@ #include #include #include -#include -#include -#include #include XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader"); -xbt_dict_t xbt_action_queues = nullptr; bool is_replay_active = false; namespace simgrid { @@ -29,7 +25,6 @@ namespace xbt { std::ifstream* action_fs = nullptr; std::unordered_map action_funs; -typedef std::vector ReplayAction; static void read_and_trim_line(std::ifstream* fs, std::string* line) { @@ -78,15 +73,12 @@ bool ReplayReader::get(ReplayAction* action) void replay_init() { if (!is_replay_active) { - xbt_action_queues = xbt_dict_new_homogeneous(nullptr); is_replay_active = true; } } void replay_exit() { - xbt_dict_free(&xbt_action_queues); - xbt_action_queues = nullptr; } bool replay_is_active() @@ -98,8 +90,9 @@ static ReplayAction* get_action(char* name) { ReplayAction* action; - std::queue* myqueue = - static_cast*>(xbt_dict_get_or_null(xbt_action_queues, name)); + std::queue* myqueue = nullptr; + if (action_queues.find(std::string(name)) != action_queues.end()) + myqueue = action_queues.at(std::string(name)); if (myqueue == nullptr || myqueue->empty()) { // Nothing stored for me. Read the file further if (action_fs == nullptr) { // File closed now. There's nothing more to read. I'm out of here goto todo_done; @@ -119,11 +112,12 @@ static ReplayAction* get_action(char* name) return action; } else { // Else, I have to store it for the relevant colleague - std::queue* otherqueue = - static_cast*>(xbt_dict_get_or_null(xbt_action_queues, evtname.c_str())); + std::queue* otherqueue = nullptr; + if (action_queues.find(evtname) != action_queues.end()) + otherqueue = action_queues.at(evtname); if (otherqueue == nullptr) { // Damn. Create the queue of that guy otherqueue = new std::queue(); - xbt_dict_set(xbt_action_queues, evtname.c_str(), otherqueue, nullptr); + action_queues.insert({evtname, otherqueue}); } otherqueue->push(action); } @@ -141,7 +135,7 @@ static ReplayAction* get_action(char* name) todo_done: if (myqueue != nullptr) { delete myqueue; - xbt_dict_remove(xbt_action_queues, name); + action_queues.erase(std::string(name)); } return nullptr; }