[13/13] libsframe: refactor out sframe_fre_grow_tbl

Message ID 20251202073414.200246-14-indu.bhagat@oracle.com
State New
Headers
Series sframe: some refactoring and misc improvements |

Commit Message

Indu Bhagat Dec. 2, 2025, 7:34 a.m. UTC
  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.
---
 libsframe/sframe.c | 84 ++++++++++++++++++++++++++++++++--------------
 1 file changed, 58 insertions(+), 26 deletions(-)
  

Patch

diff --git a/libsframe/sframe.c b/libsframe/sframe.c
index ee94c9227f4..e0ff5db3816 100644
--- a/libsframe/sframe.c
+++ b/libsframe/sframe.c
@@ -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, within the encoder object ECTX. 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];