]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Merge revisions 14344 and 14345 from the BUF_REMOVAL branch to trunk.
authorFlorian Krohm <florian@eich-krohm.de>
Thu, 6 Nov 2014 21:43:44 +0000 (21:43 +0000)
committerFlorian Krohm <florian@eich-krohm.de>
Thu, 6 Nov 2014 21:43:44 +0000 (21:43 +0000)
Basically:
CLG_(sprint_eventmapping)  --> CLG_(eventmapping_as_string)
CLG_(sprint_mappingcost)   --> CLG_(mappingcost_as_string)
The new functions return the string in a dynamically allocated buffer
that caller ought to free.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14699

callgrind/dump.c
callgrind/events.c
callgrind/events.h
callgrind/main.c

index fe525c39f9252768b06cadada3831530bc27a2cf..515d7ec69a73f32ebb66bb918be686d10ffdc74b 100644 (file)
@@ -597,9 +597,10 @@ void fprint_pos(Int fd, AddrPos* curr, AddrPos* last)
 static
 void fprint_cost(int fd, EventMapping* es, ULong* cost)
 {
-  int p = CLG_(sprint_mappingcost)(outbuf, es, cost);
-  VG_(sprintf)(outbuf+p, "\n");
+  HChar *mcost = CLG_(mappingcost_as_string)(es, cost);
+  VG_(sprintf)(outbuf, "%s\n", mcost);
   my_fwrite(fd, outbuf, VG_(strlen)(outbuf));
+  CLG_FREE(mcost);
   return;
 }
 
@@ -1220,12 +1221,10 @@ BBCC** prepare_dump(void)
 static void fprint_cost_ln(int fd, const HChar* prefix,
                           EventMapping* em, ULong* cost)
 {
-    int p;
-
-    p = VG_(sprintf)(outbuf, "%s", prefix);
-    p += CLG_(sprint_mappingcost)(outbuf + p, em, cost);
-    VG_(sprintf)(outbuf + p, "\n");
+    HChar *mcost = CLG_(mappingcost_as_string)(em, cost);
+    VG_(sprintf)(outbuf, "%s%s\n", prefix, mcost);
     my_fwrite(fd, outbuf, VG_(strlen)(outbuf));
+    CLG_FREE(mcost);
 }
 
 static ULong bbs_done = 0;
@@ -1402,10 +1401,10 @@ static int new_dumpfile(HChar buf[BUF_LEN], int tid, const HChar* trigger)
    my_fwrite(fd, buf, VG_(strlen)(buf));
 
    /* "events:" line */
-   i = VG_(sprintf)(buf, "events: ");
-   CLG_(sprint_eventmapping)(buf+i, CLG_(dumpmap));
+   HChar *evmap = CLG_(eventmapping_as_string)(CLG_(dumpmap));
+   VG_(sprintf)(buf, "events: %s\n", evmap);
+   VG_(free)(evmap);
    my_fwrite(fd, buf, VG_(strlen)(buf));
-   my_fwrite(fd, "\n", 1);
 
    /* summary lines */
    sum = CLG_(get_eventset_cost)( CLG_(sets).full );
index ab693c71859fc2ddf8c2e5762077ae2fcbcb0c2d..95babcab3fa23ff3956904e8cd608040db83ee19 100644 (file)
@@ -445,34 +445,47 @@ void CLG_(append_event)(EventMapping* em, const HChar* n)
 }
 
 
-/* Returns number of characters written */
-Int CLG_(sprint_eventmapping)(HChar* buf, EventMapping* em)
+/* Returns pointer to dynamically string. The string will be overwritten
+   with each invocation. */
+HChar *CLG_(eventmapping_as_string)(const EventMapping* em)
 {
-    Int i, pos = 0;
+    Int i;
     EventGroup* eg;
 
     CLG_ASSERT(em != 0);
 
+    XArray *xa = VG_(newXA)(VG_(malloc), "cl.events.emas", VG_(free),
+                            sizeof(HChar));
+
     for(i=0; i< em->size; i++) {
-       if (pos>0) buf[pos++] = ' ';
+       if (i > 0) {
+           VG_(xaprintf)(xa, "%c", ' ');
+        }
        eg = eventGroup[em->entry[i].group];
        CLG_ASSERT(eg != 0);
-       pos += VG_(sprintf)(buf + pos, "%s", eg->name[em->entry[i].index]);
+        VG_(xaprintf)(xa, "%s", eg->name[em->entry[i].index]);
     }
-    buf[pos] = 0;
+    VG_(xaprintf)(xa, "%c", '\0');   // zero terminate the string
+
+    HChar *buf = VG_(strdup)("cl.events.emas", VG_(indexXA)(xa, 0));
+    VG_(deleteXA)(xa);
 
-    return pos;
+    return buf;
 }
 
-/* Returns number of characters written */
-Int CLG_(sprint_mappingcost)(HChar* buf, EventMapping* em, ULong* c)
+/* Returns pointer to dynamically allocated string. Caller needs to
+   VG_(free) it. */
+HChar *CLG_(mappingcost_as_string)(const EventMapping* em, const ULong* c)
 {
-    Int i, pos, skipped = 0;
+    Int i, skipped = 0;
 
-    if (!c || em->size==0) return 0;
+    if (!c || em->size==0) return VG_(strdup)("cl.events.mcas", "");
+
+    XArray *xa = VG_(newXA)(VG_(malloc), "cl.events.mcas", VG_(free),
+                            sizeof(HChar));
 
     /* At least one entry */
-    pos = VG_(sprintf)(buf, "%llu", c[em->entry[0].offset]);
+    VG_(xaprintf)(xa, "%llu", c[em->entry[0].offset]);
 
     for(i=1; i<em->size; i++) {
        if (c[em->entry[i].offset] == 0) {
@@ -480,13 +493,15 @@ Int CLG_(sprint_mappingcost)(HChar* buf, EventMapping* em, ULong* c)
            continue;
        }
        while(skipped>0) {
-           buf[pos++] = ' ';
-           buf[pos++] = '0';
+            VG_(xaprintf)(xa, " 0");
            skipped--;
        }
-       buf[pos++] = ' ';
-       pos += VG_(sprintf)(buf+pos, "%llu", c[em->entry[i].offset]);
+       VG_(xaprintf)(xa, " %llu", c[em->entry[i].offset]);
     }
+    VG_(xaprintf)(xa, "%c", '\0');   // zero terminate the string
+
+    HChar *buf = VG_(strdup)("cl.events.mas", VG_(indexXA)(xa, 0));
+    VG_(deleteXA)(xa);
 
-    return pos;
+    return buf;
 }
index 322d27fdf25373fee08b127c63ad03111bed5b15..0293043c02b2fa52c279e0799ef845854171abdd 100644 (file)
@@ -121,9 +121,13 @@ struct _EventMapping {
 /* Allocate space for an event mapping */
 EventMapping* CLG_(get_eventmapping)(EventSet*);
 void CLG_(append_event)(EventMapping*, const HChar*);
-/* Returns number of characters written */
-Int CLG_(sprint_eventmapping)(HChar* buf, EventMapping*);
-/* Returns number of characters written */
-Int CLG_(sprint_mappingcost)(HChar* buf, EventMapping*, ULong*);
+/* Returns event mapping as a character string. That string is dynamically
+   allocated and it is the caller's responsibility to free it.
+   The function never returns NULL. */
+HChar *CLG_(eventmapping_as_string)(const EventMapping*);
+/* Returns mapping cost as a character string. That string is dynamically
+   allocated and it is the caller's responsibility to free it.
+   The function never returns NULL. */
+HChar *CLG_(mappingcost_as_string)(const EventMapping*, const ULong*);
 
 #endif /* CLG_EVENTS */
index 86a93385dddf97f6a979daf1751ea09001b2ba3c..0c54d88e9ea99581381c5ba27d5d34a857c30abf 100644 (file)
@@ -1478,11 +1478,11 @@ void CLG_(set_instrument_state)(const HChar* reason, Bool state)
 /* helper for dump_state_togdb */
 static void dump_state_of_thread_togdb(thread_info* ti)
 {
-    static HChar buf[512];
     static FullCost sum = 0, tmp = 0;
-    Int t, p, i;
+    Int t, i;
     BBCC *from, *to;
     call_entry* ce;
+    HChar *mcost;
 
     t = CLG_(current_tid);
     CLG_(init_cost_lz)( CLG_(sets).full, &sum );
@@ -1490,8 +1490,9 @@ static void dump_state_of_thread_togdb(thread_info* ti)
     CLG_(add_diff_cost)( CLG_(sets).full, sum, ti->lastdump_cost,
                         ti->states.entry[0]->cost);
     CLG_(copy_cost)( CLG_(sets).full, ti->lastdump_cost, tmp );
-    CLG_(sprint_mappingcost)(buf, CLG_(dumpmap), sum);
-    VG_(gdb_printf)("events-%d: %s\n", t, buf);
+    mcost = CLG_(mappingcost_as_string)(CLG_(dumpmap), sum);
+    VG_(gdb_printf)("events-%d: %s\n", t, mcost);
+    VG_(free)(mcost);
     VG_(gdb_printf)("frames-%d: %d\n", t, CLG_(current_call_stack).sp);
 
     ce = 0;
@@ -1511,9 +1512,9 @@ static void dump_state_of_thread_togdb(thread_info* ti)
                          ce->enter_cost, CLG_(current_state).cost );
       CLG_(copy_cost)( CLG_(sets).full, ce->enter_cost, tmp );
       
-      p = VG_(sprintf)(buf, "events-%d-%d: ",t, i);
-      CLG_(sprint_mappingcost)(buf + p, CLG_(dumpmap), sum );
-      VG_(gdb_printf)("%s\n", buf);
+      mcost = CLG_(mappingcost_as_string)(CLG_(dumpmap), sum);
+      VG_(gdb_printf)("events-%d-%d: %s\n",t, i, mcost);
+      VG_(free)(mcost);
     }
     if (ce && ce->jcc) {
       to = ce->jcc->to;
@@ -1541,9 +1542,9 @@ static void dump_state_togdb(void)
     VG_(gdb_printf)("distinct-contexts: %d\n", CLG_(stat).distinct_contexts);
 
     /* "events:" line. Given here because it will be dynamic in the future */
-    p = VG_(sprintf)(buf, "events: ");
-    CLG_(sprint_eventmapping)(buf+p, CLG_(dumpmap));
-    VG_(gdb_printf)("%s\n", buf);
+    HChar *evmap = CLG_(eventmapping_as_string)(CLG_(dumpmap));
+    VG_(gdb_printf)("events: %s\n", evmap);
+    VG_(free)(evmap);
     /* "part:" line (number of last part. Is 0 at start */
     VG_(gdb_printf)("part: %d\n", CLG_(get_dump_counter)());
                
@@ -1886,8 +1887,7 @@ void clg_print_stats(void)
 static
 void finish(void)
 {
-  HChar buf[32+COSTS_LEN];
-  HChar fmt[128];
+  HChar fmt[128];    // large enough
   Int l1, l2, l3;
   FullCost total;
 
@@ -1909,10 +1909,12 @@ void finish(void)
     VG_(message)(Vg_DebugMsg, "\n");
   }
 
-  CLG_(sprint_eventmapping)(buf, CLG_(dumpmap));
-  VG_(message)(Vg_UserMsg, "Events    : %s\n", buf);
-  CLG_(sprint_mappingcost)(buf, CLG_(dumpmap), CLG_(total_cost));
-  VG_(message)(Vg_UserMsg, "Collected : %s\n", buf);
+  HChar *evmap = CLG_(eventmapping_as_string)(CLG_(dumpmap));
+  VG_(message)(Vg_UserMsg, "Events    : %s\n", evmap);
+  VG_(free)(evmap);
+  HChar *mcost = CLG_(mappingcost_as_string)(CLG_(dumpmap), CLG_(total_cost));
+  VG_(message)(Vg_UserMsg, "Collected : %s\n", mcost);
+  VG_(free)(mcost);
   VG_(message)(Vg_UserMsg, "\n");
 
   /* determine value widths for statistics */