Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Save a couple of strdup/free.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Fri, 27 Oct 2017 13:26:36 +0000 (15:26 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Fri, 27 Oct 2017 13:27:55 +0000 (15:27 +0200)
src/surf/xml/surfxml_sax_cb.cpp

index d1c9456..6de0b44 100644 (file)
@@ -23,7 +23,17 @@ int ETag_surfxml_include_state();
 
 #include "simgrid_dtd.c"
 
-char* surf_parsed_filename = nullptr; // to locate parse error messages
+/*
+ * Stuff relative to the <include> tag
+ */
+static std::vector<YY_BUFFER_STATE> surf_input_buffer_stack;
+static std::vector<FILE*> surf_file_to_parse_stack;
+static std::vector<std::string> surf_parsed_filename_stack;
+
+static inline const char* surf_parsed_filename() // to locate parse error messages
+{
+  return surf_parsed_filename_stack.empty() ? nullptr : surf_parsed_filename_stack.back().c_str();
+}
 
 std::vector<simgrid::surf::LinkImpl*> parsed_link_list; /* temporary store of current list link of a route */
 
@@ -35,7 +45,7 @@ void surf_parse_assert(bool cond, std::string msg)
   if (not cond) {
     int lineno = surf_parse_lineno;
     cleanup();
-    XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename, lineno, msg.c_str());
+    XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename(), lineno, msg.c_str());
     surf_exit();
     xbt_die("Exiting now");
   }
@@ -45,7 +55,7 @@ void surf_parse_error(std::string msg)
 {
   int lineno = surf_parse_lineno;
   cleanup();
-  XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename, lineno, msg.c_str());
+  XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename(), lineno, msg.c_str());
   surf_exit();
   xbt_die("Exiting now");
 }
@@ -82,7 +92,7 @@ void surf_parse_assert_netpoint(std::string hostname, std::string pre, std::stri
 
 void surf_parse_warn(std::string msg)
 {
-  XBT_WARN("%s:%d: %s", surf_parsed_filename, surf_parse_lineno, msg.c_str());
+  XBT_WARN("%s:%d: %s", surf_parsed_filename(), surf_parse_lineno, msg.c_str());
 }
 
 double surf_parse_get_double(std::string s)
@@ -358,20 +368,12 @@ void ETag_surfxml_mount()
   sg_platf_new_mount(&mount);
 }
 
-/*
- * Stuff relative to the <include> tag
- */
-static std::vector<YY_BUFFER_STATE> surf_input_buffer_stack;
-static std::vector<FILE*> surf_file_to_parse_stack;
-static std::vector<char*> surf_parsed_filename_stack;
-
 void STag_surfxml_include()
 {
   XBT_ERROR("<include> tag is deprecated, and will be removed in SimGrid v3.18. Please stop using it now (or tell us why you need it).");
   parse_after_config();
   XBT_DEBUG("STag_surfxml_include '%s'",A_surfxml_include_file);
-  surf_parsed_filename_stack.push_back(surf_parsed_filename); // save old file name
-  surf_parsed_filename = xbt_strdup(A_surfxml_include_file);
+  surf_parsed_filename_stack.emplace_back(A_surfxml_include_file); // save file name
 
   surf_file_to_parse_stack.push_back(surf_file_to_parse); // save old file descriptor
 
@@ -416,7 +418,6 @@ int ETag_surfxml_include_state()
   surf_input_buffer_stack.pop_back();
 
   // Restore the filename for error messages
-  free(surf_parsed_filename);
   surf_parsed_filename_stack.pop_back();
 
   return 1;
@@ -457,19 +458,19 @@ void STag_surfxml_platform() {
              "Use simgrid_update_xml to update your file automatically. "
              "This program is installed automatically with SimGrid, or "
              "available in the tools/ directory of the source archive.",
-             surf_parsed_filename, version);
+             surf_parsed_filename(), version);
   if (version < 4.1) {
     XBT_INFO("You're using a v%.1f XML file (%s) while the current standard is v4.1 "
              "That's fine, the new version is backward compatible. \n\n"
              "Use simgrid_update_xml to update your file automatically. "
              "This program is installed automatically with SimGrid, or "
              "available in the tools/ directory of the source archive.",
-             version, surf_parsed_filename);
+             version, surf_parsed_filename());
   }
   xbt_assert(version <= 4.1, "******* FILE %s COMES FROM THE FUTURE (v:%.1f) *********\n "
                              "The most recent formalism that this version of SimGrid understands is v4.1.\n"
                              "Please update your code, or use another, more adapted, file.",
-             surf_parsed_filename, version);
+             surf_parsed_filename(), version);
 
   sg_platf_begin();
 }
@@ -1068,7 +1069,7 @@ void surf_parse_open(const char *file)
 {
   xbt_assert(file, "Cannot parse the nullptr file. Bypassing the parser is strongly deprecated nowadays.");
 
-  surf_parsed_filename = xbt_strdup(file);
+  surf_parsed_filename_stack.emplace_back(file);
   std::string dir      = simgrid::xbt::Path(file).getDirname();
   surf_path.push_back(dir);
 
@@ -1082,12 +1083,11 @@ void surf_parse_open(const char *file)
 
 void surf_parse_close()
 {
-  if (surf_parsed_filename) {
+  if (surf_parsed_filename()) {
     surf_path.pop_back();
   }
 
-  free(surf_parsed_filename);
-  surf_parsed_filename = nullptr;
+  surf_parsed_filename_stack.pop_back();
 
   if (surf_file_to_parse) {
     surf_parse__delete_buffer(surf_input_buffer);