From: Martin Sebor <msebor@gmail.com>
To: Richard Biener <richard.guenther@gmail.com>
Cc: gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] define auto_vec copy ctor and assignment (PR 90904)
Date: Tue, 27 Apr 2021 09:52:54 -0600 [thread overview]
Message-ID: <91545a73-12af-33b2-c6e7-119b5a21de60@gmail.com> (raw)
In-Reply-To: <CAFiYyc2Pn_cuVVO=jaKV2BVHjT1qD-iSZc3OVNdBeRDG_QCydw@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 2398 bytes --]
On 4/27/21 8:04 AM, Richard Biener wrote:
> On Tue, Apr 27, 2021 at 3:59 PM Martin Sebor <msebor@gmail.com> wrote:
>>
>> On 4/27/21 1:58 AM, Richard Biener wrote:
>>> On Tue, Apr 27, 2021 at 2:46 AM Martin Sebor via Gcc-patches
>>> <gcc-patches@gcc.gnu.org> wrote:
>>>>
>>>> PR 90904 notes that auto_vec is unsafe to copy and assign because
>>>> the class manages its own memory but doesn't define (or delete)
>>>> either special function. Since I first ran into the problem,
>>>> auto_vec has grown a move ctor and move assignment from
>>>> a dynamically-allocated vec but still no copy ctor or copy
>>>> assignment operator.
>>>>
>>>> The attached patch adds the two special functions to auto_vec along
>>>> with a few simple tests. It makes auto_vec safe to use in containers
>>>> that expect copyable and assignable element types and passes bootstrap
>>>> and regression testing on x86_64-linux.
>>>
>>> The question is whether we want such uses to appear since those
>>> can be quite inefficient? Thus the option is to delete those operators?
>>
>> I would strongly prefer the generic vector class to have the properties
>> expected of any other generic container: copyable and assignable. If
>> we also want another vector type with this restriction I suggest to add
>> another "noncopyable" type and make that property explicit in its name.
>> I can submit one in a followup patch if you think we need one.
>
> I'm not sure (and not strictly against the copy and assign). Looking around
> I see that vec<> does not do deep copying. Making auto_vec<> do it
> might be surprising (I added the move capability to match how vec<>
> is used - as "reference" to a vector)
The vec base classes are special: they have no ctors at all (because
of their use in unions). That's something we might have to live with
but it's not a model to follow in ordinary containers.
The auto_vec class was introduced to fill the need for a conventional
sequence container with a ctor and dtor. The missing copy ctor and
assignment operators were an oversight, not a deliberate feature.
This change fixes that oversight.
The revised patch also adds a copy ctor/assignment to the auto_vec
primary template (that's also missing it). In addition, it adds
a new class called auto_vec_ncopy that disables copying and
assignment as you prefer. It also disables copying for
the auto_string_vec class.
Martin
[-- Attachment #2: gcc-90904.diff --]
[-- Type: text/x-patch, Size: 6204 bytes --]
PR middle-end/90904 - vec assignment and copying undefined
gcc/ChangeLog:
PR middle-end/90904
* vec.c (test_copy_assign): New function.
(vec_c_tests): Call it.
* vec.h (vec_assign): New function.
(auto_vec copy ctor): Define.
(auto_vec::operator=): Define.
(auto_vec_no_copy): New class template.
(auto_string_vec): Disable copying/assignment.
diff --git a/gcc/vec.c b/gcc/vec.c
index f9dbb2cac31..f15530d1e43 100644
--- a/gcc/vec.c
+++ b/gcc/vec.c
@@ -317,6 +317,86 @@ test_safe_push ()
ASSERT_EQ (7, v[2]);
}
+
+/* Verify that auto_vec copy ctor and assignment work correctly. */
+
+template <size_t N>
+void test_copy_assign ()
+{
+ typedef auto_vec <int, N> test_vec;
+
+ test_vec a0;
+ test_vec b0 (0);
+ test_vec c0 (7);
+ ASSERT_EQ (0, b0.length ());
+ ASSERT_EQ (0, c0.length ());
+
+ a0 = a0;
+ ASSERT_EQ (0, a0.length ());
+ b0 = a0;
+ ASSERT_EQ (0, b0.length ());
+ c0 = a0;
+ ASSERT_EQ (0, c0.length ());
+
+ test_vec a3;
+ a3.safe_push (5);
+ a3.safe_push (6);
+ a3.safe_push (7);
+
+ test_vec b3 (a3);
+ ASSERT_EQ (3, b3.length ());
+ ASSERT_EQ (5, b3[0]);
+ ASSERT_EQ (6, b3[1]);
+ ASSERT_EQ (7, b3[2]);
+
+ test_vec c3;
+ c3 = b3;
+ ASSERT_EQ (3, c3.length ());
+ ASSERT_EQ (5, c3[0]);
+ ASSERT_EQ (6, c3[1]);
+ ASSERT_EQ (7, c3[2]);
+
+ test_vec d4;
+ d4.safe_push (1);
+ d4.safe_push (2);
+ d4.safe_push (3);
+ d4.safe_push (4);
+
+ c3 = d4;
+ ASSERT_EQ (4, c3.length ());
+ ASSERT_EQ (1, c3[0]);
+ ASSERT_EQ (2, c3[1]);
+ ASSERT_EQ (3, c3[2]);
+ ASSERT_EQ (4, c3[3]);
+
+ d4 = a3;
+ ASSERT_EQ (3, d4.length ());
+ ASSERT_EQ (5, d4[0]);
+ ASSERT_EQ (6, d4[1]);
+ ASSERT_EQ (7, d4[2]);
+
+ a3 = b0;
+ ASSERT_EQ (0, a3.length ());
+
+ b3 = b0;
+ ASSERT_EQ (0, b3.length ());
+
+ c3 = c0;
+ ASSERT_EQ (0, c3.length ());
+
+ b0 = d4;
+ ASSERT_EQ (3, b0.length ());
+ ASSERT_EQ (5, b0[0]);
+ ASSERT_EQ (6, b0[1]);
+ ASSERT_EQ (7, b0[2]);
+
+ c0 = d4;
+ ASSERT_EQ (3, c0.length ());
+ ASSERT_EQ (5, c0[0]);
+ ASSERT_EQ (6, c0[1]);
+ ASSERT_EQ (7, c0[2]);
+}
+
/* Verify that vec::truncate works correctly. */
static void
@@ -549,6 +629,8 @@ vec_c_tests ()
{
test_quick_push ();
test_safe_push ();
+ test_copy_assign<0> ();
+ test_copy_assign<2> ();
test_truncate ();
test_safe_grow_cleared ();
test_pop ();
diff --git a/gcc/vec.h b/gcc/vec.h
index 24df2db0eeb..f5dd5bb329a 100644
--- a/gcc/vec.h
+++ b/gcc/vec.h
@@ -1509,7 +1509,41 @@ public:
want to ask for internal storage for vectors on the stack because if the
size of the vector is larger than the internal storage that space is wasted.
*/
+
template<typename T, size_t N = 0>
+class auto_vec;
+
+/* Safely assign elements from SRC to DST, invoking the copy assignment
+ operator on the initial elements and the copy ctor on the excess. */
+
+template<typename T, size_t N>
+auto_vec<T, N>& vec_assign (auto_vec<T, N> &dst, const auto_vec<T, N> &src)
+{
+ unsigned n0 = dst.length ();
+ const unsigned n1 = src.length ();
+ if (n0 < n1)
+ dst.safe_grow (n1);
+ else
+ {
+ dst.truncate (n1);
+ n0 = n1;
+ }
+
+ T *pdst = dst.address ();
+ const T *psrc = src.address ();
+
+ /* Copy-assign over the first N0 elements. */
+ for (unsigned i = 0; i != n0; ++i)
+ pdst[i] = psrc[i];
+
+ if (n0 < n1)
+ /* Copy-construct elements in excess of N0. */
+ vec_copy_construct (pdst + n0, psrc + n0, n1 - n0);
+
+ return dst;
+}
+
+template<typename T, size_t N /* = 0 */>
class auto_vec : public vec<T, va_heap>
{
public:
@@ -1536,11 +1570,34 @@ public:
this->release ();
}
+ /* Copy ctor. */
+ auto_vec (const auto_vec &);
+
+ /* Copy asssignment. */
+ auto_vec& operator= (const auto_vec &r)
+ {
+ return vec_assign (*this, r);
+ }
+
private:
vec<T, va_heap, vl_embed> m_auto;
T m_data[MAX (N - 1, 1)];
};
+/* Copy elements from R to *THIS. */
+
+template<typename T, size_t N>
+auto_vec<T, N>::auto_vec (const auto_vec& r)
+{
+ const unsigned n = r.length ();
+ this->create (n);
+ if (n)
+ {
+ vec_copy_construct (this->address (), r.address (), n);
+ this->m_vec->m_vecpfx.m_num = n;
+ }
+}
+
/* auto_vec is a sub class of vec whose storage is released when it is
destroyed. */
template<typename T>
@@ -1551,12 +1608,24 @@ public:
auto_vec (size_t n CXX_MEM_STAT_INFO) { this->create (n PASS_MEM_STAT); }
~auto_vec () { this->release (); }
+ /* Copy ctor. */
+ auto_vec (const auto_vec&);
+
+ /* Move ctor from vec that's not auto_vec. */
auto_vec (vec<T, va_heap>&& r)
{
gcc_assert (!r.using_auto_storage ());
this->m_vec = r.m_vec;
r.m_vec = NULL;
}
+
+ /* Copy asssignment. */
+ auto_vec& operator= (const auto_vec &rhs)
+ {
+ return vec_assign (*this, rhs);
+ }
+
+ /* Move asssignment from vec that's not auto_vec. */
auto_vec& operator= (vec<T, va_heap>&& r)
{
gcc_assert (!r.using_auto_storage ());
@@ -1567,6 +1636,35 @@ public:
}
};
+/* Copy elements from R to *THIS. */
+
+template<typename T>
+auto_vec<T, 0>::auto_vec (const auto_vec& r)
+{
+ const unsigned n = r.length ();
+ this->create (n);
+ if (n)
+ {
+ vec_copy_construct (this->address (), r.address (), n);
+ this->m_vec->m_vecpfx.m_num = n;
+ }
+}
+
+/* Same as auto_vec<T, 0> but with deleted copy ctor and assignment
+ operator to detect uses of these special functions in contexts
+ where they may be inefficient. */
+
+template<typename T>
+class auto_vec_no_copy: public auto_vec<T, 0>
+{
+public:
+ /* Inherit all ctors. */
+ using auto_vec<T, 0>::auto_vec;
+
+private:
+ /* Prevent copying and assignment. */
+ DISABLE_COPY_AND_ASSIGN (auto_vec_no_copy);
+};
/* Allocate heap memory for pointer V and create the internal vector
with space for NELEMS elements. If NELEMS is 0, the internal
@@ -1587,7 +1685,11 @@ vec_alloc (vec<T> *&v, unsigned nelems CXX_MEM_STAT_INFO)
class auto_string_vec : public auto_vec <char *>
{
public:
+ auto_string_vec (): auto_vec () { }
~auto_string_vec ();
+
+private:
+ DISABLE_COPY_AND_ASSIGN (auto_string_vec);
};
/* A subclass of auto_vec <T *> that deletes all of its elements on
next prev parent reply other threads:[~2021-04-27 15:52 UTC|newest]
Thread overview: 59+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-26 23:30 Martin Sebor
2021-04-27 7:58 ` Richard Biener
2021-04-27 13:58 ` Martin Sebor
2021-04-27 14:04 ` Richard Biener
2021-04-27 15:52 ` Martin Sebor [this message]
2021-05-03 21:50 ` [PING][PATCH] " Martin Sebor
2021-05-11 20:02 ` [PING 2][PATCH] " Martin Sebor
2021-05-27 19:33 ` [PING 3][PATCH] " Martin Sebor
2021-05-27 20:53 ` [PATCH] " Jason Merrill
2021-06-01 19:56 ` Martin Sebor
2021-06-01 21:38 ` Jason Merrill
2021-06-25 20:51 ` Martin Sebor
2021-06-25 22:11 ` Jason Merrill
2021-06-25 22:36 ` Martin Sebor
2021-06-28 8:07 ` Richard Biener
2021-06-28 18:07 ` Martin Sebor
2021-06-29 10:58 ` Richard Biener
2021-06-29 11:34 ` Martin Jambor
2021-06-30 1:46 ` Martin Sebor
2021-06-30 8:48 ` Richard Biener
2021-06-30 9:29 ` Martin Jambor
2021-07-06 15:06 ` [PING][PATCH] " Martin Sebor
2021-07-07 7:28 ` Richard Biener
2021-07-07 14:37 ` Martin Sebor
2021-07-12 11:02 ` Richard Biener
2021-07-13 14:08 ` Jonathan Wakely
2021-07-13 18:37 ` Jason Merrill
2021-07-13 20:02 ` Martin Sebor
2021-07-14 3:39 ` Jason Merrill
2021-07-14 10:47 ` Jonathan Wakely
2021-07-14 14:46 ` Martin Sebor
2021-07-14 16:23 ` Jason Merrill
2021-07-20 18:34 ` Martin Sebor
2021-07-20 20:08 ` Jason Merrill
2021-07-20 21:52 ` Martin Sebor
2021-07-27 18:56 ` Martin Sebor
2021-07-30 15:06 ` Jason Merrill
2021-08-06 2:07 ` Martin Sebor
2021-08-06 7:52 ` Christophe Lyon
2021-08-06 12:17 ` Christophe Lyon
2021-07-14 14:44 ` Martin Sebor
2021-06-29 14:43 ` [PATCH] " Jason Merrill
2021-06-29 17:18 ` Martin Sebor
2021-06-30 8:40 ` Richard Biener
2021-06-30 9:00 ` Richard Sandiford
2021-06-30 12:01 ` Richard Biener
2021-06-28 8:05 ` Richard Biener
2021-06-29 12:30 ` Trevor Saunders
2021-06-02 6:55 ` Richard Biener
2021-06-02 16:04 ` Martin Sebor
2021-06-03 8:29 ` Trevor Saunders
2021-06-07 8:51 ` Richard Biener
2021-06-07 10:33 ` Trevor Saunders
2021-06-07 13:33 ` Richard Biener
2021-06-07 20:34 ` Martin Sebor
2021-06-08 3:26 ` Trevor Saunders
2021-06-08 7:19 ` Richard Biener
2021-06-07 22:17 ` Martin Sebor
2021-06-08 2:41 ` Trevor Saunders
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=91545a73-12af-33b2-c6e7-119b5a21de60@gmail.com \
--to=msebor@gmail.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=richard.guenther@gmail.com \
/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).