From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6360 invoked by alias); 5 Jul 2011 12:04:58 -0000 Received: (qmail 6349 invoked by uid 22791); 5 Jul 2011 12:04:57 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from mel.act-europe.fr (HELO mel.act-europe.fr) (194.98.77.210) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 05 Jul 2011 12:04:39 +0000 Received: from localhost (localhost [127.0.0.1]) by filtered-smtp.eu.adacore.com (Postfix) with ESMTP id 91A3BCB029E for ; Tue, 5 Jul 2011 14:04:38 +0200 (CEST) Received: from mel.act-europe.fr ([127.0.0.1]) by localhost (smtp.eu.adacore.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id a+Qe2Xc7YwDY for ; Tue, 5 Jul 2011 14:04:35 +0200 (CEST) Received: from ulanbator.act-europe.fr (ulanbator.act-europe.fr [10.10.1.67]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by mel.act-europe.fr (Postfix) with ESMTP id 8FB2DCB0394 for ; Tue, 5 Jul 2011 14:04:35 +0200 (CEST) From: Tristan Gingold Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: [patch/gas]: simplify frag_grow Date: Tue, 05 Jul 2011 17:49:00 -0000 Message-Id: To: binutils Development Mime-Version: 1.0 (Apple Message framework v1084) X-IsSubscribed: yes Mailing-List: contact binutils-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: binutils-owner@sourceware.org X-SW-Source: 2011-07/txt/msg00053.txt.bz2 Hi, the code for frag_grow looks overly complex. First, if the condition of the first 'if' statement is false, there is no n= eed to execute the second 'if' statement (as this is the same condition). Thus the second 'if' statement can be moved inside th= e first one. The local variable 'n' is not really used. The 'while' loop can only be executed once. The function 'frag_new' can ei= ther succeed or abort via exit (it uses xmalloc to allocate memory as defin= ed by as.h). Instead of referencing directly the internal obstack field chunk_size, we c= an use the documented obstack_chunk_size macro. There is no need to create an empty frag if there was not enough room in th= e 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 =3D 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 =3D 2 * nchars; else newc =3D nchars + 0x10000; newc +=3D SIZEOF_STRUCT_FRAG; if (newc <=3D 0) as_fatal (_("can't extend frag to %u chars"), nchars); obstack_chunk_size (&frchain_now->frch_obstack) =3D newc; frag_new (0); obstack_chunk_size (&frchain_now->frch_obstack) =3D oldc; } } Tristan. 2011-07-05 Tristan Gingold * 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; } =20 -/* 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. */ =20 void frag_grow (unsigned int nchars) { if (obstack_room (&frchain_now->frch_obstack) < nchars) { - unsigned int n; long oldc; + long newc; =20 + /* Not enough room in this frag. Close it. */ frag_wane (frag_now); - frag_new (0); - oldc =3D frchain_now->frch_obstack.chunk_size; + + oldc =3D 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 =3D 2 * nchars; + newc =3D 2 * nchars; else - frchain_now->frch_obstack.chunk_size =3D nchars + 0x10000; - frchain_now->frch_obstack.chunk_size +=3D SIZEOF_STRUCT_FRAG; - if (frchain_now->frch_obstack.chunk_size > 0) - while ((n =3D 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 =3D oldc; + newc =3D nchars + 0x10000; + newc +=3D SIZEOF_STRUCT_FRAG; + + if (newc <=3D 0) + as_fatal (_("can't extend frag to %u chars"), nchars); + + obstack_chunk_size (&frchain_now->frch_obstack) =3D newc; + + frag_new (0); + + obstack_chunk_size (&frchain_now->frch_obstack) =3D oldc; } - if (obstack_room (&frchain_now->frch_obstack) < nchars) - as_fatal (_("can't extend frag %u chars"), nchars); }