|
From: <sv...@va...> - 2014-08-23 21:11:47
|
Author: florian
Date: Sat Aug 23 21:11:39 2014
New Revision: 14344
Log:
Replace CLG_(sprint_eventmapping) with CLG_(mappingcost_as_string)
which returns the string in a dynamically allocated but local buffer.
Fix call sites.
Addded CLG_REALLOC and function convenience function "grow_buffer".
Modified:
branches/BUF_REMOVAL/callgrind/debug.c
branches/BUF_REMOVAL/callgrind/dump.c
branches/BUF_REMOVAL/callgrind/events.c
branches/BUF_REMOVAL/callgrind/events.h
branches/BUF_REMOVAL/callgrind/global.h
branches/BUF_REMOVAL/callgrind/main.c
Modified: branches/BUF_REMOVAL/callgrind/debug.c
==============================================================================
--- branches/BUF_REMOVAL/callgrind/debug.c (original)
+++ branches/BUF_REMOVAL/callgrind/debug.c Sat Aug 23 21:11:39 2014
@@ -445,6 +445,12 @@
return VG_(malloc)(cc,s);
}
+void* CLG_(realloc)(const HChar* cc, void *p, UWord s, const HChar* f)
+{
+ CLG_DEBUG(3, "Realloc(%lu) in %s.\n", s, f);
+ return VG_(realloc)(cc,p,s);
+}
+
#else /* CLG_ENABLE_DEBUG */
void CLG_(print_bbno)(void) {}
Modified: branches/BUF_REMOVAL/callgrind/dump.c
==============================================================================
--- branches/BUF_REMOVAL/callgrind/dump.c (original)
+++ branches/BUF_REMOVAL/callgrind/dump.c Sat Aug 23 21:11:39 2014
@@ -574,8 +574,8 @@
static
void fprint_cost(int fd, EventMapping* es, ULong* cost)
{
- int p = CLG_(sprint_mappingcost)(outbuf, es, cost);
- VG_(sprintf)(outbuf+p, "\n");
+ const HChar *mcost = CLG_(mappingcost_as_string)(es, cost);
+ VG_(sprintf)(outbuf, "%s\n", mcost);
my_fwrite(fd, outbuf, VG_(strlen)(outbuf));
return;
}
@@ -1197,11 +1197,8 @@
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");
+ VG_(sprintf)(outbuf, "%s%s\n", prefix,
+ CLG_(mappingcost_as_string)(em, cost));
my_fwrite(fd, outbuf, VG_(strlen)(outbuf));
}
Modified: branches/BUF_REMOVAL/callgrind/events.c
==============================================================================
--- branches/BUF_REMOVAL/callgrind/events.c (original)
+++ branches/BUF_REMOVAL/callgrind/events.c Sat Aug 23 21:11:39 2014
@@ -536,14 +536,22 @@
return pos;
}
-/* Returns number of characters written */
-Int CLG_(sprint_mappingcost)(HChar* buf, EventMapping* em, ULong* c)
+/* Returns pointer to dynamically string. The string will be overwritten
+ with each invocation. */
+const HChar *CLG_(mappingcost_as_string)(EventMapping* em, ULong* c)
{
Int i, pos, skipped = 0;
+ static HChar *buf = NULL;
+ static SizeT bufsiz = 0;
- if (!c || em->size==0) return 0;
+ if (buf == NULL)
+ grow_buffer(&buf, &bufsiz, 100); // initial size
+ buf[0] = '\0';
+
+ if (!c || em->size==0) return buf;
/* At least one entry */
+ tl_assert(bufsiz > 21); // 20 chars for a 64-bit wide ULONG
pos = VG_(sprintf)(buf, "%llu", c[em->entry[0].offset]);
for(i=1; i<em->size; i++) {
@@ -552,13 +560,14 @@
continue;
}
while(skipped>0) {
+ grow_buffer(&buf, &bufsiz, pos + 2);
buf[pos++] = ' ';
buf[pos++] = '0';
skipped--;
}
- buf[pos++] = ' ';
- pos += VG_(sprintf)(buf+pos, "%llu", c[em->entry[i].offset]);
+ grow_buffer(&buf, &bufsiz, pos + 1 + 20 + 1);
+ pos += VG_(sprintf)(buf+pos, " %llu", c[em->entry[i].offset]);
}
- return pos;
+ return buf;
}
Modified: branches/BUF_REMOVAL/callgrind/events.h
==============================================================================
--- branches/BUF_REMOVAL/callgrind/events.h (original)
+++ branches/BUF_REMOVAL/callgrind/events.h Sat Aug 23 21:11:39 2014
@@ -129,7 +129,9 @@
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 mapping cost as a character string. That string is dnamically
+ allocated in a static buffer and will be overwritten with every allocation.
+ The function never returns NULL. */
+const HChar *CLG_(mappingcost_as_string)(EventMapping*, ULong*);
#endif /* CLG_EVENTS */
Modified: branches/BUF_REMOVAL/callgrind/global.h
==============================================================================
--- branches/BUF_REMOVAL/callgrind/global.h (original)
+++ branches/BUF_REMOVAL/callgrind/global.h Sat Aug 23 21:11:39 2014
@@ -895,13 +895,25 @@
void CLG_(print_addr_ln)(Addr addr);
void* CLG_(malloc)(const HChar* cc, UWord s, const HChar* f);
+void* CLG_(realloc)(const HChar* cc, void *p, UWord s, const HChar* f);
void* CLG_(free)(void* p, const HChar* f);
#if 0
#define CLG_MALLOC(_cc,x) CLG_(malloc)((_cc),x,__FUNCTION__)
+#define CLG_REALLOC(_cc,p,x) CLG_(realloc)((_cc),p,x,__FUNCTION__)
#define CLG_FREE(p) CLG_(free)(p,__FUNCTION__)
#else
#define CLG_MALLOC(_cc,x) VG_(malloc)((_cc),x)
+#define CLG_REALLOC(_cc,p,x) VG_(realloc)((_cc),p,x)
#define CLG_FREE(p) VG_(free)(p)
#endif
+static __inline__ void grow_buffer(HChar **buf, SizeT *size, SizeT need)
+{
+ if (need > *size) {
+ if (need < 256) need = 256;
+ *size = need;
+ *buf = CLG_REALLOC("cl.grow_buffer", *buf, *size);
+ }
+}
+
#endif /* CLG_GLOBAL */
Modified: branches/BUF_REMOVAL/callgrind/main.c
==============================================================================
--- branches/BUF_REMOVAL/callgrind/main.c (original)
+++ branches/BUF_REMOVAL/callgrind/main.c Sat Aug 23 21:11:39 2014
@@ -1478,9 +1478,8 @@
/* 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;
@@ -1490,8 +1489,8 @@
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);
+ VG_(gdb_printf)("events-%d: %s\n", t,
+ CLG_(mappingcost_as_string)(CLG_(dumpmap), sum));
VG_(gdb_printf)("frames-%d: %d\n", t, CLG_(current_call_stack).sp);
ce = 0;
@@ -1511,9 +1510,8 @@
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);
+ VG_(gdb_printf)("events-%d-%d: %s\n",t, i,
+ CLG_(mappingcost_as_string)(CLG_(dumpmap), sum));
}
if (ce && ce->jcc) {
to = ce->jcc->to;
@@ -1911,8 +1909,8 @@
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);
+ VG_(message)(Vg_UserMsg, "Collected : %s\n",
+ CLG_(mappingcost_as_string)(CLG_(dumpmap), CLG_(total_cost)));
VG_(message)(Vg_UserMsg, "\n");
/* determine value widths for statistics */
|