[sysprof: 42/63] libsysprof-capture: Use calloc() to replace GArray in writer-cat.c



commit 8748db40910ed8908fccca7b82b5e24e40cfe174
Author: Philip Withnall <withnall endlessm com>
Date:   Thu Jul 2 12:46:17 2020 +0100

    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

 .../sysprof-capture-writer-cat.c                   | 40 +++++++++++++++-------
 1 file changed, 27 insertions(+), 13 deletions(-)
---
diff --git a/src/libsysprof-capture/sysprof-capture-writer-cat.c 
b/src/libsysprof-capture/sysprof-capture-writer-cat.c
index 459e052..3411ba7 100644
--- a/src/libsysprof-capture/sysprof-capture-writer-cat.c
+++ b/src/libsysprof-capture/sysprof-capture-writer-cat.c
@@ -424,7 +424,10 @@ sysprof_capture_writer_cat (SysprofCaptureWriter  *self,
               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++)
                 {
@@ -436,15 +439,15 @@ sysprof_capture_writer_cat (SysprofCaptureWriter  *self,
                   if (c.id != src)
                     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,
                                                       frame->frame.time,
                                                       frame->frame.cpu,
                                                       frame->frame.pid,
-                                                      (gpointer)counter->data,
-                                                      counter->len);
+                                                      counters,
+                                                      n_counters);
 
               translate_table_sort (tables, TRANSLATE_CTR);
             }
@@ -460,8 +463,10 @@ sysprof_capture_writer_cat (SysprofCaptureWriter  *self,
               goto panic;
 
             {
-              g_autoptr(GArray) ids = g_array_new (FALSE, FALSE, sizeof (guint));
-              g_autoptr(GArray) values = g_array_new (FALSE, FALSE, sizeof (SysprofCaptureCounterValue));
+              unsigned int *ids = NULL;
+              SysprofCaptureCounterValue *values = NULL;
+              size_t n_elements = 0;
+              size_t n_elements_allocated = 0;
 
               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]);
                           SysprofCaptureCounterValue value = v->values[y];
 
-                          g_array_append_val (ids, dst);
-                          g_array_append_val (values, value);
+                          if (n_elements == n_elements_allocated)
+                            {
+                              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,
                                                    frame->frame.time,
                                                    frame->frame.cpu,
                                                    frame->frame.pid,
-                                                   (const unsigned int *)(void *)ids->data,
-                                                   (const SysprofCaptureCounterValue *)(void *)values->data,
-                                                   ids->len);
+                                                   ids,
+                                                   values,
+                                                   n_elements);
             }
 
             break;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]