PATCH: Allocate sufficient space for string buffer

H.J. Lu hongjiu.lu@intel.com
Thu Jun 7 18:59:00 GMT 2012


Hi,

This allocates sufficient space for string buffer to avoid unnecessary
realloc calls, which may fail, depending on malloc implementation.  OK
to install?

Thanks.


H.J.
---
2012-06-07  H.J. Lu  <hongjiu.lu@intel.com>

	* input-scrub.c (input_scrub_include_sb): Use sb_build to
	allocate sufficient space for from_sb.
	* read.c (do_repeat): Use sb_build to allocate sufficient space
	for many.
	* sb.c (sb_build): Remove static.
	* sb.h (sb_build): New prototype.

diff --git a/gas/input-scrub.c b/gas/input-scrub.c
index 46dd244..78b6a8d 100644
--- a/gas/input-scrub.c
+++ b/gas/input-scrub.c
@@ -277,7 +277,7 @@ input_scrub_include_sb (sb *from, char *position, int is_expansion)
 
   next_saved_file = input_scrub_push (position);
 
-  sb_new (&from_sb);
+  sb_build (&from_sb, from->len + 1);
   from_sb_is_expansion = is_expansion;
   if (from->len >= 1 && from->ptr[0] != '\n')
     {
diff --git a/gas/read.c b/gas/read.c
index 2b37173..7119d50 100644
--- a/gas/read.c
+++ b/gas/read.c
@@ -3207,7 +3207,7 @@ do_repeat (int count, const char *start, const char *end)
       return;
     }
 
-  sb_new (&many);
+  sb_build (&many, count * one.len + 1);
   while (count-- > 0)
     sb_add_sb (&many, &one);
 
diff --git a/gas/sb.c b/gas/sb.c
index f345fe1..3872881 100644
--- a/gas/sb.c
+++ b/gas/sb.c
@@ -44,7 +44,7 @@ static void sb_check (sb *, size_t);
 
 /* Initializes an sb.  */
 
-static void
+void
 sb_build (sb *ptr, size_t size)
 {
   ptr->ptr = xmalloc (size + 1);
diff --git a/gas/sb.h b/gas/sb.h
index cc11ef6..ea010ee 100644
--- a/gas/sb.h
+++ b/gas/sb.h
@@ -53,6 +53,7 @@ typedef struct sb
 sb;
 
 extern void sb_new (sb *);
+extern void sb_build (sb *, size_t);
 extern void sb_kill (sb *);
 extern void sb_add_sb (sb *, sb *);
 extern void sb_scrub_and_add_sb (sb *, sb *);



More information about the Binutils mailing list