Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replace linear search by a faster one.
[simgrid.git] / src / s4u / s4u_storage.cpp
1 /* Copyright (c) 2006-2015. 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
11 #include "xbt/lib.h"
12 extern xbt_lib_t storage_lib;
13
14 namespace simgrid {
15 namespace s4u {
16
17 boost::unordered_map <std::string, Storage *> *Storage::storages_ = new boost::unordered_map<std::string, Storage*> ();
18 Storage::Storage(std::string name, smx_storage_t inferior) :
19     name_(name), pimpl_(inferior)
20 {
21   size_ = SIMIX_storage_get_size(pimpl_);
22   storages_->insert({name, this});
23 }
24
25 Storage::~Storage() = default;
26
27 smx_storage_t Storage::inferior() {
28   return pimpl_;
29 }
30 Storage &Storage::byName(const char*name) {
31   s4u::Storage *res = nullptr;
32   try {
33     res = storages_->at(name);
34   } catch (std::out_of_range& e) {
35     smx_storage_t inferior = xbt_lib_get_elm_or_null(storage_lib,name);
36     if (inferior == nullptr)
37       xbt_die("Storage %s does not exist. Please only use the storages that are defined in your platform.", name);
38
39     res = new Storage(name,inferior);
40   }
41   return *res;
42 }
43
44 const char* Storage::name()
45 {
46   return name_.c_str();
47 }
48
49 sg_size_t Storage::sizeFree()
50 {
51   return simcall_storage_get_free_size(pimpl_);
52 }
53
54 sg_size_t Storage::sizeUsed()
55 {
56   return simcall_storage_get_used_size(pimpl_);
57 }
58
59 sg_size_t Storage::size() {
60   return size_;
61 }
62
63 xbt_dict_t Storage::properties()
64 {
65   return simcall_storage_get_properties(pimpl_);
66 }
67
68 std::map<std::string, sg_size_t*>* Storage::content()
69 {
70   return simgrid::simix::kernelImmediate(
71       [this] { return static_cast<simgrid::surf::Storage*>(surf_storage_resource_priv(this->pimpl_))->getContent(); });
72 }
73
74 } /* namespace s4u */
75 } /* namespace simgrid */