From: Frederic Suter Date: Thu, 10 Aug 2017 07:45:40 +0000 (+0200) Subject: stringification X-Git-Tag: v3_17~220 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/2750362474d7984ac4b5c684e9d432f8365d033e stringification --- diff --git a/src/surf/sg_platf.cpp b/src/surf/sg_platf.cpp index af11d65e98..abb66df8ab 100644 --- a/src/surf/sg_platf.cpp +++ b/src/surf/sg_platf.cpp @@ -190,14 +190,14 @@ void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster) } for (int i : *cluster->radicals) { - char * host_id = bprintf("%s%d%s", cluster->prefix, i, cluster->suffix); - char * link_id = bprintf("%s_link_%d", cluster->id, i); + std::string host_id = std::string(cluster->prefix) + std::to_string(i) + cluster->suffix; + std::string link_id = std::string(cluster->id) + "_link_" + std::to_string(i); - XBT_DEBUG("", host_id, cluster->speeds.front()); + XBT_DEBUG("", host_id.c_str(), cluster->speeds.front()); s_sg_platf_host_cbarg_t host; memset(&host, 0, sizeof(host)); - host.id = host_id; + host.id = host_id.c_str(); if ((cluster->properties != nullptr) && (not cluster->properties->empty())) { host.properties = new std::map; @@ -212,7 +212,7 @@ void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster) sg_platf_new_host(&host); XBT_DEBUG(""); - XBT_DEBUG("", link_id, cluster->bw, cluster->lat); + XBT_DEBUG("", link_id.c_str(), cluster->bw, cluster->lat); // All links are saved in a matrix; // every row describes a single node; every node may have multiple links. @@ -224,11 +224,11 @@ void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster) simgrid::surf::LinkImpl* linkUp = nullptr; simgrid::surf::LinkImpl* linkDown = nullptr; if(cluster->loopback_bw > 0 || cluster->loopback_lat > 0){ - std::string tmp_link = std::string(link_id) + "_loopback"; + std::string tmp_link = link_id + "_loopback"; XBT_DEBUG("", tmp_link.c_str(), cluster->loopback_bw); LinkCreationArgs link; - link.id = tmp_link.c_str(); + link.id = tmp_link; link.bandwidth = cluster->loopback_bw; link.latency = cluster->loopback_lat; link.policy = SURF_LINK_FATPIPE; @@ -248,7 +248,7 @@ void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster) XBT_DEBUG("", tmp_link.c_str(), cluster->limiter_link); LinkCreationArgs link; - link.id = tmp_link.c_str(); + link.id = tmp_link; link.bandwidth = cluster->limiter_link; link.latency = 0; link.policy = SURF_LINK_SHARED; @@ -266,8 +266,6 @@ void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster) current_as->create_links_for_node(cluster, i, rankId, rankId*current_as->linkCountPerNode_ + current_as->hasLoopback_ + current_as->hasLimiter_ ); } - xbt_free(link_id); - xbt_free(host_id); rankId++; } delete cluster->properties; @@ -276,9 +274,8 @@ void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster) XBT_DEBUG(" "); XBT_DEBUG("", cluster->router_id); if (not cluster->router_id || not strcmp(cluster->router_id, "")) { - char* newid = bprintf("%s%s_router%s", cluster->prefix, cluster->id, cluster->suffix); - current_as->router_ = sg_platf_new_router(newid, NULL); - free(newid); + std::string newid = std::string(cluster->prefix) + cluster->id + "_router" + cluster->suffix; + current_as->router_ = sg_platf_new_router(newid.c_str(), NULL); } else { current_as->router_ = sg_platf_new_router(cluster->router_id, NULL); } @@ -317,10 +314,10 @@ void routing_cluster_add_backbone(simgrid::surf::LinkImpl* bb) XBT_DEBUG("Add a backbone to AS '%s'", current_routing->getCname()); } -void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet) +void sg_platf_new_cabinet(CabinetCreationArgs* cabinet) { for (int radical : *cabinet->radicals) { - std::string hostname = std::string(cabinet->prefix) + std::to_string(radical) + std::string(cabinet->suffix); + std::string hostname = cabinet->prefix + std::to_string(radical) + cabinet->suffix; s_sg_platf_host_cbarg_t host; memset(&host, 0, sizeof(host)); host.pstate = 0; @@ -691,14 +688,14 @@ void sg_platf_new_hostlink(sg_platf_host_link_cbarg_t hostlink) as_cluster->privateLinks_.insert({netpoint->id(), {linkUp, linkDown}}); } -void sg_platf_new_trace(sg_platf_trace_cbarg_t trace) +void sg_platf_new_trace(TraceCreationArgs* trace) { tmgr_trace_t tmgr_trace; - if (trace->file && strcmp(trace->file, "") != 0) { + if (not trace->file.empty()) { tmgr_trace = tmgr_trace_new_from_file(trace->file); } else { - xbt_assert(strcmp(trace->pc_data, ""), - "Trace '%s' must have either a content, or point to a file on disk.",trace->id); + xbt_assert(not trace->pc_data.empty(), "Trace '%s' must have either a content, or point to a file on disk.", + trace->id.c_str()); tmgr_trace = tmgr_trace_new_from_string(trace->id, trace->pc_data, trace->periodicity); } traces_set_list.insert({trace->id, tmgr_trace}); diff --git a/src/surf/surf_interface.cpp b/src/surf/surf_interface.cpp index d5e7d0230a..39c8b9d258 100644 --- a/src/surf/surf_interface.cpp +++ b/src/surf/surf_interface.cpp @@ -39,8 +39,8 @@ simgrid::xbt::signal surfExitCallbacks; } } -#include // FIXME: this plugin should not be linked to the core -#include // FIXME: this plugin should not be linked to the core +#include // FIXME: this plug-in should not be linked to the core +#include // FIXME: this plug-in should not be linked to the core s_surf_model_description_t surf_plugin_description[] = { {"Energy", "Cpu energy consumption.", &sg_host_energy_plugin_init}, @@ -128,12 +128,12 @@ double surf_get_clock() # define FILE_DELIM "/" /* FIXME: move to better location */ #endif -std::ifstream* surf_ifsopen(const char* name) +std::ifstream* surf_ifsopen(std::string name) { std::ifstream* fs = new std::ifstream(); - xbt_assert(name); - if (__surf_is_absolute_file_path(name)) { /* don't mess with absolute file names */ - fs->open(name, std::ifstream::in); + xbt_assert(not name.empty()); + if (__surf_is_absolute_file_path(name.c_str())) { /* don't mess with absolute file names */ + fs->open(name.c_str(), std::ifstream::in); } /* search relative files in the path */ @@ -149,6 +149,7 @@ std::ifstream* surf_ifsopen(const char* name) return fs; } + FILE *surf_fopen(const char *name, const char *mode) { FILE *file = nullptr; diff --git a/src/surf/surf_private.h b/src/surf/surf_private.h index 7d3fa1f45c..fc90b62a9a 100644 --- a/src/surf/surf_private.h +++ b/src/surf/surf_private.h @@ -31,7 +31,7 @@ typedef enum { /* Generic functions common to all models */ XBT_PRIVATE FILE *surf_fopen(const char *name, const char *mode); -XBT_PRIVATE std::ifstream* surf_ifsopen(const char* name); +XBT_PRIVATE std::ifstream* surf_ifsopen(std::string name); /* The __surf_is_absolute_file_path() returns 1 if * file_path is a absolute file path, in the other diff --git a/src/surf/trace_mgr.cpp b/src/surf/trace_mgr.cpp index f100e2e1a0..e9d4f785f3 100644 --- a/src/surf/trace_mgr.cpp +++ b/src/surf/trace_mgr.cpp @@ -24,7 +24,7 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_trace, surf, "Surf trace management"); namespace tmgr = simgrid::trace_mgr; -static std::unordered_map trace_list; +static std::unordered_map trace_list; static inline bool doubleEq(double d1, double d2) { @@ -58,13 +58,13 @@ simgrid::trace_mgr::future_evt_set::~future_evt_set() } } -tmgr_trace_t tmgr_trace_new_from_string(const char* name, std::string input, double periodicity) +tmgr_trace_t tmgr_trace_new_from_string(std::string name, std::string input, double periodicity) { int linecount = 0; tmgr_trace_t trace = new simgrid::trace_mgr::trace(); tmgr::DatedValue* last_event = &(trace->event_list.back()); - xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name); + xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name.c_str()); std::vector list; boost::split(list, input, boost::is_any_of("\n\r")); @@ -80,10 +80,10 @@ tmgr_trace_t tmgr_trace_new_from_string(const char* name, std::string input, dou continue; xbt_assert(sscanf(val.c_str(), "%lg %lg\n", &event.date_, &event.value_) == 2, "%s:%d: Syntax error in trace\n%s", - name, linecount, input.c_str()); + name.c_str(), linecount, input.c_str()); xbt_assert(last_event->date_ <= event.date_, - "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s", name, linecount, + "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s", name.c_str(), linecount, last_event->date_, event.date_, input.c_str()); last_event->date_ = event.date_ - last_event->date_; @@ -98,18 +98,18 @@ tmgr_trace_t tmgr_trace_new_from_string(const char* name, std::string input, dou } } - trace_list.insert({xbt_strdup(name), trace}); + trace_list.insert({name, trace}); return trace; } -tmgr_trace_t tmgr_trace_new_from_file(const char *filename) +tmgr_trace_t tmgr_trace_new_from_file(std::string filename) { - xbt_assert(filename && filename[0], "Cannot parse a trace from the null or empty filename"); - xbt_assert(trace_list.find(filename) == trace_list.end(), "Refusing to define trace %s twice", filename); + xbt_assert(not filename.empty(), "Cannot parse a trace from an empty filename"); + xbt_assert(trace_list.find(filename) == trace_list.end(), "Refusing to define trace %s twice", filename.c_str()); std::ifstream* f = surf_ifsopen(filename); - xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", filename, (boost::join(surf_path, ":")).c_str()); + xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", filename.c_str(), (boost::join(surf_path, ":")).c_str()); std::stringstream buffer; buffer << f->rdbuf(); @@ -177,10 +177,8 @@ tmgr_trace_event_t simgrid::trace_mgr::future_evt_set::pop_leq(double date, doub void tmgr_finalize() { - for (auto kv : trace_list) { - xbt_free((char*)kv.first); + for (auto kv : trace_list) delete kv.second; - } trace_list.clear(); } diff --git a/src/surf/trace_mgr.hpp b/src/surf/trace_mgr.hpp index d78d4de20d..2cb74ce73a 100644 --- a/src/surf/trace_mgr.hpp +++ b/src/surf/trace_mgr.hpp @@ -33,8 +33,8 @@ XBT_PUBLIC(void) tmgr_trace_event_unref(tmgr_trace_event_t* trace_event); XBT_PUBLIC(void) tmgr_finalize(); -XBT_PUBLIC(tmgr_trace_t) tmgr_trace_new_from_file(const char* filename); -XBT_PUBLIC(tmgr_trace_t) tmgr_trace_new_from_string(const char* id, std::string input, double periodicity); +XBT_PUBLIC(tmgr_trace_t) tmgr_trace_new_from_file(std::string filename); +XBT_PUBLIC(tmgr_trace_t) tmgr_trace_new_from_string(std::string id, std::string input, double periodicity); SG_END_DECL() diff --git a/src/surf/xml/platf_private.hpp b/src/surf/xml/platf_private.hpp index b5ae88cec5..384bae6cea 100644 --- a/src/surf/xml/platf_private.hpp +++ b/src/surf/xml/platf_private.hpp @@ -116,16 +116,16 @@ typedef struct s_sg_platf_cluster_cbarg { e_surf_link_sharing_policy_t bb_sharing_policy; } s_sg_platf_cluster_cbarg_t; -typedef struct s_sg_platf_cabinet_cbarg* sg_platf_cabinet_cbarg_t; -typedef struct s_sg_platf_cabinet_cbarg { - const char* id; - const char* prefix; - const char* suffix; +class CabinetCreationArgs { +public: + std::string id; + std::string prefix; + std::string suffix; std::vector* radicals; double speed; double bw; double lat; -} s_sg_platf_cabinet_cbarg_t; +}; class StorageCreationArgs { public: @@ -158,20 +158,20 @@ typedef struct s_sg_platf_prop_cbarg { const char *value; } s_sg_platf_prop_cbarg_t; -typedef struct s_sg_platf_trace_cbarg *sg_platf_trace_cbarg_t; -typedef struct s_sg_platf_trace_cbarg { - const char *id; - const char *file; +class TraceCreationArgs { +public: + std::string id; + std::string file; double periodicity; - const char *pc_data; -} s_sg_platf_trace_cbarg_t; + std::string pc_data; +}; -typedef struct s_sg_platf_trace_connect_cbarg *sg_platf_trace_connect_cbarg_t; -typedef struct s_sg_platf_trace_connect_cbarg { +class TraceConnectCreationArgs { +public: e_surf_trace_connect_kind_t kind; - const char *trace; - const char *element; -} s_sg_platf_trace_connect_cbarg_t; + std::string trace; + std::string element; +}; typedef struct s_sg_platf_process_cbarg *sg_platf_process_cbarg_t; typedef struct s_sg_platf_process_cbarg { @@ -212,19 +212,19 @@ sg_platf_new_router(const char* name, const char* coords); // Add a XBT_PUBLIC(void) sg_platf_new_link(LinkCreationArgs* link); // Add a link to the currently described AS XBT_PUBLIC(void) sg_platf_new_peer(PeerCreationArgs* peer); // Add a peer to the currently described AS XBT_PUBLIC(void) sg_platf_new_cluster(sg_platf_cluster_cbarg_t clust); // Add a cluster to the currently described AS -XBT_PUBLIC(void) sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet); // Add a cabinet to the currently described AS +XBT_PUBLIC(void) sg_platf_new_cabinet(CabinetCreationArgs* cabinet); // Add a cabinet to the currently described AS XBT_PUBLIC(void) sg_platf_new_route (sg_platf_route_cbarg_t route); // Add a route XBT_PUBLIC(void) sg_platf_new_bypassRoute (sg_platf_route_cbarg_t bypassroute); // Add a bypassRoute -XBT_PUBLIC(void) sg_platf_new_trace(sg_platf_trace_cbarg_t trace); +XBT_PUBLIC(void) sg_platf_new_trace(TraceCreationArgs* trace); XBT_PUBLIC(void) sg_platf_new_storage(StorageCreationArgs* storage); // Add a storage to the currently described AS XBT_PUBLIC(void) sg_platf_new_storage_type(StorageTypeCreationArgs* storage_type); XBT_PUBLIC(void) sg_platf_new_mount(MountCreationArgs* mount); XBT_PUBLIC(void) sg_platf_new_process(sg_platf_process_cbarg_t process); -XBT_PRIVATE void sg_platf_trace_connect(sg_platf_trace_connect_cbarg_t trace_connect); +XBT_PRIVATE void sg_platf_trace_connect(TraceConnectCreationArgs* trace_connect); /* Prototypes of the functions offered by flex */ XBT_PUBLIC(int) surf_parse_lex(); diff --git a/src/surf/xml/surfxml_parseplatf.cpp b/src/surf/xml/surfxml_parseplatf.cpp index fb6e7b5e46..055b5fee3b 100644 --- a/src/surf/xml/surfxml_parseplatf.cpp +++ b/src/surf/xml/surfxml_parseplatf.cpp @@ -35,10 +35,11 @@ XBT_PRIVATE std::unordered_map trace_connect_list_link XBT_PRIVATE std::unordered_map trace_connect_list_link_lat; SG_BEGIN_DECL() -void sg_platf_trace_connect(sg_platf_trace_connect_cbarg_t trace_connect) +void sg_platf_trace_connect(TraceConnectCreationArgs* trace_connect) { xbt_assert(traces_set_list.find(trace_connect->trace) != traces_set_list.end(), - "Cannot connect trace %s to %s: trace unknown", trace_connect->trace, trace_connect->element); + "Cannot connect trace %s to %s: trace unknown", trace_connect->trace.c_str(), + trace_connect->element.c_str()); switch (trace_connect->kind) { case SURF_TRACE_CONNECT_KIND_HOST_AVAIL: diff --git a/src/surf/xml/surfxml_sax_cb.cpp b/src/surf/xml/surfxml_sax_cb.cpp index 9b844b499b..2b4ad7c1b0 100644 --- a/src/surf/xml/surfxml_sax_cb.cpp +++ b/src/surf/xml/surfxml_sax_cb.cpp @@ -639,14 +639,13 @@ void STag_surfxml_cluster(){ void STag_surfxml_cabinet(){ parse_after_config(); - s_sg_platf_cabinet_cbarg_t cabinet; - memset(&cabinet,0,sizeof(cabinet)); + CabinetCreationArgs cabinet; cabinet.id = A_surfxml_cabinet_id; cabinet.prefix = A_surfxml_cabinet_prefix; cabinet.suffix = A_surfxml_cabinet_suffix; - cabinet.speed = surf_parse_get_speed(A_surfxml_cabinet_speed, "speed of cabinet", cabinet.id); - cabinet.bw = surf_parse_get_bandwidth(A_surfxml_cabinet_bw, "bw of cabinet", cabinet.id); - cabinet.lat = surf_parse_get_time(A_surfxml_cabinet_lat, "lat of cabinet", cabinet.id); + cabinet.speed = surf_parse_get_speed(A_surfxml_cabinet_speed, "speed of cabinet", cabinet.id.c_str()); + cabinet.bw = surf_parse_get_bandwidth(A_surfxml_cabinet_bw, "bw of cabinet", cabinet.id.c_str()); + cabinet.lat = surf_parse_get_time(A_surfxml_cabinet_lat, "lat of cabinet", cabinet.id.c_str()); cabinet.radicals = explodesRadical(A_surfxml_cabinet_radical); sg_platf_new_cabinet(&cabinet); @@ -903,8 +902,7 @@ void ETag_surfxml_bypassZoneRoute() } void ETag_surfxml_trace(){ - s_sg_platf_trace_cbarg_t trace; - memset(&trace,0,sizeof(trace)); + TraceCreationArgs trace; trace.id = A_surfxml_trace_id; trace.file = A_surfxml_trace_file; @@ -917,7 +915,7 @@ void ETag_surfxml_trace(){ void STag_surfxml_trace___connect() { parse_after_config(); - s_sg_platf_trace_connect_cbarg_t trace_connect; + TraceConnectCreationArgs trace_connect; memset(&trace_connect,0,sizeof(trace_connect)); trace_connect.element = A_surfxml_trace___connect_element;