Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make this example clean for valgrind.
authorArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Tue, 8 Oct 2013 13:56:03 +0000 (15:56 +0200)
committerArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Tue, 8 Oct 2013 15:13:02 +0000 (17:13 +0200)
examples/msg/chainsend/broadcaster.c
examples/msg/chainsend/common.h
examples/msg/chainsend/messages.c
examples/msg/chainsend/messages.h
examples/msg/chainsend/peer.c
examples/msg/chainsend/peer.h

index 934e14a..2eaea91 100644 (file)
@@ -5,13 +5,11 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_broadcaster,
 
 xbt_dynar_t build_hostlist_from_hostcount(int hostcount)
 {
-  xbt_dynar_t host_list = xbt_dynar_new(sizeof(char*), NULL);
-  char *hostname = NULL;
-  int i = 1;
+  xbt_dynar_t host_list = xbt_dynar_new(sizeof(char*), xbt_free_ref);
+  int i;
   
-  for (; i < hostcount+1; i++) {
-    hostname = xbt_new(char, HOSTNAME_LENGTH);
-    snprintf(hostname, HOSTNAME_LENGTH, "host%d", i);
+  for (i = 1; i <= hostcount; i++) {
+    char *hostname = bprintf("host%d", i);
     XBT_DEBUG("%s", hostname);
     xbt_dynar_push(host_list, &hostname);
   }
index eaf2de1..e28e098 100644 (file)
@@ -11,7 +11,4 @@ static XBT_INLINE void queue_pending_connection(msg_comm_t comm, xbt_dynar_t q)
 
 int process_pending_connections(xbt_dynar_t q);
 
-#define MESSAGE_SIZE 1
-#define HOSTNAME_LENGTH 20
-
 #endif /* KADEPLOY_COMMON_H */
index c91243c..16da5ac 100644 (file)
@@ -4,8 +4,10 @@ msg_task_t task_message_new(e_message_type type, unsigned int len, const char *i
 {
   message_t msg = xbt_new(s_message_t, 1);
   msg->type = type;
-  msg->issuer_hostname = issuer_hostname;
-  msg->mailbox = mailbox;
+  msg->issuer_hostname = xbt_strdup(issuer_hostname);
+  msg->mailbox = xbt_strdup(mailbox);
+  msg->prev_hostname = NULL;
+  msg->next_hostname = NULL;
   msg_task_t task = MSG_task_create(NULL, 0, len, msg);
 
   return task;
@@ -15,8 +17,8 @@ msg_task_t task_message_chain_new(const char *issuer_hostname, const char *mailb
 {
   msg_task_t task = task_message_new(MESSAGE_BUILD_CHAIN, MESSAGE_BUILD_CHAIN_SIZE, issuer_hostname, mailbox);
   message_t msg = MSG_task_get_data(task);
-  msg->prev_hostname = prev;
-  msg->next_hostname = next;
+  msg->prev_hostname = xbt_strdup(prev);
+  msg->next_hostname = xbt_strdup(next);
   msg->num_pieces = num_pieces;
 
   return task;
@@ -35,6 +37,8 @@ msg_task_t task_message_data_new(const char *issuer_hostname, const char *mailbo
 void task_message_delete(void *task)
 {
   message_t msg = MSG_task_get_data(task);
+  xbt_free(msg->issuer_hostname);
+  xbt_free(msg->mailbox);
   xbt_free(msg);
   MSG_task_destroy(task);
 }
index 7118d29..ea7141e 100644 (file)
@@ -17,10 +17,10 @@ typedef enum {
 /* Message struct */
 typedef struct s_message {
   e_message_type type;
-  const char *issuer_hostname;
-  const char *mailbox;
-  const char *prev_hostname;
-  const char *next_hostname;
+  char *issuer_hostname;
+  char *mailbox;
+  char *prev_hostname;
+  char *next_hostname;
   const char *data_block;
   unsigned int data_length;
   unsigned int num_pieces;
index 9275175..b394f3a 100644 (file)
@@ -93,12 +93,11 @@ void peer_init(peer_t p, int argc, char *argv[])
   p->bytes = 0;
   p->pending_recvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
   p->pending_sends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
-  p->me = xbt_new(char, HOSTNAME_LENGTH);
   /* Set mailbox name: use host number from argv or hostname if no argument given */
   if (argc > 1) {
-    snprintf(p->me, HOSTNAME_LENGTH, "host%s", argv[1]);
+    p->me = bprintf("host%s", argv[1]);
   } else {
-    strncpy(p->me, MSG_host_get_name(MSG_host_self()), HOSTNAME_LENGTH);
+    p->me = xbt_strdup(MSG_host_get_name(MSG_host_self()));
   }
 }
 
@@ -127,6 +126,8 @@ void peer_delete(peer_t p)
   xbt_dynar_free(&p->pending_recvs);
   xbt_dynar_free(&p->pending_sends);
   xbt_free(p->me);
+  xbt_free(p->prev);
+  xbt_free(p->next);
 
   xbt_free(p);
 }
index fd20eca..8ee42f7 100644 (file)
@@ -12,8 +12,8 @@
 /* Peer struct */
 typedef struct s_peer {
   int init;
-  const char *prev;
-  const char *next;
+  char *prev;
+  char *next;
   char *me;
   int pieces;
   unsigned long long bytes;