Combine malloc + memset to calloc

Message ID CAA42iKzvOARh+Kvjp=OWATbnJVVG-On59eA2BE627Mbm+ZBJwg@mail.gmail.com
State New
Headers
Series Combine malloc + memset to calloc |

Commit Message

Seija K. Nov. 12, 2021, 8:29 p.m. UTC
  
  

Comments

Prathamesh Kulkarni Nov. 13, 2021, 9:53 a.m. UTC | #1
On Sat, 13 Nov 2021 at 02:00, Seija K. via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> diff --git a/gcc/ada/terminals.c b/gcc/ada/terminals.c
> index a2dd4895d48..25d9acda752 100644
> --- a/gcc/ada/terminals.c
> +++ b/gcc/ada/terminals.c
> @@ -609,8 +609,7 @@ __gnat_setup_communication (struct TTY_Process**
> process_out) /* output param */
>  {
>    struct TTY_Process* process;
>
> -  process = (struct TTY_Process*)malloc (sizeof (struct TTY_Process));
> -  ZeroMemory (process, sizeof (struct TTY_Process));
> +  process = (struct TTY_Process*)calloc (1, sizeof (struct TTY_Process));
>    *process_out = process;
>
>    return 0;
> diff --git a/gcc/config/rs6000/rs6000-gen-builtins.c
> b/gcc/config/rs6000/rs6000-gen-builtins.c
> index 1655a2fd765..2c895a2d9a9 100644
> --- a/gcc/config/rs6000/rs6000-gen-builtins.c
> +++ b/gcc/config/rs6000/rs6000-gen-builtins.c
> @@ -1307,8 +1307,7 @@ parse_args (prototype *protoptr)
>    do {
>      consume_whitespace ();
>      int oldpos = pos;
> -    typelist *argentry = (typelist *) malloc (sizeof (typelist));
> -    memset (argentry, 0, sizeof *argentry);
> +    typelist *argentry = (typelist *) calloc (1, sizeof (typelist));
Just wondering -- shouldn't this be xcalloc instead (and similarly in
other places) ?
>      typeinfo *argtype = &argentry->info;
>      success = match_type (argtype, VOID_NOTOK);
>      if (success)
> diff --git a/gcc/d/dmd/ctfeexpr.c b/gcc/d/dmd/ctfeexpr.c
> index a8e97833ad0..401ed748f43 100644
> --- a/gcc/d/dmd/ctfeexpr.c
> +++ b/gcc/d/dmd/ctfeexpr.c
> @@ -1350,8 +1350,7 @@ int ctfeRawCmp(Loc loc, Expression *e1, Expression
> *e2)
>          if (es2->keys->length != dim)
>              return 1;
>
> -        bool *used = (bool *)mem.xmalloc(sizeof(bool) * dim);
> -        memset(used, 0, sizeof(bool) * dim);
> +        bool *used = (bool *)mem.xcalloc(dim, sizeof(bool));
>
>          for (size_t i = 0; i < dim; ++i)
>          {
> diff --git a/gcc/internal-fn.c b/gcc/internal-fn.c
> index 0cba95411a6..f5bff8b9441 100644
> --- a/gcc/internal-fn.c
> +++ b/gcc/internal-fn.c
> @@ -3081,9 +3081,16 @@ expand_DEFERRED_INIT (internal_fn, gcall *stmt)
>   0).exists ())
>   {
>    unsigned HOST_WIDE_INT total_bytes = tree_to_uhwi (var_size);
> -  unsigned char *buf = (unsigned char *) xmalloc (total_bytes);
> -  memset (buf, (init_type == AUTO_INIT_PATTERN
> - ? INIT_PATTERN_VALUE : 0), total_bytes);
> +  unsigned char *buf;
> +    if (init_type == AUTO_INIT_PATTERN)
> +      {
> +        buf = (unsigned char *) xmalloc (total_bytes);
> +    memset (buf, INIT_PATTERN_VALUE, total_bytes);
> +      }
> +    else
> +      {
> +        buf = (unsigned char *) xcalloc (1, total_bytes);
> +      }
Formatting nit for else -- no need for braces for single stmt.
In general, please run the patch thru contrib/check_GNU_style.py.
Leaving the actual review to maintainers.

Thanks,
Prathamesh
>    tree itype = build_nonstandard_integer_type
>   (total_bytes * BITS_PER_UNIT, 1);
>    wide_int w = wi::from_buffer (buf, total_bytes);
> diff --git a/libiberty/calloc.c b/libiberty/calloc.c
> index f4bd27b1cd2..1ef4156d28a 100644
> --- a/libiberty/calloc.c
> +++ b/libiberty/calloc.c
> @@ -17,7 +17,7 @@ Uses @code{malloc} to allocate storage for @var{nelem}
> objects of
>
>  /* For systems with larger pointers than ints, this must be declared.  */
>  PTR malloc (size_t);
> -void bzero (PTR, size_t);
> +void memset (PTR, int, size_t);
>
>  PTR
>  calloc (size_t nelem, size_t elsize)
> @@ -28,7 +28,7 @@ calloc (size_t nelem, size_t elsize)
>      nelem = elsize = 1;
>
>    ptr = malloc (nelem * elsize);
> -  if (ptr) bzero (ptr, nelem * elsize);
> +  if (ptr) memset (ptr, 0, nelem * elsize);
>
>    return ptr;
>  }
> diff --git a/libiberty/partition.c b/libiberty/partition.c
> index 81e5fc0f79a..75512d67258 100644
> --- a/libiberty/partition.c
> +++ b/libiberty/partition.c
> @@ -146,8 +146,7 @@ partition_print (partition part, FILE *fp)
>    int e;
>
>    /* Flag the elements we've already printed.  */
> -  done = (char *) xmalloc (num_elements);
> -  memset (done, 0, num_elements);
> +  done = (char *) xcalloc (num_elements, 1);
>
>    /* A buffer used to sort elements in a class.  */
>    class_elements = (int *) xmalloc (num_elements * sizeof (int));
> diff --git a/libobjc/gc.c b/libobjc/gc.c
> index 57895e61930..95a75f5cb2e 100644
> --- a/libobjc/gc.c
> +++ b/libobjc/gc.c
> @@ -307,10 +307,9 @@ __objc_generate_gc_type_description (Class class)
>               / sizeof (void *));
>    size = ROUND (bits_no, BITS_PER_WORD) / BITS_PER_WORD;
>    mask = objc_atomic_malloc (size * sizeof (int));
> -  memset (mask, 0, size * sizeof (int));
>
>    class_structure_type = objc_atomic_malloc (type_size);
> -  *class_structure_type = current = 0;
> +  current = 0;
>    __objc_class_structure_encoding (class, &class_structure_type,
>                                     &type_size, &current);
>    if (current + 1 == type_size)
  
Iain Buclaw Nov. 15, 2021, 11:54 p.m. UTC | #2
Excerpts from Seija K. via Gcc-patches's message of November 12, 2021 9:29 pm:
> diff --git a/gcc/d/dmd/ctfeexpr.c b/gcc/d/dmd/ctfeexpr.c
> index a8e97833ad0..401ed748f43 100644
> --- a/gcc/d/dmd/ctfeexpr.c
> +++ b/gcc/d/dmd/ctfeexpr.c
> @@ -1350,8 +1350,7 @@ int ctfeRawCmp(Loc loc, Expression *e1, Expression
> *e2)
>          if (es2->keys->length != dim)
>              return 1;
> 
> -        bool *used = (bool *)mem.xmalloc(sizeof(bool) * dim);
> -        memset(used, 0, sizeof(bool) * dim);
> +        bool *used = (bool *)mem.xcalloc(dim, sizeof(bool));
> 
>          for (size_t i = 0; i < dim; ++i)
>          {

Hi,

Thanks, however all changes to the dmd front-end should go through
upstream first.  But as this file is about to be dropped, I don't
immediately see a need to keep this part in the patch.

Iain.
  

Patch

diff --git a/gcc/ada/terminals.c b/gcc/ada/terminals.c
index a2dd4895d48..25d9acda752 100644
--- a/gcc/ada/terminals.c
+++ b/gcc/ada/terminals.c
@@ -609,8 +609,7 @@  __gnat_setup_communication (struct TTY_Process**
process_out) /* output param */
 {
   struct TTY_Process* process;

-  process = (struct TTY_Process*)malloc (sizeof (struct TTY_Process));
-  ZeroMemory (process, sizeof (struct TTY_Process));
+  process = (struct TTY_Process*)calloc (1, sizeof (struct TTY_Process));
   *process_out = process;

   return 0;
diff --git a/gcc/config/rs6000/rs6000-gen-builtins.c
b/gcc/config/rs6000/rs6000-gen-builtins.c
index 1655a2fd765..2c895a2d9a9 100644
--- a/gcc/config/rs6000/rs6000-gen-builtins.c
+++ b/gcc/config/rs6000/rs6000-gen-builtins.c
@@ -1307,8 +1307,7 @@  parse_args (prototype *protoptr)
   do {
     consume_whitespace ();
     int oldpos = pos;
-    typelist *argentry = (typelist *) malloc (sizeof (typelist));
-    memset (argentry, 0, sizeof *argentry);
+    typelist *argentry = (typelist *) calloc (1, sizeof (typelist));
     typeinfo *argtype = &argentry->info;
     success = match_type (argtype, VOID_NOTOK);
     if (success)
diff --git a/gcc/d/dmd/ctfeexpr.c b/gcc/d/dmd/ctfeexpr.c
index a8e97833ad0..401ed748f43 100644
--- a/gcc/d/dmd/ctfeexpr.c
+++ b/gcc/d/dmd/ctfeexpr.c
@@ -1350,8 +1350,7 @@  int ctfeRawCmp(Loc loc, Expression *e1, Expression
*e2)
         if (es2->keys->length != dim)
             return 1;

-        bool *used = (bool *)mem.xmalloc(sizeof(bool) * dim);
-        memset(used, 0, sizeof(bool) * dim);
+        bool *used = (bool *)mem.xcalloc(dim, sizeof(bool));

         for (size_t i = 0; i < dim; ++i)
         {
diff --git a/gcc/internal-fn.c b/gcc/internal-fn.c
index 0cba95411a6..f5bff8b9441 100644
--- a/gcc/internal-fn.c
+++ b/gcc/internal-fn.c
@@ -3081,9 +3081,16 @@  expand_DEFERRED_INIT (internal_fn, gcall *stmt)
  0).exists ())
  {
   unsigned HOST_WIDE_INT total_bytes = tree_to_uhwi (var_size);
-  unsigned char *buf = (unsigned char *) xmalloc (total_bytes);
-  memset (buf, (init_type == AUTO_INIT_PATTERN
- ? INIT_PATTERN_VALUE : 0), total_bytes);
+  unsigned char *buf;
+    if (init_type == AUTO_INIT_PATTERN)
+      {
+        buf = (unsigned char *) xmalloc (total_bytes);
+    memset (buf, INIT_PATTERN_VALUE, total_bytes);
+      }
+    else
+      {
+        buf = (unsigned char *) xcalloc (1, total_bytes);
+      }
   tree itype = build_nonstandard_integer_type
  (total_bytes * BITS_PER_UNIT, 1);
   wide_int w = wi::from_buffer (buf, total_bytes);
diff --git a/libiberty/calloc.c b/libiberty/calloc.c
index f4bd27b1cd2..1ef4156d28a 100644
--- a/libiberty/calloc.c
+++ b/libiberty/calloc.c
@@ -17,7 +17,7 @@  Uses @code{malloc} to allocate storage for @var{nelem}
objects of

 /* For systems with larger pointers than ints, this must be declared.  */
 PTR malloc (size_t);
-void bzero (PTR, size_t);
+void memset (PTR, int, size_t);

 PTR
 calloc (size_t nelem, size_t elsize)
@@ -28,7 +28,7 @@  calloc (size_t nelem, size_t elsize)
     nelem = elsize = 1;

   ptr = malloc (nelem * elsize);
-  if (ptr) bzero (ptr, nelem * elsize);
+  if (ptr) memset (ptr, 0, nelem * elsize);

   return ptr;
 }
diff --git a/libiberty/partition.c b/libiberty/partition.c
index 81e5fc0f79a..75512d67258 100644
--- a/libiberty/partition.c
+++ b/libiberty/partition.c
@@ -146,8 +146,7 @@  partition_print (partition part, FILE *fp)
   int e;

   /* Flag the elements we've already printed.  */
-  done = (char *) xmalloc (num_elements);
-  memset (done, 0, num_elements);
+  done = (char *) xcalloc (num_elements, 1);

   /* A buffer used to sort elements in a class.  */
   class_elements = (int *) xmalloc (num_elements * sizeof (int));
diff --git a/libobjc/gc.c b/libobjc/gc.c
index 57895e61930..95a75f5cb2e 100644
--- a/libobjc/gc.c
+++ b/libobjc/gc.c
@@ -307,10 +307,9 @@  __objc_generate_gc_type_description (Class class)
              / sizeof (void *));
   size = ROUND (bits_no, BITS_PER_WORD) / BITS_PER_WORD;
   mask = objc_atomic_malloc (size * sizeof (int));
-  memset (mask, 0, size * sizeof (int));

   class_structure_type = objc_atomic_malloc (type_size);
-  *class_structure_type = current = 0;
+  current = 0;
   __objc_class_structure_encoding (class, &class_structure_type,
                                    &type_size, &current);
   if (current + 1 == type_size)