Change eina_counter_dump to return a string so it could work easily on windows.
authorcedric <cedric@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Tue, 9 Dec 2008 13:55:10 +0000 (13:55 +0000)
committercedric <cedric@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Tue, 9 Dec 2008 13:55:10 +0000 (13:55 +0000)
git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/eina@38055 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

src/include/eina_counter.h
src/lib/eina_benchmark.c
src/lib/eina_counter.c
src/tests/eina_test_counter.c

index 69e408d..e9dd030 100644 (file)
@@ -49,7 +49,7 @@ EAPI void eina_counter_delete(Eina_Counter *counter);
 
 EAPI void eina_counter_start(Eina_Counter *counter);
 EAPI void eina_counter_stop(Eina_Counter *counter, int specimen);
-EAPI void eina_counter_dump(Eina_Counter *counter, FILE *out);
+EAPI char *eina_counter_dump(Eina_Counter *counter);
 
 /**
  * @}
index f2530e4..ef03761 100644 (file)
@@ -335,6 +335,7 @@ eina_benchmark_run(Eina_Benchmark *bench)
    EINA_INLIST_FOREACH(bench->runs, run)
      {
        Eina_Counter *counter;
+       char *result;
        int i;
 
        snprintf(buffer, PATH_MAX, "bench_%s_%s.%s.data", bench->name, bench->run, run->name);
@@ -356,7 +357,12 @@ eina_benchmark_run(Eina_Benchmark *bench)
             eina_counter_stop(counter, i);
          }
 
-       eina_counter_dump(counter, current_data);
+       result = eina_counter_dump(counter);
+       if (result)
+         {
+            fprintf(current_data, "%s", result);
+            free(result);
+         }
 
        eina_counter_delete(counter);
 
index d895555..4563e5d 100644 (file)
@@ -95,6 +95,40 @@ _eina_counter_time_get(Eina_Nano_Time *tp)
 }
 #endif /* _WIN2 */
 
+static char *
+_eina_counter_asiprintf(char *base, int *position, const char *format, ...)
+{
+   char *tmp, *result;
+   int size = 32;
+   int n;
+   va_list ap;
+
+   tmp = realloc(base, sizeof (char) * (*position + size));
+   if (!tmp) return base;
+   result = tmp;
+
+   while (1)
+     {
+       va_start(ap, format);
+       n = vsnprintf(result + *position, size, format, ap);
+       va_end(ap);
+
+       if (n > -1 && n < size)
+         {
+            /* If we always have glibc > 2.2, we could just return *position += n. */
+            *position += strlen(result + *position);
+            return result;
+         }
+
+       if (n > -1) size = n + 1;
+       else size <<= 1;
+
+       tmp = realloc(result, sizeof (char) * (*position + size));
+       if (!tmp) return result;
+       result = tmp;
+     }
+}
+
 /**
  * @endcond
  */
@@ -379,8 +413,8 @@ eina_counter_stop(Eina_Counter *counter, int specimen)
 /**
  * @brief Dump the result of all clocks of a counter to a stream.
  *
+ * @return A string with a summary of the test.
  * @param counter The counter.
- * @param out The stream to dump the clocks.
  *
  * This function dump all the valid clocks of @p counter to the stream
  * @p out. If @p counter or @p out are @c NULL, the functions exits
@@ -393,14 +427,17 @@ eina_counter_stop(Eina_Counter *counter, int specimen)
  *
  * The unit of time is the nanosecond.
 */
-EAPI void
-eina_counter_dump(Eina_Counter *counter, FILE *out)
+EAPI char *
+eina_counter_dump(Eina_Counter *counter)
 {
    Eina_Clock *clk;
+   char *result = NULL;
+   int position = 0;
 
-   if (!counter || !out) return;
+   if (!counter) return NULL;
 
-   fprintf(out, "# specimen\texperiment time\tstarting time\tending time\n");
+   result = _eina_counter_asiprintf(result, &position, "# specimen\texperiment time\tstarting time\tending time\n");
+   if (!result) return NULL;
 
    EINA_INLIST_REVERSE_FOREACH(counter->clocks, clk)
      {
@@ -420,12 +457,15 @@ eina_counter_dump(Eina_Counter *counter, FILE *out)
         diff = (long int)(((long long int)(clk->end.QuadPart - clk->start.QuadPart) * 1000000000LL) / (long long int)_eina_counter_frequency.QuadPart);
 #endif /* _WIN2 */
 
-       fprintf(out, "%i\t%li\t%li\t%li\n",
-               clk->specimen,
-               diff,
-               start,
-               end);
+       result = _eina_counter_asiprintf(result, &position,
+                                        "%i\t%li\t%li\t%li\n",
+                                        clk->specimen,
+                                        diff,
+                                        start,
+                                        end);
      }
+
+   return result;
 }
 
 /**
index 1edeb39..5698235 100644 (file)
@@ -41,6 +41,7 @@ END_TEST
 START_TEST(eina_counter_simple)
 {
    Eina_Counter *cnt;
+   char *dump;
    int i;
 
    eina_counter_init();
@@ -62,7 +63,10 @@ START_TEST(eina_counter_simple)
 
    eina_counter_stop(cnt, i);
 
-   eina_counter_dump(cnt, stderr);
+   dump = eina_counter_dump(cnt);
+   fail_if(!dump);
+
+   free(dump);
 
    eina_counter_delete(cnt);
 
@@ -73,6 +77,7 @@ END_TEST
 START_TEST(eina_counter_break)
 {
    Eina_Counter *cnt;
+   char *dump;
 
    eina_counter_init();
 
@@ -83,7 +88,10 @@ START_TEST(eina_counter_break)
 
    eina_counter_delete(cnt);
 
-   eina_counter_dump(NULL, stderr);
+   dump = eina_counter_dump(NULL);
+   fail_if(dump);
+
+   free(dump);
 
    eina_counter_shutdown();
 }