public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [patch/gas]: simplify frag_grow
@ 2011-07-05 17:49 Tristan Gingold
  2011-07-06  4:45 ` Alan Modra
  2011-07-26 21:14 ` Steve Ellcey
  0 siblings, 2 replies; 14+ messages in thread
From: Tristan Gingold @ 2011-07-05 17:49 UTC (permalink / raw)
  To: binutils Development

Hi,

the code for frag_grow looks overly complex.

First, if the condition of the first 'if' statement is false, there is no need to execute the second 'if' statement (as this is
the same condition).  Thus the second 'if' statement can be moved inside the first one.

The local variable 'n' is not really used.

The 'while' loop can only be executed once.  The function 'frag_new' can either succeed or abort via exit (it uses xmalloc to allocate memory as defined by as.h).

Instead of referencing directly the internal obstack field chunk_size, we can use the documented obstack_chunk_size macro.

There is no need to create an empty frag if there was not enough room in the previous one.  We can call frag_new only once.

No regression on powerpc-elf.

Is it ok for trunk ?

As the new code is easier to read without a diff, here is the new function body:

frag_grow (unsigned int nchars)
{
  if (obstack_room (&frchain_now->frch_obstack) < nchars)
    {
      long oldc;
      long newc;

      /* Not enough room in this frag.  Close it.  */
      frag_wane (frag_now);

      oldc = obstack_chunk_size (&frchain_now->frch_obstack);

      /* Try to allocate a bit more than needed right now.  But don't do
         this if we would waste too much memory.  Especially necessary
         for extremely big (like 2GB initialized) frags.  */
      if (nchars < 0x10000)
        newc = 2 * nchars;
      else
        newc = nchars + 0x10000;
      newc += SIZEOF_STRUCT_FRAG;

      if (newc <= 0)
        as_fatal (_("can't extend frag to %u chars"), nchars);

      obstack_chunk_size (&frchain_now->frch_obstack) = newc;

      frag_new (0);

      obstack_chunk_size (&frchain_now->frch_obstack) = oldc;
    }
}

Tristan.

2011-07-05  Tristan Gingold  <gingold@adacore.com>

	* frags.c (frag_grow): Simplify the code.

diff --git a/gas/frags.c b/gas/frags.c
index b573ce8..02b19ff 100644
--- a/gas/frags.c
+++ b/gas/frags.c
@@ -75,41 +75,41 @@ frag_alloc (struct obstack *ob)
   return ptr;
 }
 

-/* Try to augment current frag by nchars chars.
+/* Try to augment current frag by NCHARS chars.
    If there is no room, close of the current frag with a ".fill 0"
-   and begin a new frag. Unless the new frag has nchars chars available
-   do not return. Do not set up any fields of *now_frag.  */
+   and begin a new frag.  Do not set up any fields of *now_frag.  */
 
 void
 frag_grow (unsigned int nchars)
 {
   if (obstack_room (&frchain_now->frch_obstack) < nchars)
     {
-      unsigned int n;
       long oldc;
+      long newc;
 
+      /* Not enough room in this frag.  Close it.  */
       frag_wane (frag_now);
-      frag_new (0);
-      oldc = frchain_now->frch_obstack.chunk_size;
+
+      oldc = obstack_chunk_size (&frchain_now->frch_obstack);
+
       /* Try to allocate a bit more than needed right now.  But don't do
          this if we would waste too much memory.  Especially necessary
-	 for extremely big (like 2GB initialized) frags.  */
+         for extremely big (like 2GB initialized) frags.  */
       if (nchars < 0x10000)
-	frchain_now->frch_obstack.chunk_size = 2 * nchars;
+        newc = 2 * nchars;
       else
-        frchain_now->frch_obstack.chunk_size = nchars + 0x10000;
-      frchain_now->frch_obstack.chunk_size += SIZEOF_STRUCT_FRAG;
-      if (frchain_now->frch_obstack.chunk_size > 0)
-	while ((n = obstack_room (&frchain_now->frch_obstack)) < nchars
-	       && (unsigned long) frchain_now->frch_obstack.chunk_size > nchars)
-	  {
-	    frag_wane (frag_now);
-	    frag_new (0);
-	  }
-      frchain_now->frch_obstack.chunk_size = oldc;
+        newc = nchars + 0x10000;
+      newc += SIZEOF_STRUCT_FRAG;
+
+      if (newc <= 0)
+        as_fatal (_("can't extend frag to %u chars"), nchars);
+
+      obstack_chunk_size (&frchain_now->frch_obstack) = newc;
+
+      frag_new (0);
+
+      obstack_chunk_size (&frchain_now->frch_obstack) = oldc;
     }
-  if (obstack_room (&frchain_now->frch_obstack) < nchars)
-    as_fatal (_("can't extend frag %u chars"), nchars);
 }

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2011-08-01  8:06 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-05 17:49 [patch/gas]: simplify frag_grow Tristan Gingold
2011-07-06  4:45 ` Alan Modra
2011-07-06  7:25   ` Tristan Gingold
2011-07-11 12:05     ` Alan Modra
2011-07-25 14:49       ` Tristan Gingold
2011-07-26 20:14         ` Steve Ellcey
2011-07-26 21:14 ` Steve Ellcey
2011-07-27  6:56   ` Hans-Peter Nilsson
2011-07-27  7:39     ` Tristan Gingold
2011-07-27 10:06     ` Tristan Gingold
2011-07-27 16:08     ` [patch v2/gas]: " Tristan Gingold
2011-07-29  6:01       ` Alan Modra
2011-08-01  8:06         ` Tristan Gingold
2011-07-27 11:42   ` [patch/gas]: " Tristan Gingold

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