diff mbox

[fortran] Use xcalloc instead of gfc_getmem

Message ID BANLkTiksS8oc_66GuDsW9r0Em9qhx=ggVw@mail.gmail.com
State New
Headers show

Commit Message

Janne Blomqvist April 19, 2011, 5:46 p.m. UTC
On Tue, Apr 19, 2011 at 10:46, Jakub Jelinek <jakub@redhat.com> wrote:
> On Tue, Apr 19, 2011 at 10:41:33AM +0300, Janne Blomqvist wrote:
>> On Mon, Apr 18, 2011 at 23:50, Steve Kargl
>> <sgk@troutmask.apl.washington.edu> wrote:
>> > On Mon, Apr 18, 2011 at 11:41:33PM +0300, Janne Blomqvist wrote:
>> >> Hi,
>> >>
>> >> the attached patch replaces gfc_getmem with calls to xcalloc (from
>> >> libiberty). Apart from reducing duplicated code, calloc is better than
>> >> malloc + memset, as the allocator knows that the kernel always gives
>> >> out zeroed pages so in some cases it can avoid memset'in the area.
>> >>
>> >> Regtested on x86_64-unknown-linux-gnu, Ok for trunk?
>> >>
>> >
>> > OK.
>>
>> Thanks; I'll commit it later today when I get home.
>
> Please don't.  You should be using macros from libiberty.h like rest
> of gcc instead.  So instead of xcalloc use XCNEW, XCNEWVEC or XCNEWVAR
> and remove the casts.

Oh, those macros are nice. I updated the patch to use them instead,
except for one case where the usage didn't fit the XCNEW(VEC) API and
I used xcalloc directly instead.  Here's what I committed:

2011-04-19  Janne Blomqvist  <jb@gcc.gnu.org>

	* misc.c (gfc_getmem): Remove function.
	* gfortran.h: Remove gfc_getmem prototype. Replace gfc_getmem
	usage with XCNEW or XCNEWVEC.
	* expr.c (gfc_check_assign_symbol): Replace gfc_getmem usage with
	XCNEW or XCNEWVEC.
	* options.c (gfc_handle_module_path_options)
	(gfc_get_option_string): Likewise.
	* resolve.c (gfc_resolve_forall): Likewise.
	* simplify.c (simplify_transformation_to_array): Likewise.
	* target-memory.c (gfc_target_interpret_expr): Likewise.
	* trans-common.c (get_segment_info, copy_equiv_list_to_ns)
	(get_init_field): Likewise.
	* trans-expr.c (gfc_conv_statement_function): Likewise.
	* trans-io.c (nml_full_name): Likewise.
	* trans-stmt.c (gfc_trans_forall_1): Likewise.
	* scanner.c (load_file): Replace gfc_getmem usage with xcalloc.

Comments

Jakub Jelinek April 19, 2011, 5:53 p.m. UTC | #1
On Tue, Apr 19, 2011 at 08:46:17PM +0300, Janne Blomqvist wrote:
> Oh, those macros are nice. I updated the patch to use them instead,
> except for one case where the usage didn't fit the XCNEW(VEC) API and
> I used xcalloc directly instead.  Here's what I committed:

For that remaining case there is XCNEWVAR, i.e.
    b = XCNEWVAR (gfc_linebuf, gfc_linebuf_header_size 
			       + (len + 1) * sizeof (gfc_char_t));

	Jakub
Janne Blomqvist April 19, 2011, 6:19 p.m. UTC | #2
On Tue, Apr 19, 2011 at 20:53, Jakub Jelinek <jakub@redhat.com> wrote:
> On Tue, Apr 19, 2011 at 08:46:17PM +0300, Janne Blomqvist wrote:
>> Oh, those macros are nice. I updated the patch to use them instead,
>> except for one case where the usage didn't fit the XCNEW(VEC) API and
>> I used xcalloc directly instead.  Here's what I committed:
>
> For that remaining case there is XCNEWVAR, i.e.
>    b = XCNEWVAR (gfc_linebuf, gfc_linebuf_header_size
>                               + (len + 1) * sizeof (gfc_char_t));

Thanks. Committed the above as obvious (r172730).
diff mbox

Patch

diff --git a/gcc/fortran/expr.c b/gcc/fortran/expr.c
index 1e31653..42b65c6 100644
--- a/gcc/fortran/expr.c
+++ b/gcc/fortran/expr.c
@@ -3583,7 +3583,7 @@  gfc_check_assign_symbol (gfc_symbol *sym, gfc_expr *rvalue)
   lvalue.ts = sym->ts;
   if (sym->as)
     lvalue.rank = sym->as->rank;
-  lvalue.symtree = (gfc_symtree *) gfc_getmem (sizeof (gfc_symtree));
+  lvalue.symtree = XCNEW (gfc_symtree);
   lvalue.symtree->n.sym = sym;
   lvalue.where = sym->declared_at;
 
diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
index ce11c07..1d725e4 100644
--- a/gcc/fortran/gfortran.h
+++ b/gcc/fortran/gfortran.h
@@ -1272,8 +1272,7 @@  typedef struct gfc_entry_list
 }
 gfc_entry_list;
 
-#define gfc_get_entry_list() \
-  (gfc_entry_list *) gfc_getmem(sizeof(gfc_entry_list))
+#define gfc_get_entry_list() XCNEW (gfc_entry_list)
 
 /* Lists of rename info for the USE statement.  */
 
@@ -1302,8 +1301,7 @@  typedef struct gfc_use_list
 }
 gfc_use_list;
 
-#define gfc_get_use_list() \
-  (gfc_use_list *) gfc_getmem(sizeof(gfc_use_list))
+#define gfc_get_use_list() XCNEW (gfc_use_list)
 
 /* Within a namespace, symbols are pointed to by symtree nodes that
    are linked together in a balanced binary tree.  There can be
@@ -1783,7 +1781,7 @@  typedef struct gfc_expr
 gfc_expr;
 
 
-#define gfc_get_shape(rank) ((mpz_t *) gfc_getmem((rank)*sizeof(mpz_t)))
+#define gfc_get_shape(rank) (XCNEWVEC (mpz_t, (rank)))
 
 /* Structures for information associated with different kinds of
    numbers.  The first set of integer parameters define all there is
@@ -2369,7 +2367,6 @@  void gfc_start_source_files (void);
 void gfc_end_source_files (void);
 
 /* misc.c */
-void *gfc_getmem (size_t) ATTRIBUTE_MALLOC;
 int gfc_terminal_width (void);
 void gfc_clear_ts (gfc_typespec *);
 FILE *gfc_open_file (const char *);
diff --git a/gcc/fortran/misc.c b/gcc/fortran/misc.c
index a54ffc0..1274047 100644
--- a/gcc/fortran/misc.c
+++ b/gcc/fortran/misc.c
@@ -23,24 +23,6 @@  along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "gfortran.h"
 
-/* Get a block of memory.  Many callers assume that the memory we
-   return is zeroed.  */
-
-void *
-gfc_getmem (size_t n)
-{
-  void *p;
-
-  if (n == 0)
-    return NULL;
-
-  p = xmalloc (n);
-  if (p == NULL)
-    gfc_fatal_error ("Allocation would exceed memory limit -- malloc() failed");
-  memset (p, 0, n);
-  return p;
-}
-
 
 /* Get terminal width.  */
 
diff --git a/gcc/fortran/options.c b/gcc/fortran/options.c
index a4d9a66..bc65f6b 100644
--- a/gcc/fortran/options.c
+++ b/gcc/fortran/options.c
@@ -471,7 +471,7 @@  gfc_handle_module_path_options (const char *arg)
   if (gfc_option.module_dir != NULL)
     gfc_fatal_error ("gfortran: Only one -J option allowed");
 
-  gfc_option.module_dir = (char *) gfc_getmem (strlen (arg) + 2);
+  gfc_option.module_dir = XCNEWVEC (char, strlen (arg) + 2);
   strcpy (gfc_option.module_dir, arg);
 
   gfc_add_include_path (gfc_option.module_dir, true, false);
@@ -1056,7 +1056,7 @@  gfc_get_option_string (void)
         }
     }
 
-  result = (char *) gfc_getmem (len);
+  result = XCNEWVEC (char, len);
 
   pos = 0; 
   for (j = 1; j < save_decoded_options_count; j++)
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index 09cfe78..c101612 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -8598,7 +8598,7 @@  gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
       total_var = gfc_count_forall_iterators (code);
 
       /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements.  */
-      var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
+      var_expr = XCNEWVEC (gfc_expr *, total_var);
     }
 
   /* The information about FORALL iterator, including FORALL index start, end
diff --git a/gcc/fortran/scanner.c b/gcc/fortran/scanner.c
index 7f99eb8..f99429a 100644
--- a/gcc/fortran/scanner.c
+++ b/gcc/fortran/scanner.c
@@ -2012,8 +2012,8 @@  load_file (const char *realfilename, const char *displayedname, bool initial)
 
       /* Add line.  */
 
-      b = (gfc_linebuf *) gfc_getmem (gfc_linebuf_header_size
-				      + (len + 1) * sizeof (gfc_char_t));
+      b = (gfc_linebuf *) xcalloc (1, gfc_linebuf_header_size
+				   + (len + 1) * sizeof (gfc_char_t));
 
       b->location
 	= linemap_line_start (line_table, current_file->line++, 120);
diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c
index c2ece95..4c91563 100644
--- a/gcc/fortran/simplify.c
+++ b/gcc/fortran/simplify.c
@@ -517,7 +517,7 @@  simplify_transformation_to_array (gfc_expr *result, gfc_expr *array, gfc_expr *d
   gfc_array_size (array, &size);
   arraysize = mpz_get_ui (size);
 
-  arrayvec = (gfc_expr**) gfc_getmem (sizeof (gfc_expr*) * arraysize);
+  arrayvec = XCNEWVEC (gfc_expr*, arraysize);
 
   array_ctor = gfc_constructor_first (array->value.constructor);
   mask_ctor = NULL;
@@ -543,7 +543,7 @@  simplify_transformation_to_array (gfc_expr *result, gfc_expr *array, gfc_expr *d
   resultsize = mpz_get_ui (size);
   mpz_clear (size);
 
-  resultvec = (gfc_expr**) gfc_getmem (sizeof (gfc_expr*) * resultsize);
+  resultvec = XCNEWVEC (gfc_expr*, resultsize);
   result_ctor = gfc_constructor_first (result->value.constructor);
   for (i = 0; i < resultsize; ++i)
     {
diff --git a/gcc/fortran/target-memory.c b/gcc/fortran/target-memory.c
index 03a5b58..b5c90a7 100644
--- a/gcc/fortran/target-memory.c
+++ b/gcc/fortran/target-memory.c
@@ -569,7 +569,7 @@  gfc_target_interpret_expr (unsigned char *buffer, size_t buffer_size,
   else
     {
       result->representation.string =
-        (char *) gfc_getmem (result->representation.length + 1);
+        XCNEWVEC (char, result->representation.length + 1);
       memcpy (result->representation.string, buffer,
 	      result->representation.length);
       result->representation.string[result->representation.length] = '\0';
diff --git a/gcc/fortran/trans-common.c b/gcc/fortran/trans-common.c
index a2b2605..b6318b7 100644
--- a/gcc/fortran/trans-common.c
+++ b/gcc/fortran/trans-common.c
@@ -133,7 +133,7 @@  get_segment_info (gfc_symbol * sym, HOST_WIDE_INT offset)
     gfc_conv_const_charlen (sym->ts.u.cl);
 
   /* Create the segment_info and fill it in.  */
-  s = (segment_info *) gfc_getmem (sizeof (segment_info));
+  s = XCNEW (segment_info);
   s->sym = sym;
   /* We will use this type when building the segment aggregate type.  */
   s->field = gfc_sym_type (sym);
@@ -155,14 +155,14 @@  copy_equiv_list_to_ns (segment_info *c)
   gfc_equiv_info *s;
   gfc_equiv_list *l;
 
-  l = (gfc_equiv_list *) gfc_getmem (sizeof (gfc_equiv_list));
+  l = XCNEW (gfc_equiv_list);
 
   l->next = c->sym->ns->equiv_lists;
   c->sym->ns->equiv_lists = l;
 
   for (f = c; f; f = f->next)
     {
-      s = (gfc_equiv_info *) gfc_getmem (sizeof (gfc_equiv_info));
+      s = XCNEW (gfc_equiv_info);
       s->next = l->equiv;
       l->equiv = s;
       s->sym = f->sym;
@@ -505,8 +505,8 @@  get_init_field (segment_info *head, tree union_type, tree *field_init,
 
   /* Now absorb all the initializer data into a single vector,
      whilst checking for overlapping, unequal values.  */
-  data = (unsigned char*)gfc_getmem ((size_t)length);
-  chk = (unsigned char*)gfc_getmem ((size_t)length);
+  data = XCNEWVEC (unsigned char, (size_t)length);
+  chk = XCNEWVEC (unsigned char, (size_t)length);
 
   /* TODO - change this when default initialization is implemented.  */
   memset (data, '\0', (size_t)length);
diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c
index af19d32..73d8a5f 100644
--- a/gcc/fortran/trans-expr.c
+++ b/gcc/fortran/trans-expr.c
@@ -3957,8 +3957,8 @@  gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
   n = 0;
   for (fargs = sym->formal; fargs; fargs = fargs->next)
     n++;
-  saved_vars = (gfc_saved_var *)gfc_getmem (n * sizeof (gfc_saved_var));
-  temp_vars = (tree *)gfc_getmem (n * sizeof (tree));
+  saved_vars = XCNEWVEC (gfc_saved_var, n);
+  temp_vars = XCNEWVEC (tree, n);
 
   for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
     {
diff --git a/gcc/fortran/trans-io.c b/gcc/fortran/trans-io.c
index a9ad4a6..8021bc6 100644
--- a/gcc/fortran/trans-io.c
+++ b/gcc/fortran/trans-io.c
@@ -1450,7 +1450,7 @@  nml_full_name (const char* var_name, const char* cmp_name)
   char * full_name;
 
   full_name_length = strlen (var_name) + strlen (cmp_name) + 1;
-  full_name = (char*)gfc_getmem (full_name_length + 1);
+  full_name = XCNEWVEC (char, full_name_length + 1);
   strcpy (full_name, var_name);
   full_name = strcat (full_name, "%");
   full_name = strcat (full_name, cmp_name);
diff --git a/gcc/fortran/trans-stmt.c b/gcc/fortran/trans-stmt.c
index 7d72b7e..7e08e8d 100644
--- a/gcc/fortran/trans-stmt.c
+++ b/gcc/fortran/trans-stmt.c
@@ -3455,15 +3455,15 @@  gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
   nvar = n;
 
   /* Allocate the space for var, start, end, step, varexpr.  */
-  var = (tree *) gfc_getmem (nvar * sizeof (tree));
-  start = (tree *) gfc_getmem (nvar * sizeof (tree));
-  end = (tree *) gfc_getmem (nvar * sizeof (tree));
-  step = (tree *) gfc_getmem (nvar * sizeof (tree));
-  varexpr = (gfc_expr **) gfc_getmem (nvar * sizeof (gfc_expr *));
-  saved_vars = (gfc_saved_var *) gfc_getmem (nvar * sizeof (gfc_saved_var));
+  var = XCNEWVEC (tree, nvar);
+  start = XCNEWVEC (tree, nvar);
+  end = XCNEWVEC (tree, nvar);
+  step = XCNEWVEC (tree, nvar);
+  varexpr = XCNEWVEC (gfc_expr *, nvar);
+  saved_vars = XCNEWVEC (gfc_saved_var, nvar);
 
   /* Allocate the space for info.  */
-  info = (forall_info *) gfc_getmem (sizeof (forall_info));
+  info = XCNEW (forall_info);
 
   gfc_start_block (&pre);
   gfc_init_block (&post);
@@ -3475,7 +3475,7 @@  gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
       gfc_symbol *sym = fa->var->symtree->n.sym;
 
       /* Allocate space for this_forall.  */
-      this_forall = (iter_info *) gfc_getmem (sizeof (iter_info));
+      this_forall = XCNEW (iter_info);
 
       /* Create a temporary variable for the FORALL index.  */
       tmp = gfc_typenode_for_spec (&sym->ts);
diff --git a/libgcc/configure b/libgcc/configure
old mode 100644
new mode 100755