[gnumeric] Memory: more heap allocation, less stack allocation.



commit 86b04cd886c2cc6a5d0eb3ddc7f9c3981cb14976
Author: Morten Welinder <terra gnome org>
Date:   Thu Nov 30 19:24:26 2017 -0500

    Memory: more heap allocation, less stack allocation.

 ChangeLog                    |    8 ++++++++
 plugins/applix/ChangeLog     |    4 ++++
 plugins/applix/applix-read.c |    5 ++---
 src/criteria.c               |   15 ++++++++++-----
 src/item-grid.c              |    8 ++++++--
 src/parse-util.c             |   12 ++++++------
 src/parser.y                 |    4 ++--
 src/preview-grid.c           |    9 +++++++--
 src/print-cell.c             |    5 ++++-
 9 files changed, 49 insertions(+), 21 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index d46bc64..7839557 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2017-11-30  Morten Welinder  <terra gnome org>
 
+       * src/print-cell.c (gnm_gtk_print_cell_range): Eliminate g_alloca
+       usage.
+       * src/parse-util.c (wbref_parse): Ditto.
+       * src/preview-grid.c (preview_grid_draw_region): Ditto.
+       * src/item-grid.c (item_grid_draw_region): Ditto.
+       * src/parser.y (yylex): Ditto.
+       * src/criteria.c (parse_database_criteria): Ditto.
+
        * src/sheet-filter.c (gnm_filter_combo_apply): Use heap, not
        stack, allocation.
 
diff --git a/plugins/applix/ChangeLog b/plugins/applix/ChangeLog
index 87a9df8..af4c214 100644
--- a/plugins/applix/ChangeLog
+++ b/plugins/applix/ChangeLog
@@ -1,3 +1,7 @@
+2017-11-30  Morten Welinder  <terra gnome org>
+
+       * applix-read.c (applix_sheetref_parse): Eliminate g_alloca usage.
+
 2017-11-18  Morten Welinder <terra gnome org>
 
        * Release 1.12.36
diff --git a/plugins/applix/applix-read.c b/plugins/applix/applix-read.c
index eaec5dd..606407d 100644
--- a/plugins/applix/applix-read.c
+++ b/plugins/applix/applix-read.c
@@ -241,10 +241,9 @@ applix_sheetref_parse (char const *start, Sheet **sheet, Workbook const *wb)
                return start;
        }
 
-       name = g_alloca (1 + end - begin);
-       strncpy (name, begin, end-begin);
-       name [end-begin] = '\0';
+       name = g_strndup (begin, end - begin);
        *sheet = workbook_sheet_by_name (wb, name);
+       g_free (name);
        return *sheet != NULL ? end : start;
 }
 
diff --git a/src/criteria.c b/src/criteria.c
index c5ab4d3..002ae2d 100644
--- a/src/criteria.c
+++ b/src/criteria.c
@@ -448,6 +448,7 @@ parse_database_criteria (GnmEvalPos const *ep, GnmValue const *database, GnmValu
         int   i;
        int   b_col, b_row, e_col, e_row;
        int   *field_ind;
+       GSList *res;
 
        g_return_val_if_fail (VALUE_IS_CELLRANGE (criteria), NULL);
 
@@ -464,7 +465,7 @@ parse_database_criteria (GnmEvalPos const *ep, GnmValue const *database, GnmValu
        }
 
        /* Find the index numbers for the columns of criterias */
-       field_ind = g_alloca (sizeof (int) * (e_col - b_col + 1));
+       field_ind = g_new (int, e_col - b_col + 1);
        for (i = b_col; i <= e_col; i++) {
                cell = sheet_cell_get (sheet, i, b_row);
                if (cell == NULL)
@@ -474,13 +475,17 @@ parse_database_criteria (GnmEvalPos const *ep, GnmValue const *database, GnmValu
                        continue;
                field_ind[i - b_col] =
                        find_column_of_field (ep, database, cell->value);
-               if (field_ind[i - b_col] == -1)
+               if (field_ind[i - b_col] == -1) {
+                       g_free (field_ind);
                        return NULL;
+               }
        }
 
-       return parse_criteria_range (sheet, b_col, b_row + 1,
-                                    e_col, e_row, field_ind,
-                                    FALSE);
+       res = parse_criteria_range (sheet, b_col, b_row + 1,
+                                   e_col, e_row, field_ind,
+                                   FALSE);
+       g_free (field_ind);
+       return res;
 }
 
 /**
diff --git a/src/item-grid.c b/src/item-grid.c
index 840ccb3..7e554a7 100644
--- a/src/item-grid.c
+++ b/src/item-grid.c
@@ -461,6 +461,7 @@ item_grid_draw_region (GocItem const *item, cairo_t *cr,
        GnmBorder const **borders, **prev_vert;
        GnmBorder const *none =
                sheet->hide_grid ? NULL : gnm_style_border_none ();
+       gpointer *sr_array_data;
 
        GnmRange     view;
        GSList   *merged_active, *merged_active_seen,
@@ -547,15 +548,16 @@ item_grid_draw_region (GocItem const *item, cairo_t *cr,
         * Note that this means that in some cases array [-1] is legal.
         */
        n = end_col - start_col + 3; /* 1 before, 1 after, 1 fencepost */
+       sr_array_data = g_new (gpointer, n * 8);
        style_row_init (&prev_vert, &sr, &next_sr, start_col, end_col,
-                       g_alloca (n * 8 * sizeof (gpointer)), sheet->hide_grid);
+                       sr_array_data, sheet->hide_grid);
 
        /* load up the styles for the first row */
        next_sr.row = sr.row = row = start_row;
        sheet_style_get_row (sheet, &sr);
 
        /* Collect the column widths */
-       colwidths = g_alloca (n * sizeof (int));
+       colwidths = g_new (int, n);
        colwidths -= start_col;
        for (col = start_col; col <= end_col; col++) {
                ColRowInfo const *ci = sheet_col_get_info (sheet, col);
@@ -853,6 +855,8 @@ plain_draw : /* a quick hack to deal with 142267 */
        g_slist_free (merged_used);        /* merges with bottom in view */
        g_slist_free (merged_active_seen); /* merges with bottom the view */
        g_slist_free (merged_unused);      /* merges in hidden rows */
+       g_free (sr_array_data);
+       g_free (colwidths + start_col); // Offset reverts -= from above
        g_return_val_if_fail (merged_active == NULL, TRUE);
        return TRUE;
 }
diff --git a/src/parse-util.c b/src/parse-util.c
index 45d0f4f..f27555b 100644
--- a/src/parse-util.c
+++ b/src/parse-util.c
@@ -923,15 +923,15 @@ wbref_parse (GnmConventions const *convs,
                if (*end != ']')
                        return start;
 
-               /* might be too big if quoted (remember leading [' */
-               name = g_alloca (1 + end - start - 2);
-               if (num_escapes < 0) {
-                       strncpy (name, start+1, end-start-1);
-                       name [end-start-1] = '\0';
-               } else
+               if (num_escapes < 0)
+                       name = g_strndup (start + 1, end - start - 1);
+               else {
+                       name = g_malloc (1 + end - start - 2);
                        unquote (name, start+2, end-start-2);
+               }
 
                tmp_wb = (*convs->input.external_wb) (convs, ref_wb, name);
+               g_free (name);
                if (tmp_wb == NULL)
                        return NULL;
 
diff --git a/src/parser.y b/src/parser.y
index 7ef6f4f..a7c95c2 100644
--- a/src/parser.y
+++ b/src/parser.y
@@ -1110,7 +1110,7 @@ yylex (void)
                                goto double_quote_loop;
                        }
 
-                       s = string = (char *) g_alloca (1 + state->ptr - p);
+                       s = string = g_malloc (1 + state->ptr - p);
                        while (p != state->ptr) {
                                if (*p == '&') {
                                        if (!strncmp (p, "&amp;", 5)) {
@@ -1145,7 +1145,7 @@ yylex (void)
                        *s = 0;
                        state->ptr += 6;
 
-                       v = value_new_string (string);
+                       v = value_new_string_nocopy (string);
                        yylval.expr = register_expr_allocation (gnm_expr_new_constant (v));
                        return QUOTED_STRING;
                }
diff --git a/src/preview-grid.c b/src/preview-grid.c
index f6ee713..6bddf73 100644
--- a/src/preview-grid.c
+++ b/src/preview-grid.c
@@ -222,6 +222,7 @@ preview_grid_draw_region (GocItem const *item, cairo_t *cr,
        GnmStyle const **styles;
        GnmBorder const **borders, **prev_vert;
        GnmBorder const *none = pg->gridlines ? gnm_style_border_none () : NULL;
+       gpointer *sr_array_data;
 
        int *colwidths = NULL;
 
@@ -233,15 +234,16 @@ preview_grid_draw_region (GocItem const *item, cairo_t *cr,
         *      - 2 arrays of n GnmStyle const *
         */
        n = end_col - start_col + 3; /* 1 before, 1 after, 1 fencepost */
+       sr_array_data = g_new (gpointer, n * 8);
        style_row_init (&prev_vert, &sr, &next_sr, start_col, end_col,
-                       g_alloca (n * 8 * sizeof (gpointer)), !pg->gridlines);
+                       sr_array_data, !pg->gridlines);
 
        /* load up the styles for the first row */
        next_sr.row = sr.row = row = start_row;
        pg_style_get_row (pg, &sr);
 
        /* Collect the column widths */
-       colwidths = g_alloca (n * sizeof (int));
+       colwidths = g_new (int, n);
        colwidths -= start_col;
        for (col = start_col; col <= end_col; col++)
                colwidths[col] = pg->defaults.col_width;
@@ -288,6 +290,9 @@ preview_grid_draw_region (GocItem const *item, cairo_t *cr,
 
                y += row_height;
        }
+
+       g_free (sr_array_data);
+       g_free (colwidths + start_col); // Offset reverts -= from above
        return TRUE;
 }
 
diff --git a/src/print-cell.c b/src/print-cell.c
index 4207af4..cbe7d37 100644
--- a/src/print-cell.c
+++ b/src/print-cell.c
@@ -302,6 +302,7 @@ gnm_gtk_print_cell_range (cairo_t *context,
        GnmStyle const **styles;
        GnmBorder const **borders, **prev_vert;
        GnmBorder const *none;
+       gpointer *sr_array_data;
 
        int n, col, row;
        double x, y, offset;
@@ -353,8 +354,9 @@ gnm_gtk_print_cell_range (cairo_t *context,
         * Note that this means that in some cases array [-1] is legal.
         */
        n = end_col - start_col + 3; /* 1 before, 1 after, 1 fencepost */
+       sr_array_data = g_new (gpointer, n * 8);
        style_row_init (&prev_vert, &sr, &next_sr, start_col, end_col,
-                       g_alloca (n * 8 * sizeof (gpointer)), hide_grid);
+                       sr_array_data, hide_grid);
 
        /* load up the styles for the first row */
        next_sr.row = sr.row = row = start_row;
@@ -610,6 +612,7 @@ gnm_gtk_print_cell_range (cairo_t *context,
        g_slist_free (merged_used);        /* merges with bottom in view */
        g_slist_free (merged_active_seen); /* merges with bottom the view */
        g_slist_free (merged_unused);      /* merges in hidden rows */
+       g_free (sr_array_data);
        g_return_if_fail (merged_active == NULL);
 }
 


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