libsysprof-capture: Use calloc() to replace GArray in writer-cat.c

This is a straightforward replacement using a single allocation, as the
number of array elements is always known ahead of time.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

Helps: #40
This commit is contained in:
Philip Withnall
2020-07-02 12:46:17 +01:00
parent 8641789d87
commit 8748db4091

View File

@ -424,7 +424,10 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self,
goto panic; goto panic;
{ {
g_autoptr(GArray) counter = g_array_new (FALSE, FALSE, sizeof (SysprofCaptureCounter)); SysprofCaptureCounter *counters = calloc (frame->n_counters, sizeof (*counters));
size_t n_counters = 0;
if (counters == NULL)
goto panic;
for (unsigned int z = 0; z < frame->n_counters; z++) for (unsigned int z = 0; z < frame->n_counters; z++)
{ {
@ -436,15 +439,15 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self,
if (c.id != src) if (c.id != src)
translate_table_add (tables, TRANSLATE_CTR, src, c.id); translate_table_add (tables, TRANSLATE_CTR, src, c.id);
g_array_append_val (counter, c); counters[n_counters++] = c;
} }
sysprof_capture_writer_define_counters (self, sysprof_capture_writer_define_counters (self,
frame->frame.time, frame->frame.time,
frame->frame.cpu, frame->frame.cpu,
frame->frame.pid, frame->frame.pid,
(gpointer)counter->data, counters,
counter->len); n_counters);
translate_table_sort (tables, TRANSLATE_CTR); translate_table_sort (tables, TRANSLATE_CTR);
} }
@ -460,8 +463,10 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self,
goto panic; goto panic;
{ {
g_autoptr(GArray) ids = g_array_new (FALSE, FALSE, sizeof (guint)); unsigned int *ids = NULL;
g_autoptr(GArray) values = g_array_new (FALSE, FALSE, sizeof (SysprofCaptureCounterValue)); SysprofCaptureCounterValue *values = NULL;
size_t n_elements = 0;
size_t n_elements_allocated = 0;
for (unsigned int z = 0; z < frame->n_values; z++) for (unsigned int z = 0; z < frame->n_values; z++)
{ {
@ -474,21 +479,30 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self,
unsigned int dst = translate_table_translate (tables, TRANSLATE_CTR, v->ids[y]); unsigned int dst = translate_table_translate (tables, TRANSLATE_CTR, v->ids[y]);
SysprofCaptureCounterValue value = v->values[y]; SysprofCaptureCounterValue value = v->values[y];
g_array_append_val (ids, dst); if (n_elements == n_elements_allocated)
g_array_append_val (values, value); {
n_elements_allocated = (n_elements_allocated > 0) ? n_elements_allocated * 2 : 4;
ids = reallocarray (ids, n_elements_allocated, sizeof (*ids));
values = reallocarray (values, n_elements_allocated, sizeof (*values));
if (ids == NULL || values == NULL)
goto panic;
}
ids[n_elements] = dst;
values[n_elements] = value;
n_elements++;
assert (n_elements <= n_elements_allocated);
} }
} }
} }
assert (ids->len == values->len);
sysprof_capture_writer_set_counters (self, sysprof_capture_writer_set_counters (self,
frame->frame.time, frame->frame.time,
frame->frame.cpu, frame->frame.cpu,
frame->frame.pid, frame->frame.pid,
(const unsigned int *)(void *)ids->data, ids,
(const SysprofCaptureCounterValue *)(void *)values->data, values,
ids->len); n_elements);
} }
break; break;