[gtk/wip/baedert/css-values: 49/84] cssarrayvalue: Don't allocate memory when parsing array values



commit bf5d82fecb1c778f349c877537ac444e7ee87c35
Author: Timm Bäder <mail baedert org>
Date:   Wed Jan 8 08:58:07 2020 +0100

    cssarrayvalue: Don't allocate memory when parsing array values
    
    We probably won't find CSS with more than 128 values in array.

 gtk/gtkcssarrayvalue.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
---
diff --git a/gtk/gtkcssarrayvalue.c b/gtk/gtkcssarrayvalue.c
index 60019fea5c..d1e162fda9 100644
--- a/gtk/gtkcssarrayvalue.c
+++ b/gtk/gtkcssarrayvalue.c
@@ -411,25 +411,28 @@ _gtk_css_array_value_parse (GtkCssParser *parser,
                             GtkCssValue  *(* parse_func) (GtkCssParser *parser))
 {
   GtkCssValue *value, *result;
-  GPtrArray *values;
-
-  values = g_ptr_array_new ();
+  GtkCssValue *values[128];
+  guint n_values = 0;
+  guint i;
 
   do {
     value = parse_func (parser);
 
     if (value == NULL)
       {
-        g_ptr_array_set_free_func (values, (GDestroyNotify) _gtk_css_value_unref);
-        g_ptr_array_free (values, TRUE);
+        for (i = 0; i < n_values; i ++)
+          _gtk_css_value_unref (values[i]);
+
         return NULL;
       }
 
-    g_ptr_array_add (values, value);
+    values[n_values] = value;
+    n_values ++;
+    if (G_UNLIKELY (n_values > G_N_ELEMENTS (values)))
+      g_error ("Only %d elements in a css array are allowed", (int)G_N_ELEMENTS (values));
   } while (gtk_css_parser_try_token (parser, GTK_CSS_TOKEN_COMMA));
 
-  result = _gtk_css_array_value_new_from_array ((GtkCssValue **) values->pdata, values->len);
-  g_ptr_array_free (values, TRUE);
+  result = _gtk_css_array_value_new_from_array (values, n_values);
   return result;
 }
 


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