From patchwork Wed Mar 20 20:23:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 87425 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C039B385829D for ; Wed, 20 Mar 2024 20:24:35 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2d.google.com (mail-io1-xd2d.google.com [IPv6:2607:f8b0:4864:20::d2d]) by sourceware.org (Postfix) with ESMTPS id BFE8A3858D28 for ; Wed, 20 Mar 2024 20:24:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BFE8A3858D28 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BFE8A3858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d2d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710966243; cv=none; b=QT4vJDGlAmlbI713hVjmzAzF6izjZsG0ser2wmkuO5fZ0LPzO4ljyvZz8AV+55nrbyPKflXwWYEZjWP28hhe8EItdewnyflTV4tR4lKXnJFo1P8uq+Dujiv5wS7+BZ6HI/IvOVvCxGvMd3T/ba8g/uTKQst9xzp8aVJZ7uE86os= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710966243; c=relaxed/simple; bh=PCAZQtBdJmgVzUMYStIcnCAnJUQwjMbP9bNujGF4tYE=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=EklSVHcWVigLrpSUvIeaX3gVdMT7l08J89GU2+qDUrNznIMpM14ZaBeBygBlm1kVhZ8XvJ0REvoBgEjh0vAjP9uzOpzcN+NHtvwuO0VSXrvCI1aVz95WtQN/2MJcgUmjM75J9EaDHxy5MwOnOVor4ohUilJEQMtIkn5sXtowMW8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd2d.google.com with SMTP id ca18e2360f4ac-7cc77e74b5cso6941539f.2 for ; Wed, 20 Mar 2024 13:24:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1710966241; x=1711571041; darn=sourceware.org; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=QJvR9ecuRMvVH3ucwkwjYQ/Nhusk9wgGvkSC2APggxA=; b=XaWH0QkDB5Xnby78YuYVCfJRLWHINIv7OAvuZelMlbBcPVyZ/wp9Y8XjDdOZbv8dMF t2B5wJx1/V5i/TvJ7jTG4ygtJPeINeu/2ewADn4R1kacOQOZ/PUQ2BR2dTeGmeqfaXlH 6fr+KehO9O57Dw7twQPGBuPNhvHEUa2jurW7GscQ7wEk3ZMfR92R08x9Y57exV4/X5NG KYaWNgGi65g9H69tP0Ine9PgAP0zwHpQMZrCQ8Z7TBnfMiILdP8OjCWOwTXYuFgt6lz/ oqaF2mtOTkc4nEoeFplDjOy1jepVVHjeDUCVM38MxR8mtw0erXUBigBugs79UcYCHcBj vl/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710966241; x=1711571041; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QJvR9ecuRMvVH3ucwkwjYQ/Nhusk9wgGvkSC2APggxA=; b=okQ8qlOQSi+bfKyrrQ+4GJok4Njz5ZY6HIGO6UVUnpkTsAklXsRaUqAICsRiHxWxsF E4ySjG9SQq19Nr2P4S3oZqoA3bYiSg60HMP9s/ac4prh+cyHKx+fAVoHARR62rIwodCa EKVH9QTjU1lcBl3YLt5kyiyvpIZotmS+m2QgYyijgJzLj5XkrJCo33meZI4fnXogVJcb cGU4x6ctoBTFnx+a+zXBBvHjIjAFhS+Mkfz2gse39voU/usJeI2zGF8SUc26q2Caf9lr 6z74XD7E1zudDhj96MhBCAxE4OSw+8w1maEnApbC1GZhbtOD6FizSwHQFpIJA6dS/KyG MyvA== X-Gm-Message-State: AOJu0YxVk3iGbJumruOanFZK97/COmwyqSQ1Es3X6U8m5D59qkPJsNdl ke/YOfw3jFiAZLTdIlFSuxrEy3QBtbz1Uo2R9PX3ZvtTqs3PjjfR1K0mwjAS5cuO1BANNy4VX/Y = X-Google-Smtp-Source: AGHT+IHOyZcWHJ9MGFyA9/jCNZiRpwHuJ4Q+Ab+lFJMAL+r/OnjCDBBx1xkZEywHeV0Trt/obFOrOA== X-Received: by 2002:a6b:c985:0:b0:7cc:25b:55e9 with SMTP id z127-20020a6bc985000000b007cc025b55e9mr16290559iof.13.1710966240972; Wed, 20 Mar 2024 13:24:00 -0700 (PDT) Received: from localhost.localdomain (97-122-82-115.hlrn.qwest.net. [97.122.82.115]) by smtp.gmail.com with ESMTPSA id eq4-20020a0566384e2400b00474f719b8c3sm3708441jab.33.2024.03.20.13.24.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 13:24:00 -0700 (PDT) From: Tom Tromey Date: Wed, 20 Mar 2024 14:23:59 -0600 Subject: [PATCH 2/3] Make bcache more type-safe MIME-Version: 1.0 Message-Id: <20240320-bcache-type-v1-2-fe616105e9ae@adacore.com> References: <20240320-bcache-type-v1-0-fe616105e9ae@adacore.com> In-Reply-To: <20240320-bcache-type-v1-0-fe616105e9ae@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.4 X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org 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+patchwork=sourceware.org@sourceware.org The bcache uses memcpy to make copies of the data passed to it. In C++, this is only safe for trivially-copyable types. This patch changes bcache to require this property, and slightly changes the API to make it easier to use when copying a single object. It also makes the new 'insert' template methods return the correct type. --- gdb/bcache.h | 25 ++++++++++++++++++++++++- gdb/gdbtypes.c | 2 +- gdb/macrotab.c | 12 ++++++------ gdb/psymtab.c | 6 ++---- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/gdb/bcache.h b/gdb/bcache.h index 17a09880d99..a77cd005ea6 100644 --- a/gdb/bcache.h +++ b/gdb/bcache.h @@ -152,7 +152,26 @@ struct bcache were newly added to the cache, or to false if the bytes were found in the cache. */ - const void *insert (const void *addr, int length, bool *added = nullptr); + template>> + const T *insert (const T *addr, int length, bool *added = nullptr) + { + return (const T *) this->insert ((const void *) addr, length, added); + } + + /* Find a copy of OBJECT in this bcache. If BCACHE has never seen + those bytes before, add a copy of them to BCACHE. In either + case, return a pointer to BCACHE's copy of that string. Since + the cached value is meant to be read-only, return a const buffer. + If ADDED is not NULL, set *ADDED to true if the bytes were newly + added to the cache, or to false if the bytes were found in the + cache. */ + + template>> + const T *insert (const T &object, bool *added = nullptr) + { + return (const T *) this->insert ((const void *) &object, sizeof (object), + added); + } /* Print statistics on this bcache's memory usage and efficacity at eliminating duplication. TYPE should be a string describing the @@ -173,6 +192,10 @@ struct bcache private: + /* Implementation of the templated 'insert' methods. */ + + const void *insert (const void *addr, int length, bool *added); + /* All the bstrings are allocated here. */ struct obstack m_cache {}; diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c index 960a7f49e45..4fcfbc587f6 100644 --- a/gdb/gdbtypes.c +++ b/gdb/gdbtypes.c @@ -4373,7 +4373,7 @@ check_types_worklist (std::vector *worklist, /* If the type pair has already been visited, we know it is ok. */ - cache->insert (&entry, sizeof (entry), &added); + cache->insert (entry, &added); if (!added) continue; diff --git a/gdb/macrotab.c b/gdb/macrotab.c index 5329d2a5a93..a9c425b63c9 100644 --- a/gdb/macrotab.c +++ b/gdb/macrotab.c @@ -110,8 +110,9 @@ macro_free (void *object, struct macro_table *t) /* If the macro table T has a bcache, then cache the LEN bytes at ADDR there, and return the cached copy. Otherwise, just xmalloc a copy of the bytes, and return a pointer to that. */ -static const void * -macro_bcache (struct macro_table *t, const void *addr, int len) +template +static const U * +macro_bcache (struct macro_table *t, const U *addr, int len) { if (t->bcache) return t->bcache->insert (addr, len); @@ -120,7 +121,7 @@ macro_bcache (struct macro_table *t, const void *addr, int len) void *copy = xmalloc (len); memcpy (copy, addr, len); - return copy; + return (const U *) copy; } } @@ -131,7 +132,7 @@ macro_bcache (struct macro_table *t, const void *addr, int len) static const char * macro_bcache_str (struct macro_table *t, const char *s) { - return (const char *) macro_bcache (t, s, strlen (s) + 1); + return macro_bcache (t, s, strlen (s) + 1); } @@ -572,8 +573,7 @@ new_macro_definition (struct macro_table *t, cached_argv[i] = macro_bcache_str (t, argv[i]); /* Now bcache the array of argument pointers itself. */ - d->argv = ((const char * const *) - macro_bcache (t, cached_argv, cached_argv_size)); + d->argv = macro_bcache (t, cached_argv, cached_argv_size); } /* We don't bcache the entire definition structure because it's got diff --git a/gdb/psymtab.c b/gdb/psymtab.c index f1aabd5b10b..ca638515d61 100644 --- a/gdb/psymtab.c +++ b/gdb/psymtab.c @@ -1062,10 +1062,8 @@ partial_symtab::add_psymbol (const partial_symbol &psymbol, bool added; /* Stash the partial symbol away in the cache. */ - partial_symbol *psym - = ((struct partial_symbol *) - partial_symtabs->psymbol_cache.insert - (&psymbol, sizeof (struct partial_symbol), &added)); + const partial_symbol *psym = partial_symtabs->psymbol_cache.insert (psymbol, + &added); /* Do not duplicate global partial symbols. */ if (where == psymbol_placement::GLOBAL && !added)