From patchwork Sat Nov 16 01:22:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1195996 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-513720-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="Ne5ClXSe"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="P2HB3vTh"; 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 47FHQl2dlgz9s7T for ; Sat, 16 Nov 2019 12:18:07 +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 :content-type:content-transfer-encoding; q=dns; s=default; b=CJy wcnSJuq8/5EimpFQjeaCOuTSADw3LgwxSEbIjcIDYptGqFaq5ZMoL92x+fyIdVwr rcDsv1POrQOuwQ1obCBCXFj7w6uOE9CxnLKuUqZh/43kvNs3gJCVnkNvzF/u3dT2 ZW4dAVDb4x699BETVobhNavvPrALzanP1T8Ed9/4= 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 :content-type:content-transfer-encoding; s=default; bh=aVCAhKD7G aQPeDKLyLB1modjdqU=; b=Ne5ClXSevpUo0DJuZ+RQz6F8t1v6+MC7DWYg4Qzsv DRfT90ZGoND3a5EW30vRoOLSzQ1hSiGrzmaCBULqsMzZ8KAHmgAEvZeSmxw0Nnb2 iu5USQ16WIJzrQTSwSKiA1IVSA8qN9viseGI5ycVf/NxTKwnsP85jKqCxo8CiRpo CY= Received: (qmail 55263 invoked by alias); 16 Nov 2019 01:17:12 -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 55181 invoked by uid 89); 16 Nov 2019 01:17:11 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-19.7 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=kit, dtor, HX-Languages-Length:2929, crude X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (205.139.110.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 16 Nov 2019 01:17:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573867028; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=loy9WFJc1wRAGrtlmCV6AAR4s3XJr0rd7h7m2vKQ8Bw=; b=P2HB3vTh2QF5Q3l1T+x+TooO0Q/YiRZWr2NfYt2jORFwi5AQTCLygUvKcizRWEx1AFeuro 6GVtEPO5ksEmBbj/Z5RqrEOjuL7NhvX2Bq5lxPoUexbr0mwww6srfJUGDo5MsQLKNGFofg tTz3By7TMeti1Y4Q7q0wFnDcwQ6nn2o= 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-100-9XCEEOzUM-eY-vlZ722SQw-1; Fri, 15 Nov 2019 20:17:07 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8CD32107ACC4 for ; Sat, 16 Nov 2019 01:17:06 +0000 (UTC) Received: from c64.redhat.com (ovpn-112-32.phx2.redhat.com [10.3.112.32]) by smtp.corp.redhat.com (Postfix) with ESMTP id A152B10246FB; Sat, 16 Nov 2019 01:17:05 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 05/49] vec.h: add auto_delete_vec Date: Fri, 15 Nov 2019 20:22:52 -0500 Message-Id: <1573867416-55618-6-git-send-email-dmalcolm@redhat.com> In-Reply-To: <1573867416-55618-1-git-send-email-dmalcolm@redhat.com> References: <1573867416-55618-1-git-send-email-dmalcolm@redhat.com> X-Mimecast-Spam-Score: 0 X-IsSubscribed: yes 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 | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/gcc/vec.c b/gcc/vec.c index bac5eb7..a9c840d 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 091056b..1957739 100644 --- a/gcc/vec.h +++ b/gcc/vec.h @@ -1539,6 +1539,28 @@ 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 (); +}; + /* Conditionally allocate heap memory for VEC and its internal vector. */ template @@ -1643,6 +1665,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. */