Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
plug a huge memleak in regular communications
[simgrid.git] / src / kernel / activity / MailboxImpl.cpp
1 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/kernel/activity/MailboxImpl.hpp"
7
8 #include "src/kernel/activity/CommImpl.hpp"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_mailbox, simix, "Mailbox implementation");
11
12 static xbt_dict_t mailboxes = xbt_dict_new_homogeneous([](void* data) {
13   delete static_cast<smx_mailbox_t>(data);
14 });
15
16 void SIMIX_mailbox_exit()
17 {
18   xbt_dict_free(&mailboxes);
19 }
20
21 /******************************************************************************/
22 /*                           Rendez-Vous Points                               */
23 /******************************************************************************/
24
25 namespace simgrid {
26 namespace kernel {
27 namespace activity {
28 /** @brief Returns the mailbox of that name, or nullptr */
29 MailboxImpl* MailboxImpl::byNameOrNull(const char* name)
30 {
31   return static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
32 }
33 /** @brief Returns the mailbox of that name, newly created on need */
34 MailboxImpl* MailboxImpl::byNameOrCreate(const char* name)
35 {
36   xbt_assert(name, "Mailboxes must have a name");
37   /* two processes may have pushed the same mbox_create simcall at the same time */
38   smx_mailbox_t mbox = static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
39   if (not mbox) {
40     mbox = new MailboxImpl(name);
41     XBT_DEBUG("Creating a mailbox at %p with name %s", mbox, name);
42     xbt_dict_set(mailboxes, mbox->name_, mbox, nullptr);
43   }
44   return mbox;
45 }
46 /** @brief set the receiver of the mailbox to allow eager sends
47  *  \param actor The receiving dude
48  */
49 void MailboxImpl::setReceiver(s4u::ActorPtr actor)
50 {
51   this->permanent_receiver = actor.get()->getImpl();
52 }
53 /** @brief Pushes a communication activity into a mailbox
54  *  @param comm What to add
55  */
56 void MailboxImpl::push(activity::CommImpl* comm)
57 {
58   this->comm_queue.push_back(comm);
59   comm->mbox = this;
60 }
61
62 /** @brief Removes a communication activity from a mailbox
63  *  @param activity What to remove
64  */
65 void MailboxImpl::remove(smx_activity_t activity)
66 {
67   simgrid::kernel::activity::CommImpl* comm = static_cast<simgrid::kernel::activity::CommImpl*>(activity);
68
69   comm->mbox = nullptr;
70   for (auto it = this->comm_queue.begin(); it != this->comm_queue.end(); it++)
71     if (*it == comm) {
72       this->comm_queue.erase(it);
73       return;
74     }
75   xbt_die("Cannot remove the comm %p that is not part of the mailbox %s", comm, this->name_);
76 }
77 }
78 }
79 }