From patchwork Thu Mar 7 20:57:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 31777 Received: (qmail 53931 invoked by alias); 7 Mar 2019 20:57:18 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 53678 invoked by uid 89); 7 Mar 2019 20:57:17 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.3 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_JMF_BL, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=bunches, caught X-HELO: gateway34.websitewelcome.com Received: from gateway34.websitewelcome.com (HELO gateway34.websitewelcome.com) (192.185.149.77) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 07 Mar 2019 20:57:14 +0000 Received: from cm17.websitewelcome.com (cm17.websitewelcome.com [100.42.49.20]) by gateway34.websitewelcome.com (Postfix) with ESMTP id 4AAD94D153 for ; Thu, 7 Mar 2019 14:57:13 -0600 (CST) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id 204nhwZ4590on204nhFYzv; Thu, 07 Mar 2019 14:57:13 -0600 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=NlM6qPJ6DBJ6JD48iHjuOCNIbeNNwaEIaQtXw53FtTk=; b=lunWCpKZJkPw/WQ4OED2n2m0G5 xKa0t5PbA7cce10pViiZPhotYYOn8sXR4jc8pjG9mIa9ZNojkjJWgJ3fzKSDJu6Y+9AoUkc36NS31 3ImP7jc6QGA8C0lbDzCLAUJY/; Received: from 75-166-85-218.hlrn.qwest.net ([75.166.85.218]:53182 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1h204n-002TOW-3C; Thu, 07 Mar 2019 14:57:13 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 7/9] Allocate minimal symbols with malloc Date: Thu, 7 Mar 2019 13:57:07 -0700 Message-Id: <20190307205709.21919-8-tom@tromey.com> In-Reply-To: <20190307205709.21919-1-tom@tromey.com> References: <20190307205709.21919-1-tom@tromey.com> Currently, minimal symbols are allocated on the per-BFD obstack. However, it is also possible for multiple symbol readers to create minimal symbols for a given objfile. In this case, the minimal symbols will be reallocated on the obstack, leading to some waste of storage. This is a memory leak, but I think it won't be caught by tools like valgrind, because valgrind doesn't know about obstacks. This patch fixes the problem by using malloc to allocate the storage for minimal symbols. gdb/ChangeLog 2019-03-07 Tom Tromey * objfiles.h (struct objfile_per_bfd_storage) : Now a unique_xmalloc_ptr. (objfile::msymbols_range::begin, objfile::msymbols_range::end): Update. * minsyms.c (lookup_minimal_symbol_by_pc_section) (build_minimal_symbol_hash_tables) (minimal_symbol_reader::install): Update. --- gdb/ChangeLog | 10 ++++++++++ gdb/minsyms.c | 25 ++++++++++++------------- gdb/objfiles.h | 10 ++++------ 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/gdb/minsyms.c b/gdb/minsyms.c index d937e5d2a99..f86105db7ee 100644 --- a/gdb/minsyms.c +++ b/gdb/minsyms.c @@ -735,7 +735,7 @@ lookup_minimal_symbol_by_pc_section (CORE_ADDR pc_in, struct obj_section *sectio { int best_zero_sized = -1; - msymbol = objfile->per_bfd->msymbols; + msymbol = objfile->per_bfd->msymbols.get (); lo = 0; hi = objfile->per_bfd->minimal_symbol_count - 1; @@ -1291,7 +1291,7 @@ build_minimal_symbol_hash_tables (struct objfile *objfile) /* Now, (re)insert the actual entries. */ for ((i = objfile->per_bfd->minimal_symbol_count, - msym = objfile->per_bfd->msymbols); + msym = objfile->per_bfd->msymbols.get ()); i > 0; i--, msym++) { @@ -1356,14 +1356,16 @@ minimal_symbol_reader::install () alloc_count = m_msym_count + m_objfile->per_bfd->minimal_symbol_count; obstack_blank (&m_objfile->per_bfd->storage_obstack, alloc_count * sizeof (struct minimal_symbol)); - msymbols = (struct minimal_symbol *) - obstack_base (&m_objfile->per_bfd->storage_obstack); + gdb::unique_xmalloc_ptr + msym_holder (XNEWVEC (minimal_symbol, alloc_count)); + msymbols = msym_holder.get (); /* Copy in the existing minimal symbols, if there are any. */ if (m_objfile->per_bfd->minimal_symbol_count) - memcpy ((char *) msymbols, (char *) m_objfile->per_bfd->msymbols, - m_objfile->per_bfd->minimal_symbol_count * sizeof (struct minimal_symbol)); + memcpy (msymbols, m_objfile->per_bfd->msymbols.get (), + m_objfile->per_bfd->minimal_symbol_count + * sizeof (struct minimal_symbol)); /* Walk through the list of minimal symbol bunches, adding each symbol to the new contiguous array of symbols. Note that we start with the @@ -1389,19 +1391,16 @@ minimal_symbol_reader::install () no longer using. */ mcount = compact_minimal_symbols (msymbols, mcount, m_objfile); - - ssize_t shrink_bytes - = (mcount + 1 - alloc_count) * sizeof (struct minimal_symbol); - obstack_blank_fast (&m_objfile->per_bfd->storage_obstack, shrink_bytes); - msymbols = (struct minimal_symbol *) - obstack_finish (&m_objfile->per_bfd->storage_obstack); + msym_holder.reset (XRESIZEVEC (struct minimal_symbol, + msym_holder.release (), + mcount)); /* Attach the minimal symbol table to the specified objfile. The strings themselves are also located in the storage_obstack of this objfile. */ m_objfile->per_bfd->minimal_symbol_count = mcount; - m_objfile->per_bfd->msymbols = msymbols; + m_objfile->per_bfd->msymbols = std::move (msym_holder); /* Now build the hash tables; we can't do this incrementally at an earlier point since we weren't finished with the obstack diff --git a/gdb/objfiles.h b/gdb/objfiles.h index 709525ae5af..d03a8b608b3 100644 --- a/gdb/objfiles.h +++ b/gdb/objfiles.h @@ -281,11 +281,9 @@ struct objfile_per_bfd_storage name and a zero value for the address. This makes it easy to walk through the array when passed a pointer to somewhere in the middle of it. There is also a count of the number of symbols, which does - not include the terminating null symbol. The array itself, as well - as all the data that it points to, should be allocated on the - objfile_obstack for this file. */ + not include the terminating null symbol. */ - minimal_symbol *msymbols = NULL; + gdb::unique_xmalloc_ptr msymbols; int minimal_symbol_count = 0; /* The number of minimal symbols read, before any minimal symbol @@ -374,13 +372,13 @@ struct objfile minimal_symbol_iterator begin () const { - return minimal_symbol_iterator (m_objfile->per_bfd->msymbols); + return minimal_symbol_iterator (m_objfile->per_bfd->msymbols.get ()); } minimal_symbol_iterator end () const { return minimal_symbol_iterator - (m_objfile->per_bfd->msymbols + (m_objfile->per_bfd->msymbols.get () + m_objfile->per_bfd->minimal_symbol_count); }