Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Revert "Revert "cast once for all at surf level and not in the APIs""
[simgrid.git] / src / s4u / s4u_storage.cpp
1 /* Copyright (c) 2006-2015, 2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/s4u/storage.hpp"
8 #include "simgrid/simix.hpp"
9 #include "src/surf/storage_interface.hpp"
10 #include "xbt/lib.h"
11 #include <unordered_map>
12
13 extern xbt_lib_t storage_lib;
14
15 namespace simgrid {
16 namespace s4u {
17
18 std::unordered_map<std::string, Storage*>* Storage::storages_ = new std::unordered_map<std::string, Storage*>();
19
20 Storage::Storage(std::string name, smx_storage_t inferior) :
21     name_(name), pimpl_(inferior)
22 {
23   hostname_ = surf_storage_get_host(pimpl_);
24   size_     = surf_storage_get_size(pimpl_);
25   storages_->insert({name, this});
26 }
27
28 Storage::~Storage() = default;
29
30 smx_storage_t Storage::inferior()
31 {
32   return pimpl_;
33 }
34
35 Storage& Storage::byName(const char* name)
36 {
37   s4u::Storage* res = nullptr;
38   try {
39     res = storages_->at(name);
40   } catch (std::out_of_range& e) {
41     smx_storage_t inferior = xbt_lib_get_elm_or_null(storage_lib,name);
42     if (inferior == nullptr)
43       xbt_die("Storage %s does not exist. Please only use the storages that are defined in your platform.", name);
44
45     res = new Storage(name,inferior);
46   }
47   return *res;
48 }
49
50 const char* Storage::name()
51 {
52   return name_.c_str();
53 }
54
55 const char* Storage::host()
56 {
57   return hostname_.c_str();
58 }
59
60 sg_size_t Storage::sizeFree()
61 {
62   return simcall_storage_get_free_size(pimpl_);
63 }
64
65 sg_size_t Storage::sizeUsed()
66 {
67   return simcall_storage_get_used_size(pimpl_);
68 }
69
70 sg_size_t Storage::size() {
71   return size_;
72 }
73
74 xbt_dict_t Storage::properties()
75 {
76   return simcall_storage_get_properties(pimpl_);
77 }
78
79 const char* Storage::property(const char* key)
80 {
81   return static_cast<const char*>(xbt_dict_get_or_null(this->properties(), key));
82 }
83
84 void Storage::setProperty(const char* key, char* value)
85 {
86   xbt_dict_set(this->properties(), key, value, nullptr);
87 }
88
89 std::map<std::string, sg_size_t*>* Storage::content()
90 {
91   return simgrid::simix::kernelImmediate([this] { return surf_storage_resource_priv(this->pimpl_)->getContent(); });
92 }
93
94 std::unordered_map<std::string, Storage*>* Storage::allStorages()
95 {
96   return storages_;
97 }
98
99 } /* namespace s4u */
100 } /* namespace simgrid */