This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] replace some raw xmalloc / xrealloc with the XNEW* macros


From: Trevor Saunders <tbsaunde@tbsaunde.org>

Hi,

This increases consistancy of how we allocate memory, and always casting the
result to the proper type.  It also helps make sure we get any use of sizeof on
the result type correct.

built and regtested on x86_64-linux-gnu, ok?

Trev

gas/ChangeLog:

2016-03-19  Trevor Saunders  <tbsaunde+binutils@tbsaunde.org>

	* listing.c (listing_message): Use XNEW style allocation macros.
	* read.c (read_a_source_file): Likewise.
	(read_symbol_name): Likewise.
	(s_mri_common): Likewise.
	(assign_symbol): Likewise.
	(s_reloc): Likewise.
	(emit_expr_with_reloc): Likewise.
	(s_incbin): Likewise.
	(s_include): Likewise.
	* sb.c (sb_build): Likewise.
	(sb_check): Likewise.
---
 gas/listing.c |  2 +-
 gas/read.c    | 33 ++++++++++++++++-----------------
 gas/sb.c      |  4 ++--
 3 files changed, 19 insertions(+), 20 deletions(-)

diff --git a/gas/listing.c b/gas/listing.c
index 4b5fd34..53e84ab 100644
--- a/gas/listing.c
+++ b/gas/listing.c
@@ -236,7 +236,7 @@ listing_message (const char *name, const char *message)
     {
       unsigned int l = strlen (name) + strlen (message) + 1;
       char *n = (char *) xmalloc (l);
-      struct list_message *lm = xmalloc (sizeof *lm);
+      struct list_message *lm = XNEW (struct list_message);
       strcpy (n, name);
       strcat (n, message);
       lm->message = n;
diff --git a/gas/read.c b/gas/read.c
index ea6d9f6..a22c750 100644
--- a/gas/read.c
+++ b/gas/read.c
@@ -879,7 +879,7 @@ read_a_source_file (const char *name)
 		      /* Copy it for safe keeping.  Also give an indication of
 			 how much macro nesting is involved at this point.  */
 		      len = s - input_line_pointer;
-		      copy = (char *) xmalloc (len + macro_nest + 2);
+		      copy = XNEWVEC (char, len + macro_nest + 2);
 		      memset (copy, '>', macro_nest);
 		      copy[macro_nest] = ' ';
 		      memcpy (copy + macro_nest + 1, input_line_pointer, len);
@@ -1271,7 +1271,7 @@ read_a_source_file (const char *name)
 		     that goes with this #APP  There is one.  The specs
 		     guarantee it...  */
 		  tmp_len = buffer_limit - s;
-		  tmp_buf = (char *) xmalloc (tmp_len + 1);
+		  tmp_buf = XNEWVEC (char, tmp_len + 1);
 		  memcpy (tmp_buf, s, tmp_len);
 		  do
 		    {
@@ -1287,7 +1287,7 @@ read_a_source_file (const char *name)
 		      else
 			num = buffer_limit - buffer;
 
-		      tmp_buf = (char *) xrealloc (tmp_buf, tmp_len + num);
+		      tmp_buf = XRESIZEVEC (char, tmp_buf, tmp_len + num);
 		      memcpy (tmp_buf + tmp_len, buffer, num);
 		      tmp_len += num;
 		    }
@@ -1308,7 +1308,7 @@ read_a_source_file (const char *name)
 	      scrub_string_end = ends;
 
 	      new_length = ends - s;
-	      new_buf = (char *) xmalloc (new_length);
+	      new_buf = XNEWVEC (char, new_length);
 	      new_tmp = new_buf;
 	      for (;;)
 		{
@@ -1324,7 +1324,7 @@ read_a_source_file (const char *name)
 		      break;
 		    }
 
-		  new_buf = (char *) xrealloc (new_buf, new_length + 100);
+		  new_buf = XRESIZEVEC (char, new_buf, new_length + 100);
 		  new_tmp = new_buf + new_length;
 		  new_length += 100;
 		}
@@ -1657,7 +1657,7 @@ read_symbol_name (void)
       char * name_end;
       unsigned int C;
 
-      start = name = xmalloc (len + 1);
+      start = name = XNEWVEC (char, len + 1);
 
       name_end = name + SYM_NAME_CHUNK_LEN;
 
@@ -1669,7 +1669,7 @@ read_symbol_name (void)
 
 	      sofar = name - start;
 	      len += SYM_NAME_CHUNK_LEN;
-	      start = xrealloc (start, len + 1);
+	      start = XRESIZEVEC (char, start, len + 1);
 	      name_end = start + len;
 	      name = start + sofar;
 	    }
@@ -1697,7 +1697,7 @@ read_symbol_name (void)
 	;
 
       len = (input_line_pointer - name) - 1;
-      start = xmalloc (len + 1);
+      start = XNEWVEC (char, len + 1);
 
       memcpy (start, name, len);
       start[len] = 0;
@@ -1850,9 +1850,8 @@ s_mri_common (int small ATTRIBUTE_UNUSED)
 
       if (line_label != NULL)
 	{
-	  alc = (char *) xmalloc (strlen (S_GET_NAME (line_label))
-				  + (input_line_pointer - name)
-				  + 1);
+	  alc = XNEWVEC (char, strlen (S_GET_NAME (line_label))
+			 + (input_line_pointer - name) + 1);
 	  sprintf (alc, "%s%s", name, S_GET_NAME (line_label));
 	  name = alc;
 	}
@@ -3238,7 +3237,7 @@ assign_symbol (char *name, int mode)
       if (listing & LISTING_SYMBOLS)
 	{
 	  extern struct list_info_struct *listing_tail;
-	  fragS *dummy_frag = (fragS *) xcalloc (1, sizeof (fragS));
+	  fragS *dummy_frag = XCNEW (fragS);
 	  dummy_frag->line = listing_tail;
 	  dummy_frag->fr_symbol = symbolP;
 	  symbol_set_frag (symbolP, dummy_frag);
@@ -4067,7 +4066,7 @@ s_reloc (int ignore ATTRIBUTE_UNUSED)
     { "64", BFD_RELOC_64 }
   };
 
-  reloc = (struct reloc_list *) xmalloc (sizeof (*reloc));
+  reloc = XNEW (struct reloc_list);
 
   if (flag_mri)
     stop = mri_comment_field (&stopc);
@@ -4347,7 +4346,7 @@ emit_expr_with_reloc (expressionS *exp,
     {
       struct broken_word *x;
 
-      x = (struct broken_word *) xmalloc (sizeof (struct broken_word));
+      x = XNEW (struct broken_word);
       x->next_broken_word = broken_words;
       broken_words = x;
       x->seg = now_seg;
@@ -5858,7 +5857,7 @@ s_incbin (int x ATTRIBUTE_UNUSED)
     {
       int i;
 
-      path = (char *) xmalloc ((unsigned long) len + include_dir_maxlen + 5);
+      path = XNEWVEC (char, (unsigned long) len + include_dir_maxlen + 5);
 
       for (i = 0; i < include_dir_count; i++)
 	{
@@ -5961,8 +5960,8 @@ s_include (int arg ATTRIBUTE_UNUSED)
     }
 
   demand_empty_rest_of_line ();
-  path = (char *) xmalloc ((unsigned long) i
-			   + include_dir_maxlen + 5 /* slop */ );
+  path = XNEWVEC (char, (unsigned long) i
+		  + include_dir_maxlen + 5 /* slop */ );
 
   for (i = 0; i < include_dir_count; i++)
     {
diff --git a/gas/sb.c b/gas/sb.c
index ed471b8..76d555e 100644
--- a/gas/sb.c
+++ b/gas/sb.c
@@ -59,7 +59,7 @@ static void sb_check (sb *, size_t);
 void
 sb_build (sb *ptr, size_t size)
 {
-  ptr->ptr = xmalloc (size + 1);
+  ptr->ptr = XNEWVEC (char, size + 1);
   ptr->max = size;
   ptr->len = 0;
 }
@@ -147,7 +147,7 @@ sb_check (sb *ptr, size_t len)
 #endif
       max -= MALLOC_OVERHEAD + 1;
       ptr->max = max;
-      ptr->ptr = xrealloc (ptr->ptr, max + 1);
+      ptr->ptr = XRESIZEVEC (char, ptr->ptr, max + 1);
     }
 }
 
-- 
2.1.4


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]