public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Martin Sebor <msebor@gmail.com>
To: David Malcolm <dmalcolm@redhat.com>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH 05/49] vec.h: add auto_delete_vec
Date: Wed, 04 Dec 2019 16:29:00 -0000	[thread overview]
Message-ID: <34fe3514-f06f-a44b-b7ae-892acd255a11@gmail.com> (raw)
In-Reply-To: <1573867416-55618-6-git-send-email-dmalcolm@redhat.com>

On 11/15/19 6:22 PM, David Malcolm wrote:
> This patch adds a class auto_delete_vec<T>, a subclass of auto_vec <T *>
> 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<T>::~auto_delete_vec): New dtor.

Because of slicing, unless preventing the elements from being
deleted in the class dtor is meant to be a feature, it seems that
using a wrapper class rather than public derivation from auto_vec
might be a safer solution.

It might be worth mentioning in a comment that the class isn't
safe to copy or assign (each copy would wind up delete the same
pointers), in addition to making its copy ctor and copy assignment
operator inaccessible or deleted.

Martin

> ---
>   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 <count_dtor> 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 <char *>
>     ~auto_string_vec ();
>   };
>   
> +/* A subclass of auto_vec <T *> 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 <typename T>
> +class auto_delete_vec : public auto_vec <T *>
> +{
> + public:
> +  auto_delete_vec () {}
> +  auto_delete_vec (size_t s) : auto_vec <T *> (s) {}
> +
> +  ~auto_delete_vec ();
> +};
> +
>   /* Conditionally allocate heap memory for VEC and its internal vector.  */
>   
>   template<typename T>
> @@ -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 <T*>, which frees the internal buffer.  */
> +
> +template <typename T>
> +inline
> +auto_delete_vec<T>::~auto_delete_vec ()
> +{
> +  int i;
> +  T *item;
> +  FOR_EACH_VEC_ELT (*this, i, item)
> +    delete item;
> +}
> +
>   
>   /* Return a copy of this vector.  */
>   
> 

  reply	other threads:[~2019-12-04 16:29 UTC|newest]

Thread overview: 160+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-16  1:21 [PATCH 00/49] RFC: Add a static analysis framework to GCC David Malcolm
2019-11-16  1:17 ` [PATCH 08/49] Introduce pretty_printer::clone vfunc David Malcolm
2019-12-07 14:36   ` Jeff Law
2019-11-16  1:17 ` [PATCH 14/49] hash-map-tests.c: add a selftest involving int_hash David Malcolm
2019-12-07 14:38   ` Jeff Law
2019-11-16  1:17 ` [PATCH 02/49] analyzer: internal documentation David Malcolm
2019-11-16  1:17 ` [PATCH 11/49] Add diagnostic_metadata and CWE support David Malcolm
2019-12-04 17:36   ` Martin Sebor
2019-12-13  3:00     ` David Malcolm
2019-12-13 16:44       ` Martin Sebor
2019-12-13 17:32         ` David Malcolm
2019-11-16  1:17 ` [PATCH 09/49] gimple const-correctness fixes David Malcolm
2019-12-04 17:06   ` Martin Sebor
2019-12-06 10:52   ` Richard Biener
2019-12-06 17:47     ` David Malcolm
2019-12-09  8:27       ` Richard Biener
2019-12-07 14:28   ` Jeff Law
2019-12-09  8:18     ` Richard Biener
2019-11-16  1:17 ` [PATCH 01/49] analyzer: user-facing documentation David Malcolm
2019-12-06 19:57   ` Eric Gallager
2019-12-07  1:41     ` David Malcolm
2019-12-09  5:36   ` Sandra Loosemore
2019-11-16  1:18 ` [PATCH 26/49] analyzer: new files: digraph.{cc|h} and shortest-paths.h David Malcolm
2019-12-07 14:58   ` Jeff Law
2019-12-09 23:15     ` David Malcolm
2019-12-10  0:45   ` Martin Sebor
2019-11-16  1:18 ` [PATCH 23/49] analyzer: logging support David Malcolm
2019-12-04 18:56   ` Martin Sebor
2019-11-16  1:18 ` [PATCH 03/49] diagnostic_show_locus: move initial newline to callers David Malcolm
2019-12-07 14:30   ` Jeff Law
2019-12-10  1:51     ` David Malcolm
2019-11-16  1:18 ` [PATCH 18/49] Add in-tree plugin: "analyzer" David Malcolm
2019-11-16  1:18 ` [PATCH 06/49] timevar.h: add auto_client_timevar class David Malcolm
2019-12-04 16:39   ` Martin Sebor
2019-12-04 17:28     ` Tom Tromey
2019-12-04 21:15       ` David Malcolm
2019-12-07 14:29   ` Jeff Law
2019-12-08 14:30     ` David Malcolm
2019-11-16  1:18 ` [PATCH 32/49] analyzer: new files: pending-diagnostic.{cc|h} David Malcolm
2019-12-07 15:05   ` Jeff Law
2019-12-08 14:43     ` David Malcolm
2019-12-08 22:25       ` Jeff Law
2019-11-16  1:18 ` [PATCH 24/49] analyzer: new file: analyzer-pass.cc David Malcolm
2019-12-07 14:51   ` Jeff Law
2019-12-08 14:38     ` David Malcolm
2019-11-16  1:18 ` [PATCH 21/49] analyzer: command-line options David Malcolm
2019-12-04 18:35   ` Martin Sebor
2019-12-06 18:14     ` Eric Gallager
2019-11-16  1:18 ` [PATCH 22/49] analyzer: params.def: new parameters David Malcolm
2019-12-07 14:49   ` Jeff Law
2019-12-08  5:42     ` Eric Gallager
2019-12-08 14:34       ` David Malcolm
2019-11-16  1:18 ` [PATCH 19/49] analyzer: new files: analyzer-selftests.{cc|h} David Malcolm
2019-12-07 14:48   ` Jeff Law
2019-11-16  1:18 ` [PATCH 12/49] Add diagnostic paths David Malcolm
2019-12-07 14:45   ` Jeff Law
2019-12-19  2:49     ` David Malcolm
2019-11-16  1:18 ` [PATCH 30/49] analyzer: new files: constraint-manager.{cc|h} David Malcolm
2019-11-16  1:18 ` [PATCH 29/49] analyzer: new files: tristate.{cc|h} David Malcolm
2019-12-07 15:03   ` Jeff Law
2019-12-09 23:16     ` David Malcolm
2019-12-10  0:59   ` Martin Sebor
2019-11-16  1:18 ` [PATCH 17/49] Support for adding selftests via a plugin David Malcolm
2019-12-07 14:41   ` Jeff Law
2019-12-09 23:18     ` David Malcolm
2019-11-16  1:18 ` [PATCH 28/49] analyzer: new files: analyzer.{cc|h} David Malcolm
2019-12-07  3:38   ` Eric Gallager
2019-12-09 23:22     ` David Malcolm
2019-12-10 19:59       ` Eric Gallager
2019-12-07 15:02   ` Jeff Law
2019-12-11 19:49     ` David Malcolm
2019-12-20  1:21       ` [PATCH 0/4] analyzer: add class function_set and use in various places David Malcolm
2019-12-20  1:22         ` [PATCH 3/4] analyzer: add known stdio functions to sm-file.cc (PR analyzer/58237) David Malcolm
2019-12-20  1:22         ` [PATCH 2/4] analyzer: introduce a set of known async-signal-unsafe functions David Malcolm
2019-12-20  1:22         ` [PATCH 1/4] analyzer: add function-set.cc/h David Malcolm
2019-12-20  6:34         ` [PATCH 4/4] analyzer: add -Wanalyzer-use-of-closed-file David Malcolm
2019-12-10 17:17   ` [PATCH 28/49] analyzer: new files: analyzer.{cc|h} Martin Sebor
2019-11-16  1:18 ` [PATCH 36/49] analyzer: new file: sm-pattern-test.cc David Malcolm
2019-12-07 15:16   ` Jeff Law
2019-11-16  1:18 ` [PATCH 16/49] Add support for in-tree plugins David Malcolm
2019-12-06 22:41   ` Eric Gallager
2019-12-07 14:39   ` Jeff Law
2019-12-08 14:32     ` David Malcolm
2019-11-16  1:18 ` [PATCH 33/49] analyzer: new files: sm.{cc|h} David Malcolm
2019-12-11 19:24   ` Jeff Law
2020-01-10  2:28     ` David Malcolm
2019-11-16  1:18 ` [PATCH 05/49] vec.h: add auto_delete_vec David Malcolm
2019-12-04 16:29   ` Martin Sebor [this message]
2019-12-18 16:00     ` David Malcolm
2020-01-08 21:52       ` Jeff Law
2019-11-16  1:18 ` [PATCH 35/49] analyzer: new file: sm-file.cc David Malcolm
2019-12-07 15:15   ` Jeff Law
2019-11-16  1:18 ` [PATCH 07/49] Replace label_text ctor with "borrow" and "take" David Malcolm
2019-12-07 14:34   ` Jeff Law
2019-11-16  1:18 ` [PATCH 27/49] analyzer: new files: supergraph.{cc|h} David Malcolm
2019-11-16  1:20 ` [PATCH 04/49] sbitmap.h: add operator const sbitmap & to auto_sbitmap David Malcolm
2019-12-04 16:54   ` Martin Sebor
2019-11-16  1:20 ` [PATCH 39/49] analyzer: new files: analysis-plan.{cc|h} David Malcolm
2019-12-07 15:24   ` Jeff Law
2019-11-16  1:20 ` [PATCH 34/49] analyzer: new file: sm-malloc.cc David Malcolm
2019-12-07 15:11   ` Jeff Law
2019-11-16  1:21 ` [PATCH 47/49] analyzer: new files: diagnostic-manager.{cc|h} David Malcolm
2019-12-11 20:42   ` Jeff Law
2019-12-11 21:11     ` David Malcolm
2019-11-16  1:21 ` [PATCH 44/49] analyzer: new files: state-purge.{cc|h} David Malcolm
2019-12-11 20:11   ` Jeff Law
2019-11-16  1:21 ` [PATCH 43/49] analyzer: new file: exploded-graph.h David Malcolm
2019-12-11 20:04   ` Jeff Law
2019-12-12 15:29     ` David Malcolm
2019-12-12 18:22       ` David Malcolm
2020-01-10  2:41     ` David Malcolm
2019-11-16  1:21 ` [PATCH 40/49] analyzer: new files: call-string.{cc|h} David Malcolm
2019-12-11 19:28   ` Jeff Law
2019-11-16  1:21 ` [PATCH 10/49] Add -fdiagnostics-nn-line-numbers David Malcolm
2019-12-04 17:18   ` Martin Sebor
2019-12-04 17:24   ` Martin Sebor
2019-11-16  1:21 ` [PATCH 49/49] analyzer: test suite David Malcolm
2019-11-16  1:21 ` [PATCH 31/49] analyzer: new files: region-model.{cc|h} David Malcolm
2019-11-16  1:21 ` [PATCH 41/49] analyzer: new files: program-point.{cc|h} David Malcolm
2019-12-11 19:54   ` Jeff Law
2019-12-11 19:58     ` David Malcolm
2019-11-16  1:21 ` [PATCH 46/49] analyzer: new files: checker-path.{cc|h} David Malcolm
2019-12-11 20:50   ` Jeff Law
2019-11-16  1:21 ` [PATCH 38/49] analyzer: new file: sm-taint.cc David Malcolm
2019-12-07 15:22   ` Jeff Law
2019-11-16  1:21 ` [PATCH 37/49] analyzer: new file: sm-sensitive.cc David Malcolm
2019-12-07 15:18   ` Jeff Law
2019-11-16  1:21 ` [PATCH 13/49] function-tests.c: expose selftest::make_fndecl for use elsewhere David Malcolm
2019-12-07 14:37   ` Jeff Law
2019-11-16  1:21 ` [PATCH 15/49] Add ordered_hash_map David Malcolm
2019-12-04 17:59   ` Martin Sebor
2020-01-08 23:47     ` David Malcolm
2020-01-09 11:56       ` Jonathan Wakely
2020-01-10  2:58         ` [PATCH] Add ordered_hash_map (v6) David Malcolm
2019-11-16  1:21 ` [PATCH 48/49] gdbinit.in: add break-on-saved-diagnostic David Malcolm
2019-11-16  1:21 ` [PATCH 25/49] analyzer: new files: graphviz.{cc|h} David Malcolm
2019-12-07 14:54   ` Jeff Law
2019-12-09 23:13     ` David Malcolm
2019-11-16  1:21 ` [PATCH 45/49] analyzer: new files: engine.{cc|h} David Malcolm
2019-11-16  1:21 ` [PATCH 42/49] analyzer: new files: program-state.{cc|h} David Malcolm
2019-11-16  1:23 ` [PATCH 20/49] analyzer: new builtins David Malcolm
2019-12-04 18:11   ` Martin Sebor
2019-12-19 15:16     ` David Malcolm
2019-11-16 21:47 ` [PATCH 00/49] RFC: Add a static analysis framework to GCC Thomas Schwinge
2019-11-19 22:05   ` David Malcolm
2019-11-20 10:26     ` Richard Biener
2019-12-02  3:03       ` Eric Gallager
2019-12-03 16:52       ` David Malcolm
2019-12-03 17:17         ` Jakub Jelinek
2019-12-16 14:33           ` David Malcolm
2019-12-04 12:41         ` Richard Biener
2019-12-06 22:25         ` Jeff Law
2019-12-08 14:28           ` [PATCH 0/2] v3 of analyzer patch kit (unsquashed) David Malcolm
2019-12-08 14:28             ` [PATCH 1/2] Fixup for rebase: c-format.c: get_pointer_to_named_type -> get_named_type David Malcolm
2019-12-08 14:28             ` [PATCH 2/2] Rework as a non-plugin David Malcolm
2019-12-02  3:20   ` [PATCH 00/49] RFC: Add a static analysis framework to GCC Eric Gallager
2019-12-04 19:55 ` Martin Sebor
2019-12-06 22:31   ` Jeff Law
2019-12-09  8:10     ` Richard Biener
2019-12-11 20:11       ` David Malcolm

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=34fe3514-f06f-a44b-b7ae-892acd255a11@gmail.com \
    --to=msebor@gmail.com \
    --cc=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).