Use XCALLOC / XZALLOC macros where reasonable
authorJohn Darrington <john@darrington.wattle.id.au>
Sat, 2 Oct 2021 13:32:40 +0000 (15:32 +0200)
committerJohn Darrington <john@darrington.wattle.id.au>
Sat, 2 Oct 2021 13:32:40 +0000 (15:32 +0200)
19 files changed:
src/language/data-io/matrix-data.c
src/language/stats/chisquare.c
src/language/stats/crosstabs.c
src/language/stats/examine.c
src/language/stats/frequencies.c
src/language/stats/graph.c
src/language/stats/means.c
src/language/stats/median.c
src/language/stats/oneway.c
src/language/stats/regression.c
src/language/stats/reliability.c
src/libpspp/bit-vector.c
src/math/box-whisker.c
src/math/wilcoxon-sig.c
src/output/pivot-output.c
src/output/pivot-table.c
src/output/render.c
src/ui/gui/psppire-import-textfile.c
src/ui/gui/widget-io.c

index c0f5979bcd268e215b2930b7e30a60bbfceff332..bffdd92cb7f463d0183181196b9e920f7e1a2fe9 100644 (file)
@@ -940,7 +940,7 @@ cmd_matrix_data (struct lexer *lexer, struct dataset *ds)
     .cells = -1,
   };
 
-  bool *taken_vars = xzalloc (n_input_vars);
+  bool *taken_vars = XCALLOC (n_input_vars, bool);
   if (input_rowtype)
     taken_vars[var_get_dict_index (rowtype)] = true;
 
index eb21644d4af17b1ec79b88df805cbbe6ea82bdf2..d1d81bab39dcc26e422aa32e96354c5c16a808d5 100644 (file)
@@ -141,8 +141,8 @@ chisquare_execute (const struct dataset *ds,
   struct one_sample_test *ost = &cst->parent;
   double total_expected = 0.0;
 
-  double *df = xzalloc (sizeof (*df) * ost->n_vars);
-  double *xsq = xzalloc (sizeof (*df) * ost->n_vars);
+  double *df = XCALLOC (ost->n_vars, double);
+  double *xsq = XCALLOC (ost->n_vars, double);
   bool ok;
 
   for (i = 0 ; i < cst->n_expected ; ++i)
index 32e1a7736acee63ef52e3d09390f6cb151fe919a..465bdcc278fcab0d806164bad81ad91a23dd12bd 100644 (file)
@@ -606,7 +606,7 @@ parse_crosstabs_tables (struct lexer *lexer, struct dataset *ds,
        }
     }
 
-  int *by_iter = xcalloc (n_by, sizeof *by_iter);
+  int *by_iter = XCALLOC (n_by, int);
   proc->pivots = xnrealloc (proc->pivots,
                             proc->n_pivots + nx, sizeof *proc->pivots);
   for (int i = 0; i < nx; i++)
@@ -912,7 +912,7 @@ postcalc (struct crosstabs_proc *proc)
       if (proc->barchart)
         {
           int n_vars = (xt->n_vars > 2 ? 2 : xt->n_vars);
-          const struct variable **vars = xcalloc (n_vars, sizeof *vars);
+          const struct variable **vars = XCALLOC (n_vars, const struct variable*);
           for (size_t i = 0; i < n_vars; i++)
             vars[i] = xt->vars[i].var;
           chart_submit (barchart_create (vars, n_vars, _("Count"),
index 00fb801fce273283fc737fc91b2c23aa3531b352..1b668c2b778a5ec49e40e59d85819f097f0aa93d 100644 (file)
@@ -1197,13 +1197,12 @@ calculate_n (const void *aux1, void *aux2 UNUSED, void *user_data)
 
       {
        const int n_os = 5 + examine->n_percentiles;
-       struct order_stats **os ;
        es[v].percentiles = pool_calloc (examine->pool, examine->n_percentiles, sizeof (*es[v].percentiles));
 
        es[v].trimmed_mean = trimmed_mean_create (es[v].cc, 0.05);
        es[v].shapiro_wilk = NULL;
 
-       os = xcalloc (n_os, sizeof *os);
+       struct order_stats **os = XCALLOC (n_os, struct order_stats *);
        os[0] = &es[v].trimmed_mean->parent;
 
        es[v].quartiles[0] = percentile_create (0.25, es[v].cc);
@@ -1806,8 +1805,7 @@ cmd_examine (struct lexer *lexer, struct dataset *ds)
   if (percentiles_seen && examine.n_percentiles == 0)
     {
       examine.n_percentiles = 7;
-      examine.ptiles = xcalloc (examine.n_percentiles,
-                                sizeof (*examine.ptiles));
+      examine.ptiles = xcalloc (examine.n_percentiles, sizeof (*examine.ptiles));
 
       examine.ptiles[0] = 5;
       examine.ptiles[1] = 10;
index 484ce1af1e7095bf2f18e8eef0d6c0ebee16607b..e5462d083d2fd44d6e24e2e23960d360c6ad8f6f 100644 (file)
@@ -631,7 +631,7 @@ cmd_frequencies (struct lexer *lexer, struct dataset *ds)
                              PV_NO_DUPLICATE))
     goto error;
 
-  frq.vars = xzalloc (frq.n_vars * sizeof (*frq.vars));
+  frq.vars = xcalloc (frq.n_vars, sizeof (*frq.vars));
   for (i = 0; i < frq.n_vars; ++i)
     {
       frq.vars[i].var = vars[i];
index 425fd241574d76227d7cb2830dd11aa832307900..2dd98e3db27bbee33f315a0753897280e8797e52 100644 (file)
@@ -240,7 +240,7 @@ parse_function (struct lexer *lexer, struct graph *graph)
       if (!lex_force_match (lexer, T_LPAREN))
        goto error;
 
-      graph->dep_vars = xzalloc (sizeof (graph->dep_vars) * graph->n_dep_vars);
+      graph->dep_vars = xcalloc (graph->n_dep_vars, sizeof (graph->dep_vars));
       for (v = 0; v < ag_func[i].arity; ++v)
        {
          graph->dep_vars[v] = parse_variable (lexer, graph->dict);
index 0a3b413df721e7c8ef1f6114a358d1dcc523c885..8ebbca0f355f9babc1dcbb392755e9fa8ba01d65 100644 (file)
@@ -970,10 +970,8 @@ prepare_means (struct means *cmd)
         {
           struct workspace *ws = mt->ws + i;
          ws->root_cell = NULL;
-          ws->control_idx = xzalloc (mt->n_layers
-                                        * sizeof *ws->control_idx);
-          ws->instances = xzalloc (mt->n_layers
-                                        * sizeof *ws->instances);
+          ws->control_idx = xcalloc (mt->n_layers, sizeof *ws->control_idx);
+          ws->instances = xcalloc (mt->n_layers, sizeof *ws->instances);
           int cmb = i;
           for (int l = mt->n_layers - 1; l >= 0; --l)
             {
@@ -1149,9 +1147,9 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
        for (int t = 0; t < means.n_tables; ++t)
        {
          struct mtable *mt = means.table + t;
-         mt->summ = xzalloc (mt->n_combinations * mt->n_dep_vars
-                             sizeof (*mt->summ));
-         mt->ws = xzalloc (mt->n_combinations * sizeof (*mt->ws));
+         mt->summ = xcalloc (mt->n_combinations * mt->n_dep_vars,
+                             sizeof (*mt->summ));
+         mt->ws = xcalloc (mt->n_combinations, sizeof (*mt->ws));
        }
        run_means (&means, group, ds);
        for (int t = 0; t < means.n_tables; ++t)
index 8fa38c5a692134506e5c71ff67255ee2c487378d..2e09ab02cef0d21c621d8dbf6b6ca3eca624a47d 100644 (file)
@@ -253,7 +253,7 @@ median_execute (const struct dataset *ds,
          }
 
        results[v].n = count;
-       results[v].sorted_array = xcalloc (hmap_count (&map), sizeof (void*));
+       results[v].sorted_array = XCALLOC (hmap_count (&map), void*);
        results[v].var = var;
 
        HMAP_FOR_EACH (vn, struct val_node, node, &map)
index a61286255674fede14891abce111cd640c2970d5..e20d55198bc1568bdf2432d0a27b5677cc8dcfc9 100644 (file)
@@ -709,8 +709,8 @@ run_oneway (const struct oneway_spec *cmd,
   struct oneway_workspace ws;
 
   ws.actual_number_of_groups = 0;
-  ws.vws = xzalloc (cmd->n_vars * sizeof (*ws.vws));
-  ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
+  ws.vws = xcalloc (cmd->n_vars, sizeof (*ws.vws));
+  ws.dd_total = XCALLOC (cmd->n_vars, struct descriptive_data*);
 
   for (v = 0 ; v < cmd->n_vars; ++v)
     ws.dd_total[v] = dd_create (cmd->vars[v]);
index 7c98dba047b94ca3d278890e68885d08fa48fb71..e08f4a1d76f051b4686f3d4a1b19cd00cee725f7 100644 (file)
@@ -671,8 +671,7 @@ run_regression_get_models (const struct regression *cmd,
                           bool output)
 {
   size_t i;
-  struct linreg **models = NULL;
-  struct model_container *model_container = xzalloc (sizeof (*model_container) * cmd->n_vars);
+  struct model_container *model_container = XCALLOC (cmd->n_vars, struct model_container);
 
   struct ccase *c;
   struct covariance *cov;
@@ -731,7 +730,7 @@ run_regression_get_models (const struct regression *cmd,
     casereader_destroy (r);
   }
 
-  models = xcalloc (cmd->n_dep_vars, sizeof (*models));
+  struct linreg **models = XCALLOC (cmd->n_dep_vars, struct linreg*);
 
   for (int k = 0; k < cmd->n_dep_vars; k++)
     {
index 86c1add760eb2faad211df850d53fadd020c28ab..5816a2304ceba846425dab7bf1ab5af3d09ed785 100644 (file)
@@ -170,13 +170,13 @@ cmd_reliability (struct lexer *lexer, struct dataset *ds)
     /* Create a default Scale */
 
     reliability.n_sc = 1;
-    reliability.sc = xzalloc (sizeof (struct cronbach) * reliability.n_sc);
+    reliability.sc = xcalloc (reliability.n_sc, sizeof (struct cronbach));
 
     ds_assign_cstr (&reliability.scale_name, "ANY");
 
     c = &reliability.sc[0];
     c->n_items = reliability.n_variables;
-    c->items = xzalloc (sizeof (struct variable*) * c->n_items);
+    c->items = xcalloc (c->n_items, sizeof (struct variable*));
 
     for (i = 0 ; i < c->n_items ; ++i)
       c->items[i] = reliability.variables[i];
@@ -310,11 +310,8 @@ cmd_reliability (struct lexer *lexer, struct dataset *ds)
        (reliability.split_point == -1) ? s->n_items / 2 : reliability.split_point;
 
       reliability.sc[2].n_items = s->n_items - reliability.sc[1].n_items;
-      reliability.sc[1].items = xzalloc (sizeof (struct variable *)
-                                * reliability.sc[1].n_items);
-
-      reliability.sc[2].items = xzalloc (sizeof (struct variable *) *
-                                reliability.sc[2].n_items);
+      reliability.sc[1].items = XCALLOC (reliability.sc[1].n_items, const struct variable *);
+      reliability.sc[2].items = XCALLOC (reliability.sc[2].n_items, const struct variable *);
 
       for  (i = 0; i < reliability.sc[1].n_items ; ++i)
        reliability.sc[1].items[i] = s->items[i];
@@ -344,7 +341,7 @@ cmd_reliability (struct lexer *lexer, struct dataset *ds)
          struct cronbach *s = &reliability.sc[i + base_sc];
 
          s->n_items = reliability.sc[0].n_items - 1;
-         s->items = xzalloc (sizeof (struct variable *) * s->n_items);
+         s->items = xcalloc (s->n_items, sizeof (struct variable *));
          for (v_src = 0 ; v_src < reliability.sc[0].n_items ; ++v_src)
            {
              if (v_src != i)
@@ -391,7 +388,7 @@ run_reliability (struct dataset *ds, const struct reliability *reliability)
       struct cronbach *s = &reliability->sc[si];
       int i;
 
-      s->m = xzalloc (sizeof *s->m * s->n_items);
+      s->m = xcalloc (s->n_items, sizeof *s->m);
       s->total = moments1_create (MOMENT_VARIANCE);
 
       for (i = 0 ; i < s->n_items ; ++i)
index 79367248f0ae8653c9463709dc03c4702daa860e..5198c89c216bf94ba14b8e226a9768cd3c303f77 100644 (file)
@@ -25,8 +25,7 @@
 unsigned long int *
 bitvector_allocate(size_t n)
 {
-  return xcalloc (DIV_RND_UP (n, BITS_PER_ULONG),
-                  sizeof (unsigned long int));
+  return XCALLOC (DIV_RND_UP (n, BITS_PER_ULONG), unsigned long int);
 }
 
 size_t
index 4bbe0f58505d1c60ef407417f0eb53d5b31cdf90..506251682c7bf17106fba7525c2f6dc823ecac1c 100644 (file)
@@ -85,7 +85,7 @@ acc (struct statistic *s, const struct ccase *cx,
 
   /* y is an outlier */
 
-  struct outlier *o = xzalloc (sizeof *o) ;
+  struct outlier *o = XZALLOC (struct outlier);
   o->value = y;
   o->extreme = extreme;
   ds_init_empty (&o->label);
index 17484f1411d51adb170fd431c4b6193dce769f85..a9d9b70dbc9b94c5241365a2cf5580f5b78fd435 100644 (file)
@@ -93,7 +93,6 @@ count_sums_to_W (unsigned long int n, long int w)
      and using int will save some memory on 64-bit systems. */
   unsigned long int total;
   unsigned long int max;
-  int *array;
 
   assert (w >= 0);
   assert (n < CHAR_BIT * sizeof (unsigned long int));
@@ -106,7 +105,7 @@ count_sums_to_W (unsigned long int n, long int w)
   else if (n == 1)
     return 1;
 
-  array = xcalloc (w + 1, sizeof *array);
+  int *array = XCALLOC (w + 1, int);
   array[w] = 1;
 
   max = w;
index a170b6ee71a79e670c72c98fef1bda01c9e97b5a..c3751d04d888dff563b28a1155c18143c64f8b80 100644 (file)
@@ -233,7 +233,7 @@ compose_headings (struct table *t,
      |aaaa1|aaaa2|aaaa3|aaaa1|aaaa2|aaaa3|aaaa1|aaaa2|aaaa3|
      +-----+-----+-----+-----+-----+-----+-----+-----+-----+
   */
-  bool *vrules = xzalloc (n_columns + 1);
+  bool *vrules = XCALLOC (n_columns + 1, bool);
   vrules[0] = vrules[n_columns] = true;
   for (int dim_index = h_axis->n_dimensions; --dim_index >= 0; )
     {
@@ -426,7 +426,7 @@ collect_footnotes (const struct pivot_table *pt,
       return NULL;
     }
 
-  bool *refs = xzalloc (pt->n_footnotes);
+  bool *refs = XCALLOC (pt->n_footnotes, bool);
   size_t n_refs = 0;
   add_references (pt, title, refs, &n_refs);
   add_references (pt, layers, refs, &n_refs);
index 6bb446ed8ea5bb2895274a8907e58c88e5caf367..f1e0e73372177d1ed566c690523f7b0e1b7bf0dc 100644 (file)
@@ -968,7 +968,7 @@ clone_category (struct pivot_category *old,
     .extra_depth = old->extra_depth,
 
     .subs = (old->n_subs
-             ? xzalloc (old->n_subs * sizeof *new->subs)
+             ? xcalloc (old->n_subs, sizeof *new->subs)
              : NULL),
     .n_subs = old->n_subs,
     .allocated_subs = old->n_subs,
@@ -1006,9 +1006,9 @@ clone_dimension (struct pivot_dimension *old, struct pivot_table *new_pt)
     .axis_type = old->axis_type,
     .level = old->level,
     .top_index = old->top_index,
-    .data_leaves = xzalloc (old->n_leaves * sizeof *new->data_leaves),
-    .presentation_leaves = xzalloc (old->n_leaves
-                                    * sizeof *new->presentation_leaves),
+    .data_leaves = xcalloc (old->n_leaves , sizeof *new->data_leaves),
+    .presentation_leaves = xcalloc (old->n_leaves
+                                    , sizeof *new->presentation_leaves),
     .n_leaves = old->n_leaves,
     .allocated_leaves = old->n_leaves,
     .hide_all_labels = old->hide_all_labels,
index b88f10badb7a69779df99ce132dfc31c2a2ee5fa..7aa80a6977e69abd92f328429627c22a021e7260 100644 (file)
@@ -539,9 +539,8 @@ render_page_allocate__ (const struct render_params *params,
 
   for (int i = 0; i < TABLE_N_AXES; i++)
     {
-      page->cp[i] = xmalloc ((2 * n[i] + 2) * sizeof *page->cp[i]);
-      page->join_crossing[i] = xzalloc ((n[i] + 1)
-                                        * sizeof *page->join_crossing[i]);
+      page->cp[i] = xcalloc ((2 * n[i] + 2) , sizeof *page->cp[i]);
+      page->join_crossing[i] = xcalloc ((n[i] + 1) , sizeof *page->join_crossing[i]);
     }
 
   hmap_init (&page->overflows);
@@ -732,7 +731,7 @@ render_page_create (const struct render_params *params, struct table *table,
      span multiple columns. */
   struct render_row *columns[2];
   for (int i = 0; i < 2; i++)
-    columns[i] = xzalloc (nc * sizeof *columns[i]);
+    columns[i] = xcalloc (nc , sizeof *columns[i]);
   for (int y = 0; y < nr; y++)
     for (int x = 0; x < nc;)
       {
@@ -816,7 +815,7 @@ render_page_create (const struct render_params *params, struct table *table,
     }
 
   /* Calculate heights of cells that do not span multiple rows. */
-  struct render_row *rows = xzalloc (nr * sizeof *rows);
+  struct render_row *rows = XCALLOC (nr, struct render_row);
   for (int y = 0; y < nr; y++)
     for (int x = 0; x < nc;)
       {
index eedc21a082d4d6dee0e451c6acf0b6ad6ff328a3..d9dcccef2868d11b8d76e475b6ecf38655fe776d 100644 (file)
@@ -88,7 +88,7 @@ choose_likely_separators (PsppireImportAssistant *ia)
       gchar *line_text = NULL;
       gtk_tree_model_get (GTK_TREE_MODEL (ia->text_file), &iter, 1, &line_text, -1);
 
-      gint *counts = xzalloc (sizeof *counts * SEPARATOR_CNT);
+      gint *counts = XCALLOC (SEPARATOR_CNT, gint);
 
       struct substring cs = ss_cstr (line_text);
       for (;
index 9612e44e7f382b05fde510c1a832cc7a7518b1f5..589eac36ebcef4e797ee6fcb02353499bb37c251 100644 (file)
@@ -43,7 +43,6 @@ widget_printf (const gchar *fmt, ...)
   char_directives d;
   arguments a;
   GString *output;
-  GtkWidget **widgets;
   gchar *text;
   va_list ap;
   const char *s = fmt;
@@ -51,7 +50,7 @@ widget_printf (const gchar *fmt, ...)
   if (0 !=  printf_parse (fmt, &d, &a))
     return NULL;
 
-  widgets = xcalloc (d.count, sizeof (*widgets));
+  GtkWidget **widgets = XCALLOC (d.count, GtkWidget*);
   va_start (ap, fmt);
   for (i = 0 ; i < d.count ; ++i)
     {