diff options
author | David Malcolm <dmalcolm@redhat.com> | 2020-01-09 01:39:45 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2020-01-09 01:39:45 +0000 |
commit | e4d2203e31e7c017266e61dbf0d24ee85e59fa55 (patch) | |
tree | 280fe13e89a65db7d5867eceacee2ae31f26afda /gcc/vec.h | |
parent | b3de347f3296995eb2a648d6303ab66bf689e104 (diff) | |
download | gcc-e4d2203e31e7c017266e61dbf0d24ee85e59fa55.zip gcc-e4d2203e31e7c017266e61dbf0d24ee85e59fa55.tar.gz gcc-e4d2203e31e7c017266e61dbf0d24ee85e59fa55.tar.bz2 |
vec.h: add auto_delete_vec
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 in the analyzer patch 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.
From-SVN: r280027
Diffstat (limited to 'gcc/vec.h')
-rw-r--r-- | gcc/vec.h | 38 |
1 files changed, 38 insertions, 0 deletions
@@ -1547,6 +1547,31 @@ 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 (); + +private: + DISABLE_COPY_AND_ASSIGN(auto_delete_vec<T>); +}; + /* Conditionally allocate heap memory for VEC and its internal vector. */ template<typename T> @@ -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 <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. */ |