From: Frederic Suter Date: Mon, 31 Jul 2017 16:15:34 +0000 (+0200) Subject: move ugly dict closer to the C APIs X-Git-Tag: v3_17~302 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/d1a82dae27086cc6f9ea4e0163d2941c74135c94 move ugly dict closer to the C APIs + revise a bit an ugly simdag example --- diff --git a/examples/simdag/properties/sd_properties.c b/examples/simdag/properties/sd_properties.c index 697c10c83d..2e437de074 100644 --- a/examples/simdag/properties/sd_properties.c +++ b/examples/simdag/properties/sd_properties.c @@ -29,12 +29,13 @@ int main(int argc, char **argv) const char *name1 = sg_host_get_name(h1); const char *name2 = sg_host_get_name(h2); - /* Get the property list of 'host1' */ + /* Trying to set a new property */ + sg_host_set_property_value(h1, "NewProp", "newValue"); + + /* Get the property list of 'host1'. This is only a copy of the internal data structure.*/ XBT_INFO("Property list for host %s", name1); xbt_dict_t props = sg_host_get_properties(h1); - /* Trying to set a new property */ - xbt_dict_set(props, "NewProp", strdup("newValue"), NULL); /* Print the properties of 'host1' */ xbt_dict_foreach(props, cursor, key, data) { @@ -62,7 +63,7 @@ int main(int argc, char **argv) XBT_INFO("\tProperty: %s is undefined", exist); else { XBT_INFO("\tProperty: %s old value: %s", exist, value); - xbt_dict_set(props, exist, strdup("250"), NULL); + sg_host_set_property_value(h2, exist, "250"); } /* Test if we have changed the value */ diff --git a/include/simgrid/host.h b/include/simgrid/host.h index 1c418b2a82..d839c96423 100644 --- a/include/simgrid/host.h +++ b/include/simgrid/host.h @@ -46,6 +46,7 @@ XBT_PUBLIC(int) sg_host_get_pstate(sg_host_t host); XBT_PUBLIC(void) sg_host_set_pstate(sg_host_t host,int pstate); XBT_PUBLIC(xbt_dict_t) sg_host_get_properties(sg_host_t host); XBT_PUBLIC(const char*) sg_host_get_property_value(sg_host_t host, const char* name); +XBT_PUBLIC(void) sg_host_set_property_value(sg_host_t host, const char* name, const char* value); XBT_PUBLIC(void) sg_host_route(sg_host_t from, sg_host_t to, xbt_dynar_t links); XBT_PUBLIC(double) sg_host_route_latency(sg_host_t from, sg_host_t to); XBT_PUBLIC(double) sg_host_route_bandwidth(sg_host_t from, sg_host_t to); diff --git a/include/simgrid/s4u/Host.hpp b/include/simgrid/s4u/Host.hpp index d43c985fcf..33f9710ca2 100644 --- a/include/simgrid/s4u/Host.hpp +++ b/include/simgrid/s4u/Host.hpp @@ -86,7 +86,7 @@ public: double getSpeed(); int getCoreCount(); - xbt_dict_t getProperties(); + std::unordered_map* getProperties(); const char* getProperty(const char* key); void setProperty(const char* key, const char* value); void getProcesses(std::vector * list); diff --git a/include/simgrid/s4u/Storage.hpp b/include/simgrid/s4u/Storage.hpp index 2ece591608..e3b1d90354 100644 --- a/include/simgrid/s4u/Storage.hpp +++ b/include/simgrid/s4u/Storage.hpp @@ -36,7 +36,7 @@ public: sg_size_t getSizeFree(); sg_size_t getSizeUsed(); - xbt_dict_t getProperties(); + std::unordered_map* getProperties(); const char* getProperty(const char* key); void setProperty(const char* key, const char* value); std::map* getContent(); diff --git a/src/msg/msg_host.cpp b/src/msg/msg_host.cpp index fa83f621ae..82a9ad772f 100644 --- a/src/msg/msg_host.cpp +++ b/src/msg/msg_host.cpp @@ -165,7 +165,14 @@ const char *MSG_host_get_property_value(msg_host_t host, const char *name) xbt_dict_t MSG_host_get_properties(msg_host_t host) { xbt_assert((host != nullptr), "Invalid parameters (host is nullptr)"); - return host->getProperties(); + xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f); + std::unordered_map* props = host->getProperties(); + if (props == nullptr) + return nullptr; + for (auto elm : *props) { + xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr); + } + return as_dict; } /** \ingroup m_host_management diff --git a/src/msg/msg_io.cpp b/src/msg/msg_io.cpp index 1913d3bde3..648db4dc00 100644 --- a/src/msg/msg_io.cpp +++ b/src/msg/msg_io.cpp @@ -388,7 +388,14 @@ sg_size_t MSG_storage_get_used_size(msg_storage_t storage) xbt_dict_t MSG_storage_get_properties(msg_storage_t storage) { xbt_assert((storage != nullptr), "Invalid parameters (storage is nullptr)"); - return storage->getProperties(); + xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f); + std::unordered_map* props = storage->getProperties(); + if (props == nullptr) + return nullptr; + for (auto elm : *props) { + xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr); + } + return as_dict; } /** \ingroup msg_storage_management @@ -470,14 +477,14 @@ void *MSG_storage_get_data(msg_storage_t storage) xbt_dict_t MSG_storage_get_content(msg_storage_t storage) { std::map* content = storage->getContent(); - xbt_dict_t content_dict = xbt_dict_new_homogeneous(&free); + xbt_dict_t content_as_dict = xbt_dict_new_homogeneous(xbt_free_f); for (auto entry : *content) { sg_size_t* psize = static_cast(malloc(sizeof(sg_size_t))); *psize = entry.second; - xbt_dict_set(content_dict, entry.first.c_str(), psize, nullptr); + xbt_dict_set(content_as_dict, entry.first.c_str(), psize, nullptr); } - return content_dict; + return content_as_dict; } /** \ingroup msg_storage_management diff --git a/src/s4u/s4u_host.cpp b/src/s4u/s4u_host.cpp index f39b2a2969..9dafa33ce0 100644 --- a/src/s4u/s4u_host.cpp +++ b/src/s4u/s4u_host.cpp @@ -177,7 +177,7 @@ void Host::routeTo(Host* dest, std::vector* links, double* late } /** Get the properties assigned to a host */ -xbt_dict_t Host::getProperties() +std::unordered_map* Host::getProperties() { return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); diff --git a/src/s4u/s4u_storage.cpp b/src/s4u/s4u_storage.cpp index 74770d9ea6..58ad8f4c28 100644 --- a/src/s4u/s4u_storage.cpp +++ b/src/s4u/s4u_storage.cpp @@ -60,7 +60,7 @@ sg_size_t Storage::getSize() return pimpl_->getSize(); } -xbt_dict_t Storage::getProperties() +std::unordered_map* Storage::getProperties() { return simgrid::simix::kernelImmediate([this] { return pimpl_->getProperties(); }); } diff --git a/src/simgrid/host.cpp b/src/simgrid/host.cpp index 1deab79569..07f801756d 100644 --- a/src/simgrid/host.cpp +++ b/src/simgrid/host.cpp @@ -180,7 +180,14 @@ void sg_host_set_pstate(sg_host_t host,int pstate) { /** @brief Get the properties of an host */ xbt_dict_t sg_host_get_properties(sg_host_t host) { - return host->getProperties(); + xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f); + std::unordered_map* props = host->getProperties(); + if (props == nullptr) + return nullptr; + for (auto elm : *props) { + xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr); + } + return as_dict; } /** \ingroup m_host_management @@ -192,8 +199,14 @@ xbt_dict_t sg_host_get_properties(sg_host_t host) { */ const char *sg_host_get_property_value(sg_host_t host, const char *name) { - return (const char*) xbt_dict_get_or_null(sg_host_get_properties(host), name); + return host->getProperty(name); +} + +void sg_host_set_property_value(sg_host_t host, const char* name, const char* value) +{ + host->setProperty(name, value); } + /** * \brief Find a route between two hosts * @@ -244,21 +257,15 @@ double sg_host_route_bandwidth(sg_host_t from, sg_host_t to) /** @brief Displays debugging information about a host */ void sg_host_dump(sg_host_t host) { - xbt_dict_t props; - XBT_INFO("Displaying host %s", host->getCname()); XBT_INFO(" - speed: %.0f", host->getSpeed()); XBT_INFO(" - available speed: %.2f", sg_host_get_available_speed(host)); - props = host->getProperties(); + std::unordered_map* props = host->getProperties(); - if (not xbt_dict_is_empty(props)) { + if (not props->empty()) { XBT_INFO(" - properties:"); - xbt_dict_cursor_t cursor = nullptr; - char* key; - char* data; - - xbt_dict_foreach(props,cursor,key,data) { - XBT_INFO(" %s->%s",key,data); + for (auto elm : *props) { + XBT_INFO(" %s->%s", elm.first.c_str(), elm.second.c_str()); } } } diff --git a/src/surf/PropertyHolder.cpp b/src/surf/PropertyHolder.cpp index 2eda708ce0..03210a7ce2 100644 --- a/src/surf/PropertyHolder.cpp +++ b/src/surf/PropertyHolder.cpp @@ -3,7 +3,6 @@ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ -#include "xbt/sysdep.h" #include "PropertyHolder.hpp" namespace simgrid { @@ -12,27 +11,32 @@ namespace surf { PropertyHolder::PropertyHolder() = default; PropertyHolder::~PropertyHolder() { - xbt_dict_free(&properties_); + delete properties_; } /** @brief Return the property associated to the provided key (or nullptr if not existing) */ const char *PropertyHolder::getProperty(const char*key) { if (properties_ == nullptr) return nullptr; - return static_cast(xbt_dict_get_or_null(properties_,key)); + try { + return properties_->at(key).c_str(); + } catch (std::out_of_range& unfound) { + return nullptr; + } } /** @brief Change the value of a given key in the property set */ void PropertyHolder::setProperty(const char*key, const char*value) { if (not properties_) - properties_ = xbt_dict_new_homogeneous(xbt_free_f); - xbt_dict_set(properties_, key, xbt_strdup(value), nullptr); + properties_ = new std::unordered_map; + (*properties_)[key] = value; } /** @brief Return the whole set of properties. Don't mess with it, dude! */ -xbt_dict_t PropertyHolder::getProperties() { +std::unordered_map* PropertyHolder::getProperties() +{ if (not properties_) - properties_ = xbt_dict_new_homogeneous(xbt_free_f); + properties_ = new std::unordered_map; return properties_; } diff --git a/src/surf/PropertyHolder.hpp b/src/surf/PropertyHolder.hpp index ba06640feb..afbdbc2491 100644 --- a/src/surf/PropertyHolder.hpp +++ b/src/surf/PropertyHolder.hpp @@ -5,7 +5,7 @@ #ifndef SRC_SURF_PROPERTYHOLDER_HPP_ #define SRC_SURF_PROPERTYHOLDER_HPP_ -#include +#include namespace simgrid { namespace surf { @@ -26,9 +26,10 @@ public: /* FIXME: This should not be exposed, as users may do bad things with the dict they got (it's not a copy). * But some user API expose this call so removing it is not so easy. */ - xbt_dict_t getProperties(); + std::unordered_map* getProperties(); + private: - xbt_dict_t properties_ = nullptr; + std::unordered_map* properties_ = nullptr; }; } /* namespace surf */ diff --git a/src/surf/sg_platf.cpp b/src/surf/sg_platf.cpp index cd318395e1..ec195988b1 100644 --- a/src/surf/sg_platf.cpp +++ b/src/surf/sg_platf.cpp @@ -100,7 +100,6 @@ void sg_platf_new_host(sg_platf_host_cbarg_t args) host->pimpl_cpu->setPState(args->pstate); if (args->coord && strcmp(args->coord, "")) new simgrid::kernel::routing::vivaldi::Coords(host->pimpl_netpoint, args->coord); - } /** @brief Add a "router" to the network element list */ diff --git a/teshsuite/s4u/storage_client_server/storage_client_server.cpp b/teshsuite/s4u/storage_client_server/storage_client_server.cpp index db4614c31e..7d71f9365d 100644 --- a/teshsuite/s4u/storage_client_server/storage_client_server.cpp +++ b/teshsuite/s4u/storage_client_server/storage_client_server.cpp @@ -10,15 +10,13 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(storage, "Messages specific for this simulation"); static void display_storage_properties(simgrid::s4u::Storage* storage) { - xbt_dict_t props = storage->getProperties(); - if (xbt_dict_length(props) > 0) { + std::unordered_map* props = storage->getProperties(); + if (not props->empty()) { XBT_INFO("\tProperties of mounted storage: %s", storage->getName()); - xbt_dict_cursor_t cursor = NULL; - char* key; - char* data; - xbt_dict_foreach (props, cursor, key, data) - XBT_INFO("\t\t'%s' -> '%s'", key, data); + for (auto elm : *props) { + XBT_INFO(" %s->%s", elm.first.c_str(), elm.second.c_str()); + } } else { XBT_INFO("\tNo property attached."); } diff --git a/teshsuite/simdag/flatifier/flatifier.cpp b/teshsuite/simdag/flatifier/flatifier.cpp index bd04422b6d..bb2aa74919 100644 --- a/teshsuite/simdag/flatifier/flatifier.cpp +++ b/teshsuite/simdag/flatifier/flatifier.cpp @@ -81,6 +81,7 @@ static void dump_platform() } else { std::printf("/>\n"); } + xbt_dict_free(&props); } // Routers