Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
tesh: fix the stable sort.
authorArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Wed, 30 Nov 2011 22:29:03 +0000 (23:29 +0100)
committerArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Thu, 1 Dec 2011 07:40:17 +0000 (08:40 +0100)
Use a double indirection to be sure that the addresses of the
string pointers do not move.

examples/msg/masterslave/masterslave_arg.c
tools/tesh/run_context.c

index e1cd7f3..1fc804f 100644 (file)
@@ -23,7 +23,8 @@ int slave(int argc, char *argv[]);
 long number_of_jobs;
 long number_of_slaves;
 
-long my_random(long n) {
+static long my_random(long n)
+{
   return n * (rand() / ((double)RAND_MAX + 1));
 }
 
@@ -67,7 +68,6 @@ int slave(int argc, char *argv[])
 {
   m_task_t task = NULL;
   _XBT_GNUC_UNUSED int res;
-  int id = -1;
 
   XBT_DEBUG("mailbox: %s",MSG_process_get_name(MSG_process_self()));
   while (1) {
index 8a3fed4..eb93117 100644 (file)
@@ -647,20 +647,35 @@ void rctx_start(void)
 
 /* Helper function to sort the output */
 static int cmpstringp(const void *p1, const void *p2) {
-  /* Sort only using the 19 first chars (date+pid)
-   * If the dates are the same, then, sort using pointer address (be stable wrt output of each process)
+  /* Sort only using the sort_len first chars
+   * If they are the same, then, sort using pointer address
+   * (be stable wrt output of each process)
    */
-  const char *s1 = *((const char**) p1);
-  const char *s2 = *((const char**) p2);
+  const char **s1 = *(const char***)p1;
+  const char **s2 = *(const char***)p2;
 
-  XBT_DEBUG("Compare strings '%s' and '%s'", s1, s2);
+  XBT_DEBUG("Compare strings '%s' and '%s'", *s1, *s2);
 
-  int res = strncmp(s1, s2, sort_len);
+  int res = strncmp(*s1, *s2, sort_len);
   if (res == 0)
-    res = p1 > p2 ? 1 : (p1 < p2 ? -1 : 0);
+    res = s1 > s2 ? 1 : (s1 < s2 ? -1 : 0);
   return res;
 }
 
+static void stable_sort(xbt_dynar_t a)
+{
+  unsigned long len = xbt_dynar_length(a);
+  void **b = xbt_new(void*, len);
+  unsigned long i;
+  for (i = 0 ; i < len ; i++)   /* fill the array b with pointers to strings */
+    b[i] = xbt_dynar_get_ptr(a, i);
+  qsort(b, len, sizeof *b, cmpstringp); /* sort it */
+  for (i = 0 ; i < len ; i++) /* dereference the pointers to get the strings */
+    b[i] = *(char**)b[i];
+  for (i = 0 ; i < len ; i++)   /* put everything in place */
+    xbt_dynar_set_as(a, i, char*, b[i]);
+  xbt_free(b);
+}
 
 /* Waits for the child to end (or to timeout), and check its
    ending conditions. This is launched from rctx_start but either in main
@@ -724,7 +739,7 @@ void *rctx_wait(void *r)
     }
 
     if (rctx->output_sort) {
-      xbt_dynar_sort(b, cmpstringp);
+      stable_sort(b);
       /* If empty lines moved in first position, remove them */
       while (!xbt_dynar_is_empty(b) && *xbt_dynar_getfirst_as(b, char*) == '\0')
         xbt_dynar_shift(b, NULL);