1 /* Copyright (c) 2006-2018. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "simgrid/s4u/Engine.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include "simgrid/s4u/Storage.hpp"
9 #include "simgrid/storage.h"
10 #include "src/surf/StorageImpl.hpp"
13 #include <unordered_map>
17 template class Extendable<simgrid::s4u::Storage>;
22 void getStorageList(std::map<std::string, Storage*>* whereTo)
24 for (auto const& s : simgrid::s4u::Engine::get_instance()->get_all_storages())
25 whereTo->insert({s->get_name(), s});
28 Storage::Storage(std::string name, surf::StorageImpl* pimpl) : pimpl_(pimpl), name_(name)
30 simgrid::s4u::Engine::get_instance()->storage_register(name, this);
33 Storage* Storage::byName(std::string name)
35 return Engine::get_instance()->storage_by_name_or_null(name);
38 const std::string& Storage::get_name() const
43 const char* Storage::get_cname() const
48 const char* Storage::getType()
50 return pimpl_->typeId_.c_str();
53 Host* Storage::getHost()
58 std::map<std::string, std::string>* Storage::getProperties()
60 return simgrid::simix::simcall([this] { return pimpl_->get_properties(); });
63 const char* Storage::getProperty(std::string key)
65 return this->pimpl_->get_property(key);
68 void Storage::setProperty(std::string key, std::string value)
70 simgrid::simix::simcall([this, key, value] { this->pimpl_->set_property(key, value); });
73 sg_size_t Storage::read(sg_size_t size)
75 return simcall_storage_read(pimpl_, size);
78 sg_size_t Storage::write(sg_size_t size)
80 return simcall_storage_write(pimpl_, size);
86 simgrid::xbt::signal<void(s4u::Storage&)> Storage::on_creation;
87 simgrid::xbt::signal<void(s4u::Storage&)> Storage::on_destruction;
88 simgrid::xbt::signal<void(s4u::Storage&)> Storage::on_state_change;
91 } /* namespace simgrid */
93 /* **************************** Public C interface *************************** */
95 /** @addtogroup sg_storage_management
96 * (#sg_storage_t) and the functions for managing it.
99 /** \ingroup sg_storage_management
101 * \brief Returns the name of the #sg_storage_t.
103 * This functions checks whether a storage is a valid pointer or not and return its name.
105 const char* sg_storage_get_name(sg_storage_t storage)
107 xbt_assert((storage != nullptr), "Invalid parameters");
108 return storage->get_cname();
111 const char* sg_storage_get_host(sg_storage_t storage)
113 xbt_assert((storage != nullptr), "Invalid parameters");
114 return storage->getHost()->get_cname();
117 /** \ingroup sg_storage_management
118 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
119 * \param storage a storage
120 * \return a dict containing the properties
122 xbt_dict_t sg_storage_get_properties(sg_storage_t storage)
124 xbt_assert((storage != nullptr), "Invalid parameters (storage is nullptr)");
125 xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
126 std::map<std::string, std::string>* props = storage->getProperties();
127 if (props == nullptr)
129 for (auto const& elm : *props) {
130 xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
135 /** \ingroup sg_storage_management
136 * \brief Change the value of a given storage property
138 * \param storage a storage
139 * \param name a property name
140 * \param value what to change the property to
142 void sg_storage_set_property_value(sg_storage_t storage, const char* name, const char* value)
144 storage->setProperty(name, value);
147 /** \ingroup sg_storage_management
148 * \brief Returns the value of a given storage property
150 * \param storage a storage
151 * \param name a property name
152 * \return value of a property (or nullptr if property not set)
154 const char* sg_storage_get_property_value(sg_storage_t storage, const char* name)
156 return storage->getProperty(name);
159 /** \ingroup sg_storage_management
160 * \brief Finds a sg_storage_t using its name.
161 * \param name the name of a storage
162 * \return the corresponding storage
164 sg_storage_t sg_storage_get_by_name(const char* name)
166 return simgrid::s4u::Storage::byName(name);
169 /** \ingroup sg_storage_management
170 * \brief Returns a dynar containing all the storage elements declared at a given point of time
172 xbt_dynar_t sg_storages_as_dynar()
174 std::vector<simgrid::s4u::Storage*> storage_list = simgrid::s4u::Engine::get_instance()->get_all_storages();
175 xbt_dynar_t res = xbt_dynar_new(sizeof(sg_storage_t), nullptr);
176 for (auto const& s : storage_list)
177 xbt_dynar_push(res, &s);
181 void* sg_storage_get_data(sg_storage_t storage)
183 xbt_assert((storage != nullptr), "Invalid parameters");
184 return storage->getUserdata();
187 void sg_storage_set_data(sg_storage_t storage, void* data)
189 storage->setUserdata(data);
192 sg_size_t sg_storage_read(sg_storage_t storage, sg_size_t size)
194 return storage->read(size);
197 sg_size_t sg_storage_write(sg_storage_t storage, sg_size_t size)
199 return storage->write(size);