diff mbox

[fortran] Use xcalloc instead of gfc_getmem

Message ID BANLkTinsA7qU1UoTPq5F7uE2E0J5_D-mTA@mail.gmail.com
State New
Headers show

Commit Message

Janne Blomqvist April 18, 2011, 8:41 p.m. UTC
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?

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

	* misc.c (gfc_getmem): Remove function.
	* gfortran.h: Remove gfc_getmem prototype. Replace gfc_getmem
	usage with xcalloc.
	* expr.c (gfc_check_assign_symbol): Replace gfc_getmem usage with
	xcalloc.
	* options.c (gfc_handle_module_path_options)
	(gfc_get_option_string): Likewise.
	* resolve.c (gfc_resolve_forall): Likewise.
	* scanner.c (load_file): 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.

Comments

Steve Kargl April 18, 2011, 8:50 p.m. UTC | #1
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.

(I suppose we'll never have sizeof(char) != 1).
Janne Blomqvist April 19, 2011, 7:41 a.m. UTC | #2
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.

> (I suppose we'll never have sizeof(char) != 1).

Indeed, ISO C defines sizeof(char) == 1. See 6.5.3.4 (page 80) in n1256.pdf.
Jakub Jelinek April 19, 2011, 7:46 a.m. UTC | #3
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.

	Jakub
diff mbox

Patch

diff --git a/gcc/fortran/expr.c b/gcc/fortran/expr.c
index 1e31653..b300398 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 = (gfc_symtree *) xcalloc (1, sizeof (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..e64409b 100644
--- a/gcc/fortran/gfortran.h
+++ b/gcc/fortran/gfortran.h
@@ -1273,7 +1273,7 @@  typedef struct gfc_entry_list
 gfc_entry_list;
 
 #define gfc_get_entry_list() \
-  (gfc_entry_list *) gfc_getmem(sizeof(gfc_entry_list))
+  (gfc_entry_list *) xcalloc (1, sizeof(gfc_entry_list))
 
 /* Lists of rename info for the USE statement.  */
 
@@ -1303,7 +1303,7 @@  typedef struct gfc_use_list
 gfc_use_list;
 
 #define gfc_get_use_list() \
-  (gfc_use_list *) gfc_getmem(sizeof(gfc_use_list))
+  (gfc_use_list *) xcalloc (1, sizeof(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 +1783,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) ((mpz_t *) xcalloc((rank), sizeof(mpz_t)))
 
 /* Structures for information associated with different kinds of
    numbers.  The first set of integer parameters define all there is
@@ -2369,7 +2369,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..753a365 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 = (char *) xcalloc (strlen (arg) + 2, 1);
   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 = (char *) xcalloc (len, 1);
 
   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..60aed9a 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 = (gfc_expr **) xcalloc (total_var, sizeof (gfc_expr *));
     }
 
   /* 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..1e052c9 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 = (gfc_expr**) xcalloc (arraysize, sizeof (gfc_expr*));
 
   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 = (gfc_expr**) xcalloc (resultsize, sizeof (gfc_expr*));
   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..daa88cf 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);
+        (char *) xcalloc (result->representation.length + 1, 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..0cd4699 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 = (segment_info *) xcalloc (1, sizeof (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 = (gfc_equiv_list *) xcalloc (1, sizeof (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 = (gfc_equiv_info *) xcalloc (1, sizeof (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 = (unsigned char*) xcalloc ((size_t)length, 1);
+  chk = (unsigned char*) xcalloc ((size_t)length, 1);
 
   /* 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..a004764 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 = (gfc_saved_var *) xcalloc (n, sizeof (gfc_saved_var));
+  temp_vars = (tree *) xcalloc (n, sizeof (tree));
 
   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..08d6831 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 = (char*) xcalloc (full_name_length + 1, 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..9f958cb 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 = (tree *) xcalloc (nvar, sizeof (tree));
+  start = (tree *) xcalloc (nvar, sizeof (tree));
+  end = (tree *) xcalloc (nvar, sizeof (tree));
+  step = (tree *) xcalloc (nvar, sizeof (tree));
+  varexpr = (gfc_expr **) xcalloc (nvar, sizeof (gfc_expr *));
+  saved_vars = (gfc_saved_var *) xcalloc (nvar, sizeof (gfc_saved_var));
 
   /* Allocate the space for info.  */
-  info = (forall_info *) gfc_getmem (sizeof (forall_info));
+  info = (forall_info *) xcalloc (1, sizeof (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 = (iter_info *) xcalloc (1, sizeof (iter_info));
 
       /* Create a temporary variable for the FORALL index.  */
       tmp = gfc_typenode_for_spec (&sym->ts);