Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dict--
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Thu, 3 Aug 2017 08:24:34 +0000 (10:24 +0200)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Thu, 3 Aug 2017 08:24:34 +0000 (10:24 +0200)
src/kernel/activity/MailboxImpl.cpp
src/kernel/activity/MailboxImpl.hpp
src/s4u/s4u_mailbox.cpp

index 3f8cedc..330345b 100644 (file)
@@ -9,13 +9,13 @@
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_mailbox, simix, "Mailbox implementation");
 
-static xbt_dict_t mailboxes = xbt_dict_new_homogeneous([](void* data) {
-  delete static_cast<smx_mailbox_t>(data);
-});
+static std::map<std::string, smx_mailbox_t>* mailboxes = new std::map<std::string, smx_mailbox_t>;
 
 void SIMIX_mailbox_exit()
 {
-  xbt_dict_free(&mailboxes);
+  for (auto elm : *mailboxes)
+    delete elm.second;
+  delete mailboxes;
 }
 
 /******************************************************************************/
@@ -28,20 +28,25 @@ namespace activity {
 /** @brief Returns the mailbox of that name, or nullptr */
 MailboxImpl* MailboxImpl::byNameOrNull(const char* name)
 {
-  return static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
+  auto mbox = mailboxes->find(name);
+  if (mbox != mailboxes->end())
+    return mbox->second;
+  else
+    return nullptr;
 }
 /** @brief Returns the mailbox of that name, newly created on need */
 MailboxImpl* MailboxImpl::byNameOrCreate(const char* name)
 {
   xbt_assert(name, "Mailboxes must have a name");
   /* two processes may have pushed the same mbox_create simcall at the same time */
-  smx_mailbox_t mbox = static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
-  if (not mbox) {
-    mbox = new MailboxImpl(name);
+  auto m = mailboxes->find(name);
+  if (m == mailboxes->end()) {
+    smx_mailbox_t mbox = new MailboxImpl(name);
     XBT_DEBUG("Creating a mailbox at %p with name %s", mbox, name);
-    xbt_dict_set(mailboxes, mbox->name_, mbox, nullptr);
-  }
-  return mbox;
+    (*mailboxes)[mbox->name_] = mbox;
+    return mbox;
+  } else
+    return m->second;
 }
 /** @brief set the receiver of the mailbox to allow eager sends
  *  \param actor The receiving dude
@@ -68,14 +73,14 @@ void MailboxImpl::remove(smx_activity_t activity)
       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(activity);
 
   xbt_assert(comm->mbox == this, "Comm %p is in mailbox %s, not mailbox %s", comm.get(),
-             (comm->mbox ? comm->mbox->name_ : "(null)"), this->name_);
+             (comm->mbox ? comm->mbox->name_.c_str() : "(null)"), this->name_.c_str());
   comm->mbox = nullptr;
   for (auto it = this->comm_queue.begin(); it != this->comm_queue.end(); it++)
     if (*it == comm) {
       this->comm_queue.erase(it);
       return;
     }
-  xbt_die("Comm %p not found in mailbox %s", comm.get(), this->name_);
+  xbt_die("Comm %p not found in mailbox %s", comm.get(), this->name_.c_str());
 }
 }
 }
index 8c4541d..c3eb5d5 100644 (file)
@@ -21,12 +21,12 @@ namespace activity {
 
 class MailboxImpl {
   explicit MailboxImpl(const char* name)
-      : piface_(this), name_(xbt_strdup(name)), comm_queue(MAX_MAILBOX_SIZE), done_comm_queue(MAX_MAILBOX_SIZE)
+      : piface_(this), name_(name), comm_queue(MAX_MAILBOX_SIZE), done_comm_queue(MAX_MAILBOX_SIZE)
   {
   }
 
 public:
-  ~MailboxImpl() { xbt_free(name_); }
+  ~MailboxImpl() = default;
 
   static MailboxImpl* byNameOrNull(const char* name);
   static MailboxImpl* byNameOrCreate(const char* name);
@@ -34,7 +34,7 @@ public:
   void push(activity::CommImplPtr comm);
   void remove(smx_activity_t activity);
   simgrid::s4u::Mailbox piface_; // Our interface
-  char* name_;
+  std::string name_;
 
   simgrid::simix::ActorImplPtr permanent_receiver; // process which the mailbox is attached to
   boost::circular_buffer_space_optimized<smx_activity_t> comm_queue;
index b53d652..cb5b0a0 100644 (file)
@@ -18,7 +18,7 @@ namespace s4u {
 
 const char* Mailbox::getName()
 {
-  return pimpl_->name_;
+  return pimpl_->name_.c_str();
 }
 
 MailboxPtr Mailbox::byName(const char*name)
@@ -53,9 +53,7 @@ smx_activity_t Mailbox::front()
 }
 
 void Mailbox::setReceiver(ActorPtr actor) {
-  simix::kernelImmediate([this, actor]() {
-    this->pimpl_->setReceiver(actor);
-  });
+  simix::kernelImmediate([this, actor]() { this->pimpl_->setReceiver(actor); });
 }
 
 /** @brief get the receiver (process associated to the mailbox) */