]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
libsframe: refactor out sframe_fre_grow_tbl
authorIndu Bhagat <indu.bhagat@oracle.com>
Thu, 20 Nov 2025 20:09:00 +0000 (12:09 -0800)
committerIndu Bhagat <indu.bhagat@oracle.com>
Tue, 9 Dec 2025 08:26:12 +0000 (00:26 -0800)
Usage of a global int number_of_entries is likely unnecessary.  The same
global is used for growing the FDE tbl too, when adding FDEs.  At the
moment, however, carve out a new function to grow the FRE table, and
use a macro instead of 'number_of_entries'.

This refactoring helps provide basis for a later patch where we add
SFrame FREs in bulk instead of one at a time to the SFrame encoder
object.

libsframe/
        * sframe.c (SFRAME_FRE_ALLOC_LEN): New definition.
        (sframe_grow_fre_tbl): New definition.
        (sframe_encoder_add_fre): Use the new function.
---
[Changes in V2]
  - Minor code comment adjustment.
[End of changes in v2]

libsframe/sframe.c

index fb8ece9743b4760c426435c0da837385c696fb4b..d8586ea95635e63936e555f0d6cf1b3db613d1e2 100644 (file)
@@ -57,6 +57,8 @@ struct sf_fre_tbl
 static void debug_printf (const char *, ...);
 
 static int _sframe_debug;      /* Control for printing out debug info.  */
+
+#define SFRAME_FRE_ALLOC_LEN  64
 static int number_of_entries = 64;
 
 static void
@@ -697,6 +699,58 @@ bad:
   return SFRAME_ERR;
 }
 
+/* Expands the memory allocated for the SFrame Frame Row Entry (FRE) table
+   FRE_TBL.  This function is called when the current FRE buffer is
+   insufficient and more stack trace data in the form of COUNT number of SFrame
+   FREs need to be added to the SFrame section.
+
+   Updates ERRP with 0 on success, or an SFrame error code on failure (e.g.,
+   memory allocation error).  */
+
+static sf_fre_tbl *
+sframe_grow_fre_tbl (sf_fre_tbl *fre_tbl, uint32_t count, int *errp)
+{
+  size_t fre_tbl_sz = 0;
+  uint32_t grow_count = SFRAME_FRE_ALLOC_LEN;
+  sf_fre_tbl *new_tbl = NULL;
+
+  if (fre_tbl == NULL)
+    {
+      fre_tbl_sz = (sizeof (sf_fre_tbl)
+                   + (grow_count * sizeof (sframe_frame_row_entry)));
+      new_tbl = malloc (fre_tbl_sz);
+      if (new_tbl == NULL)
+       {
+         sframe_set_errno (errp, SFRAME_ERR_NOMEM);
+         goto bad;
+       }
+
+      memset (new_tbl, 0, fre_tbl_sz);
+      new_tbl->alloced = grow_count;
+    }
+  else if (fre_tbl->count + count >= fre_tbl->alloced)
+    {
+      grow_count += count;
+      uint32_t new_len = fre_tbl->alloced + grow_count;
+      fre_tbl_sz = (sizeof (sf_fre_tbl)
+                   + (new_len * sizeof (sframe_frame_row_entry)));
+      void *tmp = realloc (fre_tbl, fre_tbl_sz);
+      if (tmp == NULL)
+       {
+         sframe_set_errno (errp, SFRAME_ERR_NOMEM);
+         goto bad;
+       }
+      new_tbl = tmp;
+
+      memset (&new_tbl->entry[new_tbl->alloced], 0,
+             grow_count * sizeof (sframe_frame_row_entry));
+      new_tbl->alloced += grow_count;
+    }
+
+bad:
+  return new_tbl;
+}
+
 /* The SFrame Decoder.  */
 
 /* Get SFrame header from the given decoder context DCTX.  */
@@ -1696,7 +1750,6 @@ sframe_encoder_add_fre (sframe_encoder_ctx *ectx,
   sframe_frame_row_entry *ectx_frep;
   size_t offsets_sz, esz;
   uint32_t fre_type;
-  size_t fre_tbl_sz;
   int err = 0;
 
   if (ectx == NULL || frep == NULL)
@@ -1713,36 +1766,15 @@ sframe_encoder_add_fre (sframe_encoder_ctx *ectx,
   fre_type = sframe_get_fre_type (fdep);
   sf_fre_tbl *fre_tbl = ectx->sfe_fres;
 
-  if (fre_tbl == NULL)
-    {
-      fre_tbl_sz = (sizeof (sf_fre_tbl)
-                   + (number_of_entries * sizeof (sframe_frame_row_entry)));
-      fre_tbl = malloc (fre_tbl_sz);
-
-      if (fre_tbl == NULL)
-       {
-         sframe_set_errno (&err, SFRAME_ERR_NOMEM);
-         goto bad;             /* OOM.  */
-       }
-      memset (fre_tbl, 0, fre_tbl_sz);
-      fre_tbl->alloced = number_of_entries;
-    }
-  else if (fre_tbl->count == fre_tbl->alloced)
+  if (fre_tbl == NULL || fre_tbl->count == fre_tbl->alloced)
     {
-      fre_tbl_sz = (sizeof (sf_fre_tbl)
-                   + ((fre_tbl->alloced + number_of_entries)
-                      * sizeof (sframe_frame_row_entry)));
-      sf_fre_tbl *tmp = realloc (fre_tbl, fre_tbl_sz);
-      if (tmp == NULL)
+      sf_fre_tbl *tmp = sframe_grow_fre_tbl (fre_tbl, 1, &err);
+      if (err)
        {
          sframe_set_errno (&err, SFRAME_ERR_NOMEM);
-         goto bad;             /* OOM.  */
+         goto bad;
        }
       fre_tbl = tmp;
-
-      memset (&fre_tbl->entry[fre_tbl->alloced], 0,
-             number_of_entries * sizeof (sframe_frame_row_entry));
-      fre_tbl->alloced += number_of_entries;
     }
 
   ectx_frep = &fre_tbl->entry[fre_tbl->count];