bison-patches
[Top][All Lists]
Advanced

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

Bison memory-allocation patches to remove CALLOC, etc.


From: Paul Eggert
Subject: Bison memory-allocation patches to remove CALLOC, etc.
Date: Fri, 10 Dec 2004 00:00:35 -0800

The macros CALLOC, MALLOC, and REALLOC were obsolescent; they were
removed in gnulib but Bison kept them.  All in all I thought it better
to stick with the gnulib versions, so I removed their use from Bison
and replaced them with gnulib xcalloc, xnmalloc, and xnrealloc.

While doing this I noticed several instances of calloc that could be
replaced with malloc, since the code wasn't relying on the storage
being initialized.  I noticed a similar situation with some of the
static variables controlling the corresponding pointers: their initial
values were never used.  The patch below removes these unnecessary
initializations as well.  In a few cases, I added some explicit
initializations to zero when I thought it was clearer to do this than
to call calloc.

I fixed a related type glitch for new_closure's arg: callers were
passing it unsigned int, but it was declared as int.

I also noticed what appeared to be one real bug in table_grow:
when it grew conflict_table, it didn't initialize the new part.
I fixed this.

Here's what I installed:

2004-12-08  Paul Eggert  <address@hidden>

        * src/system.h (CALLOC, MALLOC, REALLOC): Remove.  All callers
        changed to use xcalloc, xnmalloc, xnrealloc, respectively,
        unless otherwise specified below.

        * src/LR0.c (allocate_itemsets): Use xnmalloc, not xcalloc,
        to allocate kernel_base, kernel_items, kernel_size, since
        they needn't be initialized to 0.
        (allocate_storgae): Likewise, for shiftset, redset, shift_symbol.
        * src/closure.c (new_closure): Likewise, for itemset.
        * src/derives.c (derives_compute): Likewise, for delts, derives, q.
        * src/lalr.c (set_goto_map): Likewise, for temp_map.
        (initialize_F): Likewise, for reads, edge, reads[i], includes[i].
        (build_relations): Likewise for edge, states1, includes.
        * src/nullable.c (nullable_compute): Likewise, for squeue, relts.
        * src/reader.c (packgram): Likewise, for ritem, rules.
        * src/reduce.c (nonterminals_reduce): Likewise for nontermmap.
        * src/relation.c (relation_digraph): Likewise for VERTICES.
        (relation_transpose): Likewise for new_R, end_R.
        * src/symtab.c (symbols_token_translations_init): Likewise for
        token_translations.
        * src/tables.c (save_row): Likewise for froms, tos, conflict_tos.
        (token_actions): Likewise for yydefact, actrow, conflrow,
        conflict_list.
        (save_column): Likewise for froms[symno], tos[symno].
        (goto_actions): Likewise for state_count.
        (pack_table): Likewise for base, pos, check.
        (tables_generate): Likewise for width.

        * src/LR0.c (set_states): Don't reuse kernel_size and kernel_base
        for initial core.  Just have a separate core, so we needn't worry
        about whether kernel_size and kernel_base are initialized.

        * src/LR0.c (shift_symbol, redset, shiftset, kernel_base,
        kernel_size, kernel_items): Remove unnecessary initialization.
        * src/conflicts.c (conflicts): Likewise.
        * src/derives.c (derives): Likewise.
        * src/muscle_tablc (muscle_insert): Likewise.
        * src/relation.c (relation_digraph): Likewise.
        * src/tables.c (froms, tos, conflict_tos, tally, width, actrow, order,
        conflrow, conflict_table, conflict_list, table, check):
        Likewise.

        * src/closure.c (new_closure): Arg is of type unsigned int, not int.
        This is because all callers pass unsigned int.
        * src/closure.h (new_closure): Likewise.

        * src/lalr.c (initialize_F): Initialize reads[i] in all cases.
        (build_relations): Initialize includes[i] in all cases.
        * src/reader.c (packgram): Always initialize rules[ruleno].prec
        and rules[ruleno].precsym.  Initialize members in order.
        * src/relation.c (relation_transpose): Always initialize new_R[i]
        and end_R[i].
        * src/table.c (conflict_row): Initialize 0 at end of conflict_list.

        * src/output.c (prepare_actions): Pass 0 instead of conflict_list[0];
        conflict_list[0] was always 0, but now it isn't initialized.

        * src/table.c (table_grow): When conflict_table grew, the grown
        area wasn't cleared.  Fix this.

Index: src/LR0.c
===================================================================
RCS file: /cvsroot/bison/bison/src/LR0.c,v
retrieving revision 1.89
diff -p -u -r1.89 LR0.c
--- src/LR0.c   22 Nov 2004 21:05:25 -0000      1.89
+++ src/LR0.c   10 Dec 2004 07:44:43 -0000
@@ -59,7 +59,7 @@ static state_list *last_state = NULL;
 static state *
 state_list_append (symbol_number sym, size_t core_size, item_number *core)
 {
-  state_list *node = MALLOC (node, 1);
+  state_list *node = xmalloc (sizeof *node);
   state *s = state_new (sym, core_size, core);
 
   if (trace_flag & trace_automaton)
@@ -84,14 +84,14 @@ state_list_append (symbol_number sym, si
 }
 
 static int nshifts;
-static symbol_number *shift_symbol = NULL;
+static symbol_number *shift_symbol;
 
-static rule **redset = NULL;
-static state **shiftset = NULL;
+static rule **redset;
+static state **shiftset;
 
-static item_number **kernel_base = NULL;
-static int *kernel_size = NULL;
-static item_number *kernel_items = NULL;
+static item_number **kernel_base;
+static int *kernel_size;
+static item_number *kernel_items;
 
 
 static void
@@ -106,8 +106,8 @@ allocate_itemsets (void)
      browsed too, hence we need to allocate room for _all_ the
      symbols.  */
   int count = 0;
-  short int *symbol_count = CALLOC (symbol_count,
-                                   nsyms + nuseless_nonterminals);
+  short int *symbol_count = xcalloc (nsyms + nuseless_nonterminals,
+                                    sizeof *symbol_count);
 
   for (r = 0; r < nrules; ++r)
     for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
@@ -122,9 +122,8 @@ allocate_itemsets (void)
      appears as an item, which is SYMBOL_COUNT[S].
      We allocate that much space for each symbol.  */
 
-  CALLOC (kernel_base, nsyms);
-  if (count)
-    CALLOC (kernel_items, count);
+  kernel_base = xnmalloc (nsyms, sizeof *kernel_base);
+  kernel_items = xnmalloc (count, sizeof *kernel_items);
 
   count = 0;
   for (i = 0; i < nsyms; i++)
@@ -134,7 +133,7 @@ allocate_itemsets (void)
     }
 
   free (symbol_count);
-  CALLOC (kernel_size, nsyms);
+  kernel_size = xnmalloc (nsyms, sizeof *kernel_size);
 }
 
 
@@ -143,10 +142,10 @@ allocate_storage (void)
 {
   allocate_itemsets ();
 
-  CALLOC (shiftset, nsyms);
-  CALLOC (redset, nrules);
+  shiftset = xnmalloc (nsyms, sizeof *shiftset);
+  redset = xnmalloc (nrules, sizeof *redset);
   state_hash_new ();
-  CALLOC (shift_symbol, nsyms);
+  shift_symbol = xnmalloc (nsyms, sizeof *shift_symbol);
 }
 
 
@@ -252,7 +251,7 @@ append_states (state *s)
     {
       symbol_number sym = shift_symbol[i];
       int j;
-      for (j = i; 0 < j && sym < shift_symbol [j - 1]; j--)
+      for (j = i; 0 < j && sym < shift_symbol[j - 1]; j--)
        shift_symbol[j] = shift_symbol[j - 1];
       shift_symbol[j] = sym;
     }
@@ -297,7 +296,7 @@ save_reductions (state *s)
 static void
 set_states (void)
 {
-  CALLOC (states, nstates);
+  states = xcalloc (nstates, sizeof *states);
 
   while (first_state)
     {
@@ -331,15 +330,14 @@ set_states (void)
 void
 generate_states (void)
 {
+  item_number initial_core = 0;
   state_list *list = NULL;
   allocate_storage ();
   new_closure (nritems);
 
   /* Create the initial state.  The 0 at the lhs is the index of the
      item of this initial rule.  */
-  kernel_base[0][0] = 0;
-  kernel_size[0] = 1;
-  state_list_append (0, kernel_size[0], kernel_base[0]);
+  state_list_append (0, 1, &initial_core);
 
   list = first_state;
 
Index: src/closure.c
===================================================================
RCS file: /cvsroot/bison/bison/src/closure.c,v
retrieving revision 1.68
diff -p -u -r1.68 closure.c
--- src/closure.c       22 Nov 2004 21:05:25 -0000      1.68
+++ src/closure.c       10 Dec 2004 07:44:43 -0000
@@ -180,9 +180,9 @@ set_fderives (void)
 
 
 void
-new_closure (int n)
+new_closure (unsigned int n)
 {
-  CALLOC (itemset, n);
+  itemset = xnmalloc (n, sizeof *itemset);
 
   ruleset = bitset_create (nrules, BITSET_FIXED);
 
Index: src/closure.h
===================================================================
RCS file: /cvsroot/bison/bison/src/closure.h,v
retrieving revision 1.11
diff -p -u -r1.11 closure.h
--- src/closure.h       11 Dec 2002 06:22:41 -0000      1.11
+++ src/closure.h       10 Dec 2004 07:44:43 -0000
@@ -29,7 +29,7 @@
    data so that closure can be called.  n is the number of elements to
    allocate for itemset.  */
 
-void new_closure (int n);
+void new_closure (unsigned int n);
 
 
 /* Given the kernel (aka core) of a state (a vector of item numbers
Index: src/conflicts.c
===================================================================
RCS file: /cvsroot/bison/bison/src/conflicts.c,v
retrieving revision 1.109
diff -p -u -r1.109 conflicts.c
--- src/conflicts.c     22 Nov 2004 21:05:25 -0000      1.109
+++ src/conflicts.c     10 Dec 2004 07:44:43 -0000
@@ -38,7 +38,7 @@
 /* -1 stands for not specified. */
 int expected_sr_conflicts = -1;
 int expected_rr_conflicts = -1;
-static char *conflicts = NULL;
+static char *conflicts;
 struct obstack solved_conflicts_obstack;
 
 static bitset shift_set;
@@ -305,9 +305,9 @@ conflicts_solve (void)
 {
   state_number i;
   /* List of look-ahead tokens on which we explicitly raise a syntax error.  */
-  symbol **errors = MALLOC (errors, ntokens + 1);
+  symbol **errors = xnmalloc (ntokens + 1, sizeof *errors);
 
-  CALLOC (conflicts, nstates);
+  conflicts = xcalloc (nstates, sizeof *conflicts);
   shift_set = bitset_create (ntokens, BITSET_FIXED);
   look_ahead_set = bitset_create (ntokens, BITSET_FIXED);
   obstack_init (&solved_conflicts_obstack);
Index: src/derives.c
===================================================================
RCS file: /cvsroot/bison/bison/src/derives.c,v
retrieving revision 1.42
diff -p -u -r1.42 derives.c
--- src/derives.c       22 Nov 2004 21:05:25 -0000      1.42
+++ src/derives.c       10 Dec 2004 07:44:43 -0000
@@ -37,7 +37,7 @@ typedef struct rule_list
   rule *value;
 } rule_list;
 
-rule ***derives = NULL;
+rule ***derives;
 
 static void
 print_derives (void)
@@ -70,12 +70,12 @@ derives_compute (void)
 
   /* DSET[NTERM - NTOKENS] -- A linked list of the numbers of the rules
      whose LHS is NTERM.  */
-  rule_list **dset = CALLOC (dset, nvars);
+  rule_list **dset = xcalloc (nvars, sizeof *dset);
 
   /* DELTS[RULE] -- There are NRULES rule number to attach to nterms.
      Instead of performing NRULES allocations for each, have an array
      indexed by rule numbers.  */
-  rule_list *delts = CALLOC (delts, nrules);
+  rule_list *delts = xnmalloc (nrules, sizeof *delts);
 
   for (r = nrules - 1; r >= 0; --r)
     {
@@ -90,8 +90,8 @@ derives_compute (void)
   /* DSET contains what we need under the form of a linked list.  Make
      it a single array.  */
 
-  CALLOC (derives, nvars);
-  CALLOC (q, nvars + nrules);
+  derives = xnmalloc (nvars, sizeof *derives);
+  q = xnmalloc (nvars + nrules, sizeof *q);
 
   for (i = ntokens; i < nsyms; i++)
     {
Index: src/lalr.c
===================================================================
RCS file: /cvsroot/bison/bison/src/lalr.c,v
retrieving revision 1.101
diff -p -u -r1.101 lalr.c
--- src/lalr.c  8 Dec 2004 05:54:20 -0000       1.101
+++ src/lalr.c  10 Dec 2004 07:44:43 -0000
@@ -80,8 +80,8 @@ set_goto_map (void)
   state_number s;
   goto_number *temp_map;
 
-  CALLOC (goto_map, nvars + 1);
-  CALLOC (temp_map, nvars + 1);
+  goto_map = xcalloc (nvars + 1, sizeof *goto_map);
+  temp_map = xnmalloc (nvars + 1, sizeof *temp_map);
 
   ngotos = 0;
   for (s = 0; s < nstates; ++s)
@@ -116,8 +116,8 @@ set_goto_map (void)
     temp_map[nsyms - ntokens] = ngotos;
   }
 
-  CALLOC (from_state, ngotos);
-  CALLOC (to_state, ngotos);
+  from_state = xcalloc (ngotos, sizeof *from_state);
+  to_state = xcalloc (ngotos, sizeof *to_state);
 
   for (s = 0; s < nstates; ++s)
     {
@@ -170,8 +170,8 @@ map_goto (state_number s0, symbol_number
 static void
 initialize_F (void)
 {
-  goto_number **reads = CALLOC (reads, ngotos);
-  goto_number *edge = CALLOC (edge, ngotos + 1);
+  goto_number **reads = xnmalloc (ngotos, sizeof *reads);
+  goto_number *edge = xnmalloc (ngotos + 1, sizeof *edge);
   goto_number nedges = 0;
 
   goto_number i;
@@ -194,10 +194,12 @@ initialize_F (void)
            edge[nedges++] = map_goto (stateno, sym);
        }
 
-      if (nedges)
+      if (nedges == 0)
+       reads[i] = NULL;
+      else
        {
-         CALLOC (reads[i], nedges + 1);
-         memcpy (reads[i], edge, nedges * sizeof (edge[0]));
+         reads[i] = xnmalloc (nedges + 1, sizeof reads[i][0]);
+         memcpy (reads[i], edge, nedges * sizeof edge[0]);
          reads[i][nedges] = END_NODE;
          nedges = 0;
        }
@@ -217,7 +219,7 @@ static void
 add_lookback_edge (state *s, rule *r, goto_number gotono)
 {
   int ri = state_reduction_find (s, r);
-  goto_list *sp = MALLOC (sp, 1);
+  goto_list *sp = xmalloc (sizeof *sp);
   sp->next = lookback[(s->reductions->look_ahead_tokens - LA) + ri];
   sp->value = gotono;
   lookback[(s->reductions->look_ahead_tokens - LA) + ri] = sp;
@@ -228,11 +230,11 @@ add_lookback_edge (state *s, rule *r, go
 static void
 build_relations (void)
 {
-  goto_number *edge = CALLOC (edge, ngotos + 1);
-  state_number *states1 = CALLOC (states1, ritem_longest_rhs () + 1);
+  goto_number *edge = xnmalloc (ngotos + 1, sizeof *edge);
+  state_number *states1 = xnmalloc (ritem_longest_rhs () + 1, sizeof *states1);
   goto_number i;
 
-  CALLOC (includes, ngotos);
+  includes = xnmalloc (ngotos, sizeof *includes);
 
   for (i = 0; i < ngotos; i++)
     {
@@ -276,10 +278,12 @@ build_relations (void)
            }
        }
 
-      if (nedges)
+      if (nedges == 0)
+       includes[i] = NULL;
+      else
        {
          int j;
-         CALLOC (includes[i], nedges + 1);
+         includes[i] = xnmalloc (nedges + 1, sizeof includes[i][0]);
          for (j = 0; j < nedges; j++)
            includes[i][j] = edge[j];
          includes[i][nedges] = END_NODE;
@@ -381,7 +385,7 @@ initialize_LA (void)
     nLA = 1;
 
   pLA = LA = bitsetv_create (nLA, ntokens, BITSET_FIXED);
-  CALLOC (lookback, nLA);
+  lookback = xcalloc (nLA, sizeof *lookback);
 
   /* Initialize the members LOOK_AHEAD_TOKENS for each state whose reductions
      require look-ahead tokens.  */
Index: src/muscle_tab.c
===================================================================
RCS file: /cvsroot/bison/bison/src/muscle_tab.c,v
retrieving revision 1.32
diff -p -u -r1.32 muscle_tab.c
--- src/muscle_tab.c    7 May 2004 07:35:10 -0000       1.32
+++ src/muscle_tab.c    10 Dec 2004 07:44:43 -0000
@@ -99,7 +99,7 @@ void
 muscle_insert (const char *key, char *value)
 {
   muscle_entry probe;
-  muscle_entry *entry = NULL;
+  muscle_entry *entry;
 
   probe.key = key;
   entry = hash_lookup (muscle_table, &probe);
@@ -107,7 +107,7 @@ muscle_insert (const char *key, char *va
   if (!entry)
     {
       /* First insertion in the hash. */
-      MALLOC (entry, 1);
+      entry = xmalloc (sizeof *entry);
       entry->key = key;
       hash_insert (muscle_table, entry);
     }
@@ -133,7 +133,7 @@ muscle_grow (const char *key, const char
   if (!entry)
     {
       /* First insertion in the hash. */
-      MALLOC (entry, 1);
+      entry = xmalloc (sizeof *entry);
       entry->key = key;
       hash_insert (muscle_table, entry);
       entry->value = xstrdup (val);
Index: src/nullable.c
===================================================================
RCS file: /cvsroot/bison/bison/src/nullable.c,v
retrieving revision 1.43
diff -p -u -r1.43 nullable.c
--- src/nullable.c      22 Nov 2004 21:05:25 -0000      1.43
+++ src/nullable.c      10 Dec 2004 07:44:43 -0000
@@ -61,16 +61,16 @@ nullable_compute (void)
   symbol_number *s2;
   rule_list *p;
 
-  symbol_number *squeue = CALLOC (squeue, nvars);
-  short int *rcount = CALLOC (rcount, nrules);
+  symbol_number *squeue = xnmalloc (nvars, sizeof *squeue);
+  short int *rcount = xcalloc (nrules, sizeof *rcount);
   /* RITEM contains all the rules, including useless productions.
      Hence we must allocate room for useless nonterminals too.  */
-  rule_list **rsets = CALLOC (rsets, nvars);
+  rule_list **rsets = xcalloc (nvars, sizeof *rsets);
   /* This is said to be more elements than we actually use.
      Supposedly NRITEMS - NRULES is enough.  But why take the risk?  */
-  rule_list *relts = CALLOC (relts, nritems + nvars + 1);
+  rule_list *relts = xnmalloc (nritems + nvars + 1, sizeof *relts);
 
-  CALLOC (nullable, nvars);
+  nullable = xcalloc (nvars, sizeof *nullable);
 
   s1 = s2 = squeue;
   p = relts;
Index: src/output.c
===================================================================
RCS file: /cvsroot/bison/bison/src/output.c,v
retrieving revision 1.227
diff -p -u -r1.227 output.c
--- src/output.c        22 Jul 2004 14:39:58 -0000      1.227
+++ src/output.c        10 Dec 2004 07:44:43 -0000
@@ -192,7 +192,7 @@ prepare_symbols (void)
   /* Output YYTOKNUM. */
   {
     int i;
-    int *values = MALLOC (values, ntokens);
+    int *values = xnmalloc (ntokens, sizeof *values);
     for (i = 0; i < ntokens; ++i)
       values[i] = symbols[i]->user_token_number;
     muscle_insert_int_table ("toknum", values,
@@ -212,13 +212,13 @@ prepare_rules (void)
 {
   rule_number r;
   unsigned int i = 0;
-  item_number *rhs = MALLOC (rhs, nritems);
-  unsigned int *prhs = MALLOC (prhs, nrules);
-  unsigned int *rline = MALLOC (rline, nrules);
-  symbol_number *r1 = MALLOC (r1, nrules);
-  unsigned int *r2 = MALLOC (r2, nrules);
-  short int *dprec = MALLOC (dprec, nrules);
-  short int *merger = MALLOC (merger, nrules);
+  item_number *rhs = xnmalloc (nritems, sizeof *rhs);
+  unsigned int *prhs = xnmalloc (nrules, sizeof *prhs);
+  unsigned int *rline = xnmalloc (nrules, sizeof *rline);
+  symbol_number *r1 = xnmalloc (nrules, sizeof *r1);
+  unsigned int *r2 = xnmalloc (nrules, sizeof *r2);
+  short int *dprec = xnmalloc (nrules, sizeof *dprec);
+  short int *merger = xnmalloc (nrules, sizeof *merger);
 
   for (r = 0; r < nrules; ++r)
     {
@@ -272,7 +272,7 @@ static void
 prepare_states (void)
 {
   state_number i;
-  symbol_number *values = MALLOC (values, nstates);
+  symbol_number *values = xnmalloc (nstates, sizeof *values);
   for (i = 0; i < nstates; ++i)
     values[i] = states[i]->accessing_symbol;
   muscle_insert_symbol_number_table ("stos", values,
@@ -498,7 +498,7 @@ prepare_actions (void)
   muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table,
                                    conflict_table[0], 1, high + 1);
   muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list,
-                                   conflict_list[0], 1, conflict_list_cnt);
+                                   0, 1, conflict_list_cnt);
 }
 
 
Index: src/reader.c
===================================================================
RCS file: /cvsroot/bison/bison/src/reader.c,v
retrieving revision 1.234
diff -p -u -r1.234 reader.c
--- src/reader.c        30 Sep 2003 20:11:29 -0000      1.234
+++ src/reader.c        10 Dec 2004 07:44:43 -0000
@@ -108,7 +108,7 @@ get_merge_function (uniqstr name, uniqst
       break;
   if (syms->next == NULL)
     {
-      MALLOC (syms->next, 1);
+      syms->next = xmalloc (sizeof syms->next[0]);
       syms->next->name = uniqstr_new (name);
       syms->next->type = uniqstr_new (type);
       syms->next->next = NULL;
@@ -387,8 +387,8 @@ packgram (void)
   rule_number ruleno = 0;
   symbol_list *p = grammar;
 
-  CALLOC (ritem, nritems);
-  CALLOC (rules, nrules);
+  ritem = xnmalloc (nritems, sizeof *ritem);
+  rules = xnmalloc (nrules, sizeof *rules);
 
   while (p)
     {
@@ -397,12 +397,14 @@ packgram (void)
       rules[ruleno].number = ruleno;
       rules[ruleno].lhs = p->sym;
       rules[ruleno].rhs = ritem + itemno;
+      rules[ruleno].prec = NULL;
+      rules[ruleno].dprec = p->dprec;
+      rules[ruleno].merger = p->merger;
+      rules[ruleno].precsym = NULL;
       rules[ruleno].location = p->location;
       rules[ruleno].useful = true;
       rules[ruleno].action = p->action;
       rules[ruleno].action_location = p->action_location;
-      rules[ruleno].dprec = p->dprec;
-      rules[ruleno].merger = p->merger;
 
       p = p->next;
       while (p && p->sym)
Index: src/reduce.c
===================================================================
RCS file: /cvsroot/bison/bison/src/reduce.c,v
retrieving revision 1.82
diff -p -u -r1.82 reduce.c
--- src/reduce.c        3 Feb 2003 15:35:57 -0000       1.82
+++ src/reduce.c        10 Dec 2004 07:44:43 -0000
@@ -247,7 +247,7 @@ reduce_grammar_tables (void)
   {
     int useful = 0;
     int useless = nrules - nuseless_productions;
-    rule *rules_sorted = MALLOC (rules_sorted, nrules);
+    rule *rules_sorted = xnmalloc (nrules, sizeof *rules_sorted);
     rule_number r;
     for (r = 0; r < nrules; ++r)
       rules_sorted[rules[r].useful ? useful++ : useless++] = rules[r];
@@ -291,7 +291,7 @@ nonterminals_reduce (void)
   /* Map the nonterminals to their new index: useful first, useless
      afterwards.  Kept for later report.  */
 
-  symbol_number *nontermmap = CALLOC (nontermmap, nvars);
+  symbol_number *nontermmap = xnmalloc (nvars, sizeof *nontermmap);
   n = ntokens;
   for (i = ntokens; i < nsyms; i++)
     if (bitset_test (V, i))
@@ -307,7 +307,7 @@ nonterminals_reduce (void)
 
   /* Shuffle elements of tables indexed by symbol number.  */
   {
-    symbol **symbols_sorted = MALLOC (symbols_sorted, nvars);
+    symbol **symbols_sorted = xnmalloc (nvars, sizeof *symbols_sorted);
 
     for (i = ntokens; i < nsyms; i++)
       symbols[i]->number = nontermmap[i - ntokens];
Index: src/relation.c
===================================================================
RCS file: /cvsroot/bison/bison/src/relation.c,v
retrieving revision 1.9
diff -p -u -r1.9 relation.c
--- src/relation.c      22 Nov 2004 21:05:25 -0000      1.9
+++ src/relation.c      10 Dec 2004 07:44:43 -0000
@@ -98,17 +98,14 @@ relation_digraph (relation r, size_t siz
   unsigned int i;
 
   infinity = size + 2;
-  CALLOC (INDEX, size + 1);
-  CALLOC (VERTICES, size + 1);
+  INDEX = xcalloc (size + 1, sizeof *INDEX);
+  VERTICES = xnmalloc (size + 1, sizeof *VERTICES);
   top = 0;
 
   R = r;
   F = *function;
 
   for (i = 0; i < size; i++)
-    INDEX[i] = 0;
-
-  for (i = 0; i < size; i++)
     if (INDEX[i] == 0 && R[i])
       traverse (i);
 
@@ -127,11 +124,11 @@ void
 relation_transpose (relation *R_arg, int n)
 {
   /* The result. */
-  relation new_R = CALLOC (new_R, n);
+  relation new_R = xnmalloc (n, sizeof *new_R);
   /* END_R[I] -- next entry of NEW_R[I]. */
-  relation end_R = CALLOC (end_R, n);
+  relation end_R = xnmalloc (n, sizeof *end_R);
   /* NEDGES[I] -- total size of NEW_R[I]. */
-  int *nedges = CALLOC (nedges, n);
+  int *nedges = xcalloc (n, sizeof *nedges);
   int i, j;
 
   if (trace_flag & trace_sets)
@@ -148,13 +145,16 @@ relation_transpose (relation *R_arg, int
 
   /* Allocate. */
   for (i = 0; i < n; i++)
-    if (nedges[i] > 0)
-      {
-       relation_node *sp = CALLOC (sp, nedges[i] + 1);
-       sp[nedges[i]] = END_NODE;
-       new_R[i] = sp;
-       end_R[i] = sp;
-      }
+    {
+      relation_node *sp = NULL;
+      if (nedges[i] > 0)
+       {
+         sp = xnmalloc (nedges[i] + 1, sizeof *sp);
+         sp[nedges[i]] = END_NODE;
+       }
+      new_R[i] = sp;
+      end_R[i] = sp;
+    }
 
   /* Store. */
   for (i = 0; i < n; i++)
Index: src/symlist.c
===================================================================
RCS file: /cvsroot/bison/bison/src/symlist.c,v
retrieving revision 1.7
diff -p -u -r1.7 symlist.c
--- src/symlist.c       13 Dec 2002 08:39:53 -0000      1.7
+++ src/symlist.c       10 Dec 2004 07:44:43 -0000
@@ -32,7 +32,7 @@
 symbol_list *
 symbol_list_new (symbol *sym, location loc)
 {
-  symbol_list *res = MALLOC (res, 1);
+  symbol_list *res = xmalloc (sizeof *res);
   res->next = NULL;
   res->sym = sym;
   res->location = loc;
Index: src/symtab.c
===================================================================
RCS file: /cvsroot/bison/bison/src/symtab.c,v
retrieving revision 1.57
diff -p -u -r1.57 symtab.c
--- src/symtab.c        11 Oct 2004 09:03:55 -0000      1.57
+++ src/symtab.c        10 Dec 2004 07:44:43 -0000
@@ -48,7 +48,7 @@ location startsymbol_location;
 static symbol *
 symbol_new (uniqstr tag, location loc)
 {
-  symbol *res = MALLOC (res, 1);
+  symbol *res = xmalloc (sizeof *res);
 
   uniqstr_assert (tag);
   res->tag = tag;
@@ -587,7 +587,8 @@ symbols_token_translations_init (void)
        max_user_token_number = this->user_token_number;
     }
 
-  CALLOC (token_translations, max_user_token_number + 1);
+  token_translations = xnmalloc (max_user_token_number + 1,
+                                sizeof *token_translations);
 
   /* Initialize all entries for literal tokens to 2, the internal
      token number for $undefined, which represents all invalid inputs.
@@ -606,7 +607,7 @@ symbols_token_translations_init (void)
 void
 symbols_pack (void)
 {
-  CALLOC (symbols, nsyms);
+  symbols = xcalloc (nsyms, sizeof *symbols);
 
   symbols_do (symbol_check_alias_consistency_processor, NULL);
   symbols_do (symbol_pack_processor, NULL);
Index: src/system.h
===================================================================
RCS file: /cvsroot/bison/bison/src/system.h,v
retrieving revision 1.64
diff -p -u -r1.64 system.h
--- src/system.h        22 Nov 2004 21:05:25 -0000      1.64
+++ src/system.h        10 Dec 2004 07:44:43 -0000
@@ -54,9 +54,6 @@ typedef size_t uintptr_t;
 #endif
 
 #include <xalloc.h>
-#define CALLOC(P, N) ((P) = xcalloc (N, sizeof *(P)))
-#define MALLOC(P, N) ((P) = xnmalloc (N, sizeof *(P)))
-#define REALLOC(P, N) ((P) = xnrealloc (P, N, sizeof *(P)))
 
 
 /*---------------------.
Index: src/tables.c
===================================================================
RCS file: /cvsroot/bison/bison/src/tables.c,v
retrieving revision 1.23
diff -p -u -r1.23 tables.c
--- src/tables.c        8 Dec 2004 05:54:51 -0000       1.23
+++ src/tables.c        10 Dec 2004 07:44:43 -0000
@@ -81,11 +81,11 @@ int nvectors;
 #define BASE_MAXIMUM INT_MAX
 #define BASE_MINIMUM INT_MIN
 
-static base_number **froms = NULL;
-static base_number **tos = NULL;
-static unsigned int **conflict_tos = NULL;
-static short int *tally = NULL;
-static base_number *width = NULL;
+static base_number **froms;
+static base_number **tos;
+static unsigned int **conflict_tos;
+static short int *tally;
+static base_number *width;
 
 
 /* For a given state, N = ACTROW[SYMBOL]:
@@ -97,12 +97,12 @@ static base_number *width = NULL;
 typedef short int action_number;
 #define ACTION_NUMBER_MINIMUM SHRT_MIN
 
-static action_number *actrow = NULL;
+static action_number *actrow;
 
 /* FROMS and TOS are reordered to be compressed.  ORDER[VECTOR] is the
    new vector number of VECTOR.  We skip `empty' vectors (i.e.,
    TALLY[VECTOR] = 0), and call these `entries'.  */
-static vector_number *order = NULL;
+static vector_number *order;
 static int nentries;
 
 base_number *base = NULL;
@@ -112,9 +112,9 @@ base_number *base = NULL;
 base_number base_ninf = 0;
 static base_number *pos = NULL;
 
-static unsigned int *conflrow = NULL;
-unsigned int *conflict_table = NULL;
-unsigned int *conflict_list = NULL;
+static unsigned int *conflrow;
+unsigned int *conflict_table;
+unsigned int *conflict_list;
 int conflict_list_cnt;
 static int conflict_list_free;
 
@@ -122,8 +122,8 @@ static int conflict_list_free;
    with more or less the original hard-coded value (which was
    SHRT_MAX).  */
 static int table_size = 32768;
-base_number *table = NULL;
-base_number *check = NULL;
+base_number *table;
+base_number *check;
 /* The value used in TABLE to denote explicit syntax errors
    (%nonassoc), a negative infinite.  First defaults to ACTION_NUMBER_MININUM,
    but in order to keep small tables, renumbered as TABLE_ERROR, which
@@ -153,13 +153,15 @@ table_grow (int desired)
     fprintf (stderr, "growing table and check from: %d to %d\n",
             old_size, table_size);
 
-  REALLOC (table, table_size);
-  REALLOC (check, table_size);
-  REALLOC (conflict_table, table_size);
+  table = xnrealloc (table, table_size, sizeof *table);
+  conflict_table = xnrealloc (conflict_table, table_size,
+                             sizeof *conflict_table);
+  check = xnrealloc (check, table_size, sizeof *check);
 
   for (/* Nothing. */; old_size < table_size; ++old_size)
     {
       table[old_size] = 0;
+      conflict_table[old_size] = 0;
       check[old_size] = -1;
     }
 }
@@ -208,6 +210,7 @@ conflict_row (state *s)
        /* Leave a 0 at the end.  */
        if (conflict_list_free <= 0)
          abort ();
+       conflict_list[conflict_list_cnt] = 0;
        conflict_list_cnt += 1;
        conflict_list_free -= 1;
       }
@@ -373,7 +376,7 @@ save_row (state_number s)
   base_number *sp;
   base_number *sp1;
   base_number *sp2;
-  unsigned int *sp3 IF_LINT (= NULL);
+  unsigned int *sp3;
 
   /* Number of non default actions in S.  */
   count = 0;
@@ -385,9 +388,10 @@ save_row (state_number s)
     return;
 
   /* Allocate non defaulted actions.  */
-  froms[s] = sp = CALLOC (sp1, count);
-  tos[s] = CALLOC (sp2, count);
-  conflict_tos[s] = nondeterministic_parser ? CALLOC (sp3, count) : NULL;
+  froms[s] = sp = sp1 = xnmalloc (count, sizeof *sp1);
+  tos[s] = sp2 = xnmalloc (count, sizeof *sp2);
+  conflict_tos[s] = sp3 =
+    nondeterministic_parser ? xnmalloc (count, sizeof *sp3) : NULL;
 
   /* Store non defaulted actions.  */
   for (i = 0; i < ntokens; i++)
@@ -421,12 +425,12 @@ token_actions (void)
 
   int nconflict = nondeterministic_parser ? conflicts_total_count () : 0;
 
-  CALLOC (yydefact, nstates);
+  yydefact = xnmalloc (nstates, sizeof *yydefact);
 
-  CALLOC (actrow, ntokens);
-  CALLOC (conflrow, ntokens);
+  actrow = xnmalloc (ntokens, sizeof *actrow);
+  conflrow = xnmalloc (ntokens, sizeof *conflrow);
 
-  CALLOC (conflict_list, 1 + 2 * nconflict);
+  conflict_list = xnmalloc (1 + 2 * nconflict, sizeof *conflict_list);
   conflict_list_free = 2 * nconflict;
   conflict_list_cnt = 1;
 
@@ -491,8 +495,8 @@ save_column (symbol_number sym, state_nu
     return;
 
   /* Allocate room for non defaulted gotos.  */
-  froms[symno] = sp = CALLOC (sp1, count);
-  tos[symno] = CALLOC (sp2, count);
+  froms[symno] = sp = sp1 = xnmalloc (count, sizeof *sp1);
+  tos[symno] = sp2 = xnmalloc (count, sizeof *sp2);
 
   /* Store the state numbers of the non defaulted gotos.  */
   for (i = begin; i < end; i++)
@@ -554,8 +558,8 @@ static void
 goto_actions (void)
 {
   symbol_number i;
-  short int *state_count = CALLOC (state_count, nstates);
-  MALLOC (yydefgoto, nvars);
+  short int *state_count = xnmalloc (nstates, sizeof *state_count);
+  yydefgoto = xnmalloc (nvars, sizeof *yydefgoto);
 
   /* For a given nterm I, STATE_COUNT[S] is the number of times there
      is a GOTO to S on I.  */
@@ -751,11 +755,11 @@ pack_table (void)
 {
   int i;
 
-  CALLOC (base, nvectors);
-  CALLOC (pos, nentries);
-  CALLOC (table, table_size);
-  CALLOC (conflict_table, table_size);
-  CALLOC (check, table_size);
+  base = xnmalloc (nvectors, sizeof *base);
+  pos = xnmalloc (nentries, sizeof *pos);
+  table = xcalloc (table_size, sizeof *table);
+  conflict_table = xcalloc (table_size, sizeof *conflict_table);
+  check = xnmalloc (table_size, sizeof *check);
 
   lowzero = 0;
   high = 0;
@@ -810,11 +814,11 @@ tables_generate (void)
 
   nvectors = state_number_as_int (nstates) + nvars;
 
-  CALLOC (froms, nvectors);
-  CALLOC (tos, nvectors);
-  CALLOC (conflict_tos, nvectors);
-  CALLOC (tally, nvectors);
-  CALLOC (width, nvectors);
+  froms = xcalloc (nvectors, sizeof *froms);
+  tos = xcalloc (nvectors, sizeof *tos);
+  conflict_tos = xcalloc (nvectors, sizeof *conflict_tos);
+  tally = xcalloc (nvectors, sizeof *tally);
+  width = xnmalloc (nvectors, sizeof *width);
 
   token_actions ();
 
@@ -823,7 +827,7 @@ tables_generate (void)
   free (from_state);
   free (to_state);
 
-  CALLOC (order, nvectors);
+  order = xcalloc (nvectors, sizeof *order);
   sort_actions ();
   pack_table ();
   free (order);
Index: src/vcg.c
===================================================================
RCS file: /cvsroot/bison/bison/src/vcg.c,v
retrieving revision 1.14
diff -p -u -r1.14 vcg.c
--- src/vcg.c   1 Oct 2003 07:46:41 -0000       1.14
+++ src/vcg.c   10 Dec 2004 07:44:43 -0000
@@ -418,9 +418,7 @@ add_edge (graph *g, edge *e)
 void
 add_classname (graph *g, int val, const char *name)
 {
-  struct classname *classname;
-
-  MALLOC (classname, 1);
+  struct classname *classname = xmalloc (sizeof *classname);
   classname->no = val;
   classname->name = name;
   classname->next = g->classname;
@@ -430,9 +428,7 @@ add_classname (graph *g, int val, const 
 void
 add_infoname (graph *g, int integer, const char *str)
 {
-  struct infoname *infoname;
-
-  MALLOC (infoname, 1);
+  struct infoname *infoname = xmalloc (sizeof *infoname);
   infoname->integer = integer;
   infoname->chars = str;
   infoname->next = g->infoname;
@@ -444,9 +440,7 @@ void
 add_colorentry (graph *g, int color_idx, int red_cp,
                int green_cp, int blue_cp)
 {
-  struct colorentry *ce;
-
-  MALLOC (ce, 1);
+  struct colorentry *ce = xmalloc (sizeof *ce);
   ce->color_index = color_idx;
   ce->red_cp = red_cp;
   ce->green_cp = green_cp;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]