public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Seija K." <doremylover123@gmail.com>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH] Combine malloc + memset to calloc
Date: Fri, 12 Nov 2021 16:04:09 -0500	[thread overview]
Message-ID: <CAA42iKynUBhfnc=b5Lz0Ev=wmThLv9fAFSyKJ9Lba84Q9QmTWA@mail.gmail.com> (raw)

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..0086aceef84 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)

             reply	other threads:[~2021-11-12 21:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-12 21:04 Seija K. [this message]
  -- strict thread matches above, loose matches on Subject: below --
2021-11-12 21:06 Seija K.
2021-11-12 21:17 ` Arnaud Charlet
2021-11-12 20:29 Seija K.
2021-11-13  9:53 ` Prathamesh Kulkarni
2021-11-15 23:54 ` Iain Buclaw

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAA42iKynUBhfnc=b5Lz0Ev=wmThLv9fAFSyKJ9Lba84Q9QmTWA@mail.gmail.com' \
    --to=doremylover123@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).