Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use ssize_t as return type for Comm::wait_any.
[simgrid.git] / examples / c / app-chainsend / peer.c
index a0036b2..022e851 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2020. The SimGrid Team.
+/* Copyright (c) 2012-2021. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -10,16 +10,16 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(chainsend_peer, "Messages specific for the peer");
 
 static void peer_join_chain(peer_t p)
 {
-  const chain_message_t msg = (chain_message_t)sg_mailbox_get(p->me);
-  p->prev                   = msg->prev_;
-  p->next                   = msg->next_;
-  p->total_pieces           = msg->num_pieces;
+  chain_message_t msg = (chain_message_t)sg_mailbox_get(p->me);
+  p->prev             = msg->prev_;
+  p->next             = msg->next_;
+  p->total_pieces     = msg->num_pieces;
   XBT_DEBUG("Peer %s got a 'BUILD_CHAIN' message (prev: %s / next: %s)", sg_mailbox_get_name(p->me),
             p->prev ? sg_mailbox_get_name(p->prev) : NULL, p->next ? sg_mailbox_get_name(p->next) : NULL);
   free(msg);
 }
 
-static void peer_forward_file(const peer_t p)
+static void peer_forward_file(peer_t p)
 {
   void* received;
   int done                = 0;
@@ -27,13 +27,11 @@ static void peer_forward_file(const peer_t p)
   size_t nb_pending_recvs = 0;
 
   while (!done) {
-    sg_comm_t comm                     = sg_mailbox_get_async(p->me, &received);
-    p->pending_recvs[nb_pending_recvs] = comm;
+    p->pending_recvs[nb_pending_recvs] = sg_mailbox_get_async(p->me, &received);
     nb_pending_recvs++;
 
-    int idx = sg_comm_wait_any(p->pending_recvs, nb_pending_recvs);
+    ssize_t idx = sg_comm_wait_any(p->pending_recvs, nb_pending_recvs);
     if (idx != -1) {
-      comm = p->pending_recvs[idx];
       XBT_DEBUG("Peer %s got a 'SEND_DATA' message", sg_mailbox_get_name(p->me));
       /* move the last pending comm where the finished one was, and decrement */
       p->pending_recvs[idx] = p->pending_recvs[--nb_pending_recvs];
@@ -58,15 +56,15 @@ static void peer_forward_file(const peer_t p)
   sg_comm_wait_all(p->pending_sends, nb_pending_sends);
 }
 
-static peer_t peer_init(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
+static peer_t peer_init(int argc, char* argv[])
 {
-  peer_t p           = (peer_t)malloc(sizeof(s_peer_t));
+  peer_t p           = xbt_malloc(sizeof(s_peer_t));
   p->prev            = NULL;
   p->next            = NULL;
   p->received_pieces = 0;
   p->received_bytes  = 0;
-  p->pending_recvs   = (sg_comm_t*)malloc(sizeof(sg_comm_t) * MAX_PENDING_COMMS);
-  p->pending_sends   = (sg_comm_t*)malloc(sizeof(sg_comm_t) * MAX_PENDING_COMMS);
+  p->pending_recvs   = xbt_malloc(sizeof(sg_comm_t) * MAX_PENDING_COMMS);
+  p->pending_sends   = xbt_malloc(sizeof(sg_comm_t) * MAX_PENDING_COMMS);
 
   p->me = sg_mailbox_by_name(sg_host_self_get_name());