textbtree: Avoid malloc in one place
authorMatthias Clasen <mclasen@redhat.com>
Sun, 4 Apr 2021 00:25:35 +0000 (20:25 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Sun, 4 Apr 2021 18:20:25 +0000 (14:20 -0400)
gtk/gtktextbtree.c

index 5f87ae70add835289c4fccfc163fb53c3e9cbc84..8167366109684244bc5f9a6b23efa9a1225dda71 100644 (file)
@@ -2469,16 +2469,12 @@ _gtk_text_btree_char_count (GtkTextBTree *tree)
   return tree->root_node->num_chars - 2;
 }
 
-#define LOTSA_TAGS 1000
 gboolean
 _gtk_text_btree_char_is_invisible (const GtkTextIter *iter)
 {
   gboolean invisible = FALSE;  /* if nobody says otherwise, it's visible */
-
-  int deftagCnts[LOTSA_TAGS] = { 0, };
-  int *tagCnts = deftagCnts;
-  GtkTextTag *deftags[LOTSA_TAGS];
-  GtkTextTag **tags = deftags;
+  int *tagCnts;
+  GtkTextTag **tags;
   int numTags;
   GtkTextBTreeNode *node;
   GtkTextLine *siblingline;
@@ -2489,7 +2485,6 @@ _gtk_text_btree_char_is_invisible (const GtkTextIter *iter)
   GtkTextBTree *tree;
   int byte_index;
 
-  line = _gtk_text_iter_get_text_line (iter);
   tree = _gtk_text_iter_get_btree (iter);
 
   /* Short-circuit if we've never seen a visibility tag within the
@@ -2498,16 +2493,14 @@ _gtk_text_btree_char_is_invisible (const GtkTextIter *iter)
   if G_LIKELY (!_gtk_text_tag_table_affects_visibility (tree->table))
     return FALSE;
 
+  line = _gtk_text_iter_get_text_line (iter);
+
   byte_index = gtk_text_iter_get_line_index (iter);
 
   numTags = gtk_text_tag_table_get_size (tree->table);
 
-  /* almost always avoid malloc, so stay out of system calls */
-  if (LOTSA_TAGS < numTags)
-    {
-      tagCnts = g_new0 (int, numTags);
-      tags = g_new (GtkTextTag*, numTags);
-    }
+  tagCnts = g_alloca (sizeof (int) * numTags);
+  tags = g_alloca (sizeof (GtkTextTag *) * numTags);
 
   /*
    * Record tag toggles within the line of indexPtr but preceding
@@ -2610,12 +2603,6 @@ _gtk_text_btree_char_is_invisible (const GtkTextIter *iter)
         }
     }
 
-  if (LOTSA_TAGS < numTags)
-    {
-      g_free (tagCnts);
-      g_free (tags);
-    }
-
   return invisible;
 }