From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id OOOzEt18WWgt1hoAWB0awg (envelope-from ) for ; Mon, 23 Jun 2025 12:12:13 -0400 Received: by simark.ca (Postfix, from userid 112) id 488231E11C; Mon, 23 Jun 2025 12:12:13 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-9.0 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED, RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE autolearn=ham autolearn_force=no version=4.0.1 Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 71B0D1E0C2 for ; Mon, 23 Jun 2025 12:12:11 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 15DA23846E63 for ; Mon, 23 Jun 2025 16:12:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 15DA23846E63 Received: from us-smtp-delivery-114.mimecast.com (us-smtp-delivery-114.mimecast.com [170.10.133.114]) by sourceware.org (Postfix) with ESMTP id 715773846E7F for ; Mon, 23 Jun 2025 16:10:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 715773846E7F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=labware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=labware.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 715773846E7F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.114 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1750695056; cv=none; b=hfwUVExJrUqOEFMdrc9gmhJtXcIXVVR54gmLy3sFP9cSNuzG+ESFCTtVqK+uMDbCG5hysucbk11k2o0XKFKuLOkVw3KrwdqnwwoKJco1mRUAav+SJ6IjG7jENw2PspyYDjJrf9mKllm0Z3cO5wiO0c52+7mItEsXcN29bmwlHzQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1750695056; c=relaxed/simple; bh=s9hUWBrftbk76oDSVlxKhXPvVFvmxNoVvCTtndz5O60=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=Cwyskra4tJuZBYU6g2pXBc6yaPbORrOogLH6UmEtYPmGu/w9tj6JVZrfcUDRbEJBLLHCf3zTnfSe3L486SHiM6ZUcYmHWNTD4wBosxfufct9xLbzP38/7VuUfJCPu/kWm7rsDcMTPqO3hFTTwio5CeQZ6jZB6y3o75gWPwFkSiQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 715773846E7F Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2094.outbound.protection.outlook.com [40.107.237.94]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-8-zdRF1FrCM1apYebF2b2tWA-1; Mon, 23 Jun 2025 12:10:53 -0400 X-MC-Unique: zdRF1FrCM1apYebF2b2tWA-1 X-Mimecast-MFC-AGG-ID: zdRF1FrCM1apYebF2b2tWA_1750695053 Received: from SA1PR17MB5365.namprd17.prod.outlook.com (2603:10b6:806:1d8::11) by PH0PR17MB4637.namprd17.prod.outlook.com (2603:10b6:510:81::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8857.29; Mon, 23 Jun 2025 16:10:52 +0000 Received: from SA1PR17MB5365.namprd17.prod.outlook.com ([fe80::6398:7dc0:7d88:5a4d]) by SA1PR17MB5365.namprd17.prod.outlook.com ([fe80::6398:7dc0:7d88:5a4d%5]) with mapi id 15.20.8857.026; Mon, 23 Jun 2025 16:10:51 +0000 From: Jan Vrany To: gdb-patches@sourceware.org CC: Jan Vrany Subject: [RFC v5 06/18] gdb: use std::vector<> to hold on blocks in struct blockvector Date: Mon, 23 Jun 2025 17:10:01 +0100 Message-ID: <20250623161013.650814-7-jan.vrany@labware.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250623161013.650814-1-jan.vrany@labware.com> References: <20250623161013.650814-1-jan.vrany@labware.com> X-ClientProxiedBy: LO4P123CA0225.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:1a6::14) To SA1PR17MB5365.namprd17.prod.outlook.com (2603:10b6:806:1d8::11) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA1PR17MB5365:EE_|PH0PR17MB4637:EE_ X-MS-Office365-Filtering-Correlation-Id: 3a9be817-698c-4359-d03f-08ddb2708669 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|376014|1800799024 X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?/JuOgARoEk0f/hZx2gT5QKM3ptaml5AZQzVFXW9wAYKMSNDGVXo1fvSxO0nG?= =?us-ascii?Q?PMNxfePuMnJNmtBXlpoT+Mvy9qgATgiMCOwtfPv3inH99pZ6NI486TL/8h1U?= =?us-ascii?Q?C65Yya2JICJyO6Z9zATB0Eyaqp/B6svdmuuiatQy5XWjCqmJXsQlU882K5u4?= =?us-ascii?Q?E5znVKBInOtaaVtSuE3Lc500gfa2/hrob1Q3nYI1GAEXr/RUO05qC/i0Kgpt?= =?us-ascii?Q?e3eZyfpU5UGuUHRLQSAvBFPObf/Xnld8n/Xy2hjp3qfuWvC7ddO44Jsk9wHq?= =?us-ascii?Q?ggz/92qfhANFsfRY2kbZn1wP876QiUPOMPOU+4ZjKZU/pxCPzB4p/E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?46LXlSAugmzFfuAruqdEdJnm1T/C77HCPkLOrL0WC8pfBGqRZumIwCicUeAX?= =?us-ascii?Q?kUCnxl7tsmFePMQODU001DsJd/jki0CIMRx0MkYvdO5uZzFU2owzY5qlMlYm?= =?us-ascii?Q?bswahsTOyGreNQWZtu0dyXhMv9VUx8hgk/XsBipmOmSHbdZZFgVlluojLvNY?= =?us-ascii?Q?binYJUTmUOUDbQR/mcpwUA3UUS2VvNjlJBsJvEQyXEgFdKYmitnfgs92YDSx?= =?us-ascii?Q?ObvRMwpG9Qw=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SA1PR17MB5365.namprd17.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(366016)(376014)(1800799024); DIR:OUT; SFP:1102 X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?NR3z2wEGDv3muqoNE0Jc1h6s3mUu4Na+8H0kmBNy4J5+laCjsDBKHHhRZrOV?= =?us-ascii?Q?afemXXVqJlaNGV80R/cAsqZ+G4eP7uTasweBzOAnPXVcqisPkMnxBE+DTHlz?= =?us-ascii?Q?XLQP9wtVJVowejj2S3g9VdWCoZqSerxQ2ObVTKo/yuTjqsLC7YXtQARTKcCu?= =?us-ascii?Q?ad5pbcuZvsJY3OAskw8AcK509ntKSEAgTf17RZvICPcxz4JxvuF+N+ZAepye?= =?us-ascii?Q?fWOZkmB7Ip7N+22XJnRjURIJWl2z2nYStb12XOIEq8Ww7/ktOG46uVO/IvUE?= =?us-ascii?Q?Pm98SRxk4+hc2ev31aMvXgHPPn0mu2PAZsrdNxYCUN8GXSWMyM1VhNPKXK4k?= =?us-ascii?Q?FEYS0NCQCPlahJiZ7idT7sGZ9vUL/UV+tfIxMpUdHVK7Q9fBJtkioqyYiVld?= =?us-ascii?Q?2EgqI+NRGkWWleVo61FGzpL8j6TXJiA4g5t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3rCUKTIBRMi0pCl5fcyQadPUZzh63Ax+0GdS9MXalxI/OJYXsiSGcIe?= =?us-ascii?Q?PMsm8dhyxLlTH3WH4MINdLhTlyQRV7Rgh+bsJP6ddR4qvuB+g1cudbORRmqo?= =?us-ascii?Q?I7TNXMG4JVbwqQQnVMKTSefJi3EqjaBVhcoyUrjVRM6K4IuJ3C3/upmkzr42?= =?us-ascii?Q?9Cr3DVuahKsfsH3xR4xDATiKJJDXczPTJTM9nRQC4cupGGqj3pHH8PKdV7Fu?= =?us-ascii?Q?unokAWCkDa4Z0kItDhYXuae2KFgXbSoUHz0Lr9QC?= X-OriginatorOrg: labware.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3a9be817-698c-4359-d03f-08ddb2708669 X-MS-Exchange-CrossTenant-AuthSource: SA1PR17MB5365.namprd17.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Jun 2025 16:10:51.9153 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b5db0322-1aa0-4c0a-859c-ad0f96966f4c X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: R9iExhA7h6+AhKBNnQkeQiao43uZaH1bsyyhZwG6NDHDFaJ4WSSSI8z2nBsy514PbNXqnc6kxn1zLBqPnzH4sw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR17MB4637 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 5LWqz90bwRjgvbC8rOny97KGDcQDet5L9jLD_yvGeL0_1750695053 X-Mimecast-Originator: labware.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=WINDOWS-1252 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org This commit changes internal implementation of struct blockvector to use std::vector<> rather than flexible array. The main motivation for this change is to simplify adding blocks to existing symtab. This feature will be used later by Python API to build objfiles, compunits and symtabs dynamically (similarly to JIT reader API). To do so, this commit 1. introduces obstack_allocator, an implementation of Allocator concept that allocates memory on obstack. 2. uses std::vector<> with the above allocator to hold on blocks 3. updates users. As a side-effect of this change, blockvectors allocated in mdebugread.c are obstack-allocated rather than xzalloc()ated which seems to be the correct thing to do. Also, code got simpler. The downside is higher memory consumption. The size of std::vector with obstack allocator is 32 bytes (GCC 14) compared to 8 bytes used currently to store the number of blocks (m_num_blocks). Stopping gdb at its main(), followed by "maint expand-symtabs" results in 4593 compunit symtabs so in this case the overhead is 24*4593 =3D 110232 bytes which I hope is acceptable. Maybe more concerning is the fact that one may waste obstack memory when excessively adding blocks. However, blockvectors are not added blocks after initial allocation at the moment (except in mdebugread.c) so this is not a problem for existing code. To to mitigate this issue code allocating may capacity - a number of blocks the blockvector may hold without reallocating. --- gdb/block.c | 59 ++++++++++++++++++++++++++++++++++++++++ gdb/block.h | 56 +++++++++++++++++++++++++++----------- gdb/buildsym.c | 6 +--- gdb/jit.c | 8 +----- gdb/mdebugread.c | 32 ++-------------------- gdbsupport/gdb_obstack.h | 52 +++++++++++++++++++++++++++++++++++ 6 files changed, 156 insertions(+), 57 deletions(-) diff --git a/gdb/block.c b/gdb/block.c index 54d768d7255..5e2c121cb3f 100644 --- a/gdb/block.c +++ b/gdb/block.c @@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . = */ =20 +#include #include "block.h" #include "symtab.h" #include "symfile.h" @@ -844,6 +845,64 @@ bool blockvector::maybe_contains (CORE_ADDR addr) cons= t return true; } =20 +static bool +block_ordering_predicate(struct block *b1, struct block *b2) +{ + CORE_ADDR start1 =3D b1->start (); + CORE_ADDR start2 =3D b2->start (); + + if (start1 !=3D start2) + return start1 < start2; + return (b2->end () < b1->end ()); +} + +/* See block.h. */ + +void +blockvector::add_block (struct block *block) +{ + if (num_blocks() <=3D FIRST_LOCAL_BLOCK) + { + /* No blocks (except global and static block). */ + m_blocks.push_back (block); + } + else + { + /* blockvector already contains some blocks. Insert new block + to a correct place. */ + auto first =3D m_blocks.begin () + FIRST_LOCAL_BLOCK; + auto last =3D m_blocks.end (); + + auto insert_before =3D std::upper_bound (first, + last, + block, + block_ordering_predicate); + + m_blocks.insert (insert_before, block); + } +} + +/* See block.h. */ + +void +blockvector::sort () +{ + if (num_blocks() > FIRST_LOCAL_BLOCK) + { + std::sort (blocks ().begin () + FIRST_LOCAL_BLOCK, + blocks ().end (), + block_ordering_predicate); + } +} + +/* See block.h. */ + +struct blockvector * +allocate_blockvector(struct obstack *obstack, int nblocks, int capacity) +{ + return new (obstack) blockvector(obstack, nblocks, capacity); +} + /* Implement 'maint info blocks' command. If passed an argument then print a list of all blocks at the given address. With no arguments then list all blocks at the current address of the current inferior. *= / diff --git a/gdb/block.h b/gdb/block.h index 4a962eff30e..89ceeb4268b 100644 --- a/gdb/block.h +++ b/gdb/block.h @@ -20,6 +20,7 @@ #ifndef GDB_BLOCK_H #define GDB_BLOCK_H =20 +#include #include "dictionary.h" #include "gdbsupport/array-view.h" #include "gdbsupport/next-iterator.h" @@ -416,41 +417,60 @@ struct global_block : public block =20 struct blockvector { + void* operator new (size_t size, struct obstack *obstack) + { + return obstack_alloc (obstack, size); + } + + void* operator new[] (size_t size, struct obstack *obstack) + { + return obstack_alloc (obstack, size); + } + + void operator delete (void *memory) {} + void operator delete[] (void *memory) {} + + blockvector (struct obstack *obstack, int nblocks, int capacity =3D 0) + : m_map (nullptr), + m_blocks (0, nullptr, obstack_allocator (obstack)) + { + m_blocks.reserve (std::max (nblocks, capacity)); + m_blocks.resize (nblocks, nullptr); + } + /* Return a view on the blocks of this blockvector. */ gdb::array_view blocks () { - return gdb::array_view (m_blocks, m_num_blocks); + return gdb::array_view (m_blocks.data (), m_blocks.siz= e ()); } =20 /* Const version of the above. */ gdb::array_view blocks () const { - const struct block **blocks =3D (const struct block **) m_blocks; - return gdb::array_view (blocks, m_num_block= s); + const struct block **blocks =3D (const struct block **) m_blocks.data = (); + return gdb::array_view (blocks, m_blocks.si= ze ()); } =20 /* Return the block at index I. */ struct block *block (size_t i) - { return this->blocks ()[i]; } + { return m_blocks[i]; } =20 /* Const version of the above. */ const struct block *block (size_t i) const - { return this->blocks ()[i]; } + { return m_blocks[i]; } =20 /* Set the block at index I. */ void set_block (int i, struct block *block) { m_blocks[i] =3D block; } =20 - /* Set the number of blocks of this blockvector. - - The storage of blocks is done using a flexible array member, so the n= umber - of blocks set here must agree with what was effectively allocated. *= / - void set_num_blocks (int num_blocks) - { m_num_blocks =3D num_blocks; } + /* Add BLOCK, making sure blocks are ordered by code-addresses + as required. Update global and static block start and end + adresses accordingly. */ + void add_block(struct block *block); =20 /* Return the number of blocks in this blockvector. */ int num_blocks () const - { return m_num_blocks; } + { return m_blocks.size (); } =20 /* Return the global block of this blockvector. */ struct global_block *global_block () @@ -487,19 +507,23 @@ struct blockvector it definitely does not. */ bool maybe_contains (CORE_ADDR addr) const; =20 + void sort (); + private: /* An address map mapping addresses to blocks in this blockvector. This pointer is zero if the blocks' start and end addresses are enough. */ addrmap_fixed *m_map; =20 - /* Number of blocks in the list. */ - int m_num_blocks; - /* The blocks themselves. */ - struct block *m_blocks[1]; + std::vector> m_blocks; }; =20 +/* Allocate new blockvector for NBLOCKS blocks with enough storage to + hold up to CAPACITY blocks. CAPACITY defaults to NBLOCKS. */ +struct blockvector *allocate_blockvector(struct obstack *obstack, +=09=09=09=09=09 int nblocks, int capacity =3D 0); + extern const struct blockvector *blockvector_for_pc (CORE_ADDR, =09=09=09=09=09 const struct block **); =20 diff --git a/gdb/buildsym.c b/gdb/buildsym.c index 6dc079f29b1..a9ad8a997d3 100644 --- a/gdb/buildsym.c +++ b/gdb/buildsym.c @@ -429,10 +429,7 @@ buildsym_compunit::make_blockvector () { } =20 - blockvector =3D (struct blockvector *) - obstack_alloc (&m_objfile->objfile_obstack, -=09=09 (sizeof (struct blockvector) -=09=09 + (i - 1) * sizeof (struct block *))); + blockvector =3D allocate_blockvector(&m_objfile->objfile_obstack, i); =20 /* Copy the blocks into the blockvector. This is done in reverse order, which happens to put the blocks into the proper order @@ -440,7 +437,6 @@ buildsym_compunit::make_blockvector () each block into the list after its subblocks in order to make sure this is true. */ =20 - blockvector->set_num_blocks (i); for (next =3D m_pending_blocks; next; next =3D next->next) blockvector->set_block (--i, next->block); =20 diff --git a/gdb/jit.c b/gdb/jit.c index 1944e8afa22..a2512b9b9a9 100644 --- a/gdb/jit.c +++ b/gdb/jit.c @@ -518,7 +518,6 @@ static void finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile) { struct compunit_symtab *cust; - size_t blockvector_size; CORE_ADDR begin, end; struct blockvector *bv; =20 @@ -553,18 +552,13 @@ finalize_symtab (struct gdb_symtab *stab, struct objf= ile *objfile) filetab->set_linetable (new_table); } =20 - blockvector_size =3D (sizeof (struct blockvector) -=09=09 + (actual_nblocks - 1) * sizeof (struct block *)); - bv =3D (struct blockvector *) obstack_alloc (&objfile->objfile_obstack, -=09=09=09=09=09 blockvector_size); + bv =3D allocate_blockvector(&objfile->objfile_obstack, actual_nblocks); cust->set_blockvector (bv); =20 /* At the end of this function, (begin, end) will contain the PC range t= his entire blockvector spans. */ - bv->set_map (nullptr); begin =3D stab->blocks.front ().begin; end =3D stab->blocks.front ().end; - bv->set_num_blocks (actual_nblocks); =20 /* First run over all the gdb_block objects, creating a real block object for each. Simultaneously, keep setting the real_block diff --git a/gdb/mdebugread.c b/gdb/mdebugread.c index 51e1907c683..25139e8ab1e 100644 --- a/gdb/mdebugread.c +++ b/gdb/mdebugread.c @@ -242,8 +242,6 @@ static struct compunit_symtab *new_symtab (const char *= , int, struct objfile *); =20 static struct linetable *new_linetable (int); =20 -static struct blockvector *new_bvect (int); - static struct type *parse_type (int, union aux_ext *, unsigned int, int *, =09=09=09=09int, const char *); =20 @@ -4502,17 +4500,8 @@ add_block (struct block *b, struct symtab *s) /* Cast away "const", but that's ok because we're building the symtab and blockvector here. */ struct blockvector *bv - =3D (struct blockvector *) s->compunit ()->blockvector (); - - bv =3D (struct blockvector *) xrealloc ((void *) bv, -=09=09=09=09=09(sizeof (struct blockvector) -=09=09=09=09=09 + bv->num_blocks () -=09=09=09=09=09 * sizeof (struct block))); - if (bv !=3D s->compunit ()->blockvector ()) - s->compunit ()->set_blockvector (bv); - - bv->set_block (bv->num_blocks (), b); - bv->set_num_blocks (bv->num_blocks () + 1); + =3D const_cast (s->compunit ()->blockvector ()); + bv->add_block (b); } =20 /* Add a new linenumber entry (LINENO,ADR) to a linevector LT. @@ -4635,7 +4624,7 @@ new_symtab (const char *name, int maxlines, struct ob= jfile *objfile) lang =3D cust->language (); =20 /* All symtabs must have at least two blocks. */ - bv =3D new_bvect (2); + bv =3D allocate_blockvector(&objfile->objfile_obstack, 2); bv->set_block (GLOBAL_BLOCK, new_block (objfile, NON_FUNCTION_BLOCK, lan= g)); bv->set_block (STATIC_BLOCK, new_block (objfile, NON_FUNCTION_BLOCK, lan= g)); bv->static_block ()->set_superblock (bv->global_block ()); @@ -4703,21 +4692,6 @@ shrink_linetable (struct linetable *lt) =09=09=09=09=09 * sizeof (lt->item)))); } =20 -/* Allocate and zero a new blockvector of NBLOCKS blocks. */ - -static struct blockvector * -new_bvect (int nblocks) -{ - struct blockvector *bv; - int size; - - size =3D sizeof (struct blockvector) + nblocks * sizeof (struct block *)= ; - bv =3D (struct blockvector *) xzalloc (size); - bv->set_num_blocks (nblocks); - - return bv; -} - /* Allocate and zero a new block of language LANGUAGE, and set its BLOCK_MULTIDICT. If function is non-zero, assume the block is associated to a function, and make sure that the symbols are stored diff --git a/gdbsupport/gdb_obstack.h b/gdbsupport/gdb_obstack.h index 755b90767aa..2ac7288226f 100644 --- a/gdbsupport/gdb_obstack.h +++ b/gdbsupport/gdb_obstack.h @@ -20,6 +20,8 @@ #ifndef GDBSUPPORT_GDB_OBSTACK_H #define GDBSUPPORT_GDB_OBSTACK_H =20 +#include +#include #include "obstack.h" =20 /* Utility macros - wrap obstack alloc into something more robust. */ @@ -157,4 +159,54 @@ struct allocate_on_obstack void operator delete[] (void *memory) {} }; =20 +/* Implementation of Allocator concept using obstack to + allocate memory. This allows standard containers to be + used with obstack. */ + +template +class obstack_allocator +{ +public: + typedef T value_type; + + obstack_allocator (struct obstack *obstack) + : m_obstack(obstack) + {} + + template constexpr obstack_allocator (const obstack_allocat= or& allocator) noexcept + : m_obstack(allocator.m_obstack) + {} + + T* allocate (std::size_t n) + { + if (n > std::numeric_limits::max () / sizeof (T)) + throw std::bad_array_new_length (); + + if (auto p =3D static_cast (obstack_alloc (m_obstack, n * sizeof (= T)))) + { +=09return p; + } + + throw std::bad_alloc (); + } + + void deallocate(T* p, std::size_t n) noexcept + {} + +private: + + struct obstack *m_obstack; +}; + +template +bool operator=3D=3D(const obstack_allocator &t, const obstack_allocator= &u) +{ + return (std::is_same::value_type) && (t.m_obstack =3D=3D u.m_obsta= ck); +} +template +bool operator!=3D(const obstack_allocator &t, const obstack_allocator &u) +{ + return ! (t =3D=3D u); +} + #endif /* GDBSUPPORT_GDB_OBSTACK_H */ --=20 2.47.2