From patchwork Fri Dec 13 18:10:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1209335 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-515921-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="Zdxkoien"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="dHObT+IO"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47ZJfy1xjbz9sPJ for ; Sat, 14 Dec 2019 05:12:42 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; q=dns; s= default; b=S+BKc2I1ZlQ5HujixZTH35M2t9ToWhrdqd8Pl7dTEQDAzg8B2VDKd toGSDxjONZdGiwHHcPDaCzm8Tko5KV72XmKfxOnv4DcFC6GZJwIfOiGHK0mIMyLy omBP5GEkB3YalMsT8pO3+0DLOKl5sdBtTtpFk88VOswmQGJ2foJQwc= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; s=default; bh=QwLcWF/YHcLpk/C2HHV0/YhjMpQ=; b=Zdxkoien/aGoOV8GbC7/zB1M9dkP 1eqAZ02YVgGYm7xBN8wWjZ1//Vn77kZ3ai6NdlBF6JBCbfrbl5U7IcZASthCFmC1 OADiphl+CydtU6O/zDUHthVeOWR0JJYE1gsVzD/Z8ltZbrxXFTRheRluBDpvuv5N A0AOZSZ6LiCNsAk= Received: (qmail 99594 invoked by alias); 13 Dec 2019 18:11:48 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 99462 invoked by uid 89); 13 Dec 2019 18:11:47 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3 autolearn=ham version=3.3.1 spammy=HX-Languages-Length:3056, crude X-HELO: us-smtp-delivery-1.mimecast.com Received: from us-smtp-1.mimecast.com (HELO us-smtp-delivery-1.mimecast.com) (205.139.110.61) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 13 Dec 2019 18:11:45 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1576260704; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IcInrx0rPkldHwtTT6ShHhFWR0FMfC9GI6sgsubE2z4=; b=dHObT+IO9Xj/DD/APtUQ1v29b658jhQFw7IgacQa2ViyrV/W49btUe1kZEaID0uf74aOgQ fYzgjN5g+lnDfQ9wuugzPV7LQ0E4n8m4eazVlFkKNX/V/prYEYEsPVkz+E8OvSdAftvLDq myPhZVRFFMUnBj2o8/yldOC9jRg+42Y= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-170-rnVOUJIiM0S1BezBZeUYNA-1; Fri, 13 Dec 2019 13:11:42 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B0D7F107ACC9 for ; Fri, 13 Dec 2019 18:11:41 +0000 (UTC) Received: from t470.redhat.com (ovpn-117-164.phx2.redhat.com [10.3.117.164]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4DDCD5C241; Fri, 13 Dec 2019 18:11:41 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 07/45] vec.h: add auto_delete_vec Date: Fri, 13 Dec 2019 13:10:56 -0500 Message-Id: <20191213181134.1830-8-dmalcolm@redhat.com> In-Reply-To: <20191213181134.1830-1-dmalcolm@redhat.com> References: <20191213181134.1830-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-IsSubscribed: yes Changed in v4: added DISABLE_COPY_AND_ASSIGN This patch adds a class auto_delete_vec, a subclass of auto_vec that deletes all of its elements on destruction; it's used in many places later in the kit. This is a crude way for a vec to "own" the objects it points to and clean up automatically (essentially a workaround for not being able to use unique_ptr, due to C++98). gcc/ChangeLog: * vec.c (class selftest::count_dtor): New class. (selftest::test_auto_delete_vec): New test. (selftest::vec_c_tests): Call it. * vec.h (class auto_delete_vec): New class template. (auto_delete_vec::~auto_delete_vec): New dtor. --- gcc/vec.c | 27 +++++++++++++++++++++++++++ gcc/vec.h | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/gcc/vec.c b/gcc/vec.c index bac5eb753a3c..a9c840de00a0 100644 --- a/gcc/vec.c +++ b/gcc/vec.c @@ -516,6 +516,32 @@ test_reverse () } } +/* A test class that increments a counter every time its dtor is called. */ + +class count_dtor +{ + public: + count_dtor (int *counter) : m_counter (counter) {} + ~count_dtor () { (*m_counter)++; } + + private: + int *m_counter; +}; + +/* Verify that auto_delete_vec deletes the elements within it. */ + +static void +test_auto_delete_vec () +{ + int dtor_count = 0; + { + auto_delete_vec v; + v.safe_push (new count_dtor (&dtor_count)); + v.safe_push (new count_dtor (&dtor_count)); + } + ASSERT_EQ (dtor_count, 2); +} + /* Run all of the selftests within this file. */ void @@ -533,6 +559,7 @@ vec_c_tests () test_block_remove (); test_qsort (); test_reverse (); + test_auto_delete_vec (); } } // namespace selftest diff --git a/gcc/vec.h b/gcc/vec.h index 8070021223a6..cebc16ec68c4 100644 --- a/gcc/vec.h +++ b/gcc/vec.h @@ -1547,6 +1547,31 @@ class auto_string_vec : public auto_vec ~auto_string_vec (); }; +/* A subclass of auto_vec that deletes all of its elements on + destruction. + + This is a crude way for a vec to "own" the objects it points to + and clean up automatically. + + For example, no attempt is made to delete elements when an item + within the vec is overwritten. + + We can't rely on gnu::unique_ptr within a container, + since we can't rely on move semantics in C++98. */ + +template +class auto_delete_vec : public auto_vec +{ + public: + auto_delete_vec () {} + auto_delete_vec (size_t s) : auto_vec (s) {} + + ~auto_delete_vec (); + +private: + DISABLE_COPY_AND_ASSIGN(auto_delete_vec); +}; + /* Conditionally allocate heap memory for VEC and its internal vector. */ template @@ -1651,6 +1676,19 @@ auto_string_vec::~auto_string_vec () free (str); } +/* auto_delete_vec's dtor, deleting all contained items, automatically + chaining up to ~auto_vec , which frees the internal buffer. */ + +template +inline +auto_delete_vec::~auto_delete_vec () +{ + int i; + T *item; + FOR_EACH_VEC_ELT (*this, i, item) + delete item; +} + /* Return a copy of this vector. */