From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4377 invoked by alias); 6 Jul 2011 07:00:54 -0000 Received: (qmail 4368 invoked by uid 22791); 6 Jul 2011 07:00:53 -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; Wed, 06 Jul 2011 07:00:33 +0000 Received: from localhost (localhost [127.0.0.1]) by filtered-smtp.eu.adacore.com (Postfix) with ESMTP id 7C14FCB0241; Wed, 6 Jul 2011 09:00:32 +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 q7PoZfcB78Zv; Wed, 6 Jul 2011 09:00:29 +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 732B7CB0224; Wed, 6 Jul 2011 09:00:29 +0200 (CEST) Subject: Re: [patch/gas]: simplify frag_grow Mime-Version: 1.0 (Apple Message framework v1084) Content-Type: text/plain; charset=us-ascii From: Tristan Gingold In-Reply-To: <20110706032646.GG26365@bubble.grove.modra.org> Date: Wed, 06 Jul 2011 07:25:00 -0000 Cc: binutils Development Content-Transfer-Encoding: 7bit Message-Id: References: <20110706032646.GG26365@bubble.grove.modra.org> To: Alan Modra 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/msg00074.txt.bz2 On Jul 6, 2011, at 5:26 AM, Alan Modra wrote: > On Tue, Jul 05, 2011 at 02:04:35PM +0200, Tristan Gingold wrote: > > I agree with all your reasoning, but I'd like to keep the final check > so that we don't depend on obstack_chunk_alloc being xmalloc. > >> - if (obstack_room (&frchain_now->frch_obstack) < nchars) >> - as_fatal (_("can't extend frag %u chars"), nchars); >> } Thank you for the review. Here is the updated version with this check added. Tristan. diff --git a/gas/frags.c b/gas/frags.c index b573ce8..5caa16c 100644 --- a/gas/frags.c +++ b/gas/frags.c @@ -75,41 +75,47 @@ 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; + /* 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); + + /* Force to allocate at least NEWC bytes. */ + oldc = obstack_chunk_size (&frchain_now->frch_obstack); + obstack_chunk_size (&frchain_now->frch_obstack) = newc; + + /* Do the real work: create a new frag. */ + frag_new (0); + + /* Restore the old chunk size. */ + obstack_chunk_size (&frchain_now->frch_obstack) = oldc; + + /* Make it obvious that we succeed. */ + if (obstack_room (&frchain_now->frch_obstack) < nchars) + as_fatal (_("can't extend frag %u chars"), nchars); } - if (obstack_room (&frchain_now->frch_obstack) < nchars) - as_fatal (_("can't extend frag %u chars"), nchars); } /* Call this to close off a completed frag, and start up a new (empty)