From: Pedro Alves <palves@redhat.com>
To: Simon Marchi <simon.marchi@polymtl.ca>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH 2/5] Don't memcpy non-trivially-copyable types: Make enum_flags triv. copyable
Date: Tue, 25 Apr 2017 01:10:00 -0000 [thread overview]
Message-ID: <add31750-8d80-f319-70ef-6def584a465c@redhat.com> (raw)
In-Reply-To: <feb7882cf12daaeea5f136b7200a0797@polymtl.ca>
On 04/20/2017 04:34 AM, Simon Marchi wrote:
> On 2017-04-12 22:27, Pedro Alves wrote:
>> + /* Define copy/move ctor/op= as defaulted so that enum_flags is
>> + trivially copyable. */
>> + enum_flags (const enum_flags &other) = default;
>> + enum_flags (enum_flags &&) noexcept = default;
>> + enum_flags &operator= (const enum_flags &other) = default;
>> + enum_flags &operator= (enum_flags &&) = default;
>
> What's the difference between defining these as default, and not
> defining them at all (which I assume will still make the compiler emit
> the default versions)?
Yeah, there's no difference. I'll just remove them, since it clearly
confuses things otherwise. Thanks.
> Do you want to add a static_assert(is_pod...) for that class? For now
> we have the VECs that will warn us if it becomes non-POD, but eventually
> they'll be gone.
I do, but enum_flags is a template, and we need to add the assertion to
some template instantiation. I'd like to add it in the enum
flags unit tests file, currently here [1]:
https://github.com/palves/gdb/blob/palves/cxx11-enum-flags/gdb/enum-flags-selftests.c
which I need to rebase and repost. I should be reposting that soon.
[1] - I'll move it to unittests/
Thanks,
Pedro Alves
From 23bcc18f470ee4364bd362a8b78c6c1415a9dadb Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>
Date: Tue, 25 Apr 2017 01:27:42 +0100
Subject: [PATCH] Don't memcpy non-trivially-copyable types: Make enum_flags
triv. copyable
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
The delete-memcpy-with-non-trivial-types patch exposed many instances
of this problem:
src/gdb/btrace.h: In function âbtrace_insn_s* VEC_btrace_insn_s_quick_insert(VEC_btrace_insn_s*, unsigned int, const btrace_insn_s*, const char*, unsigned int)â:
src/gdb/common/vec.h:948:62: error: use of deleted function âvoid* memmove(T*, const U*, size_t) [with T = btrace_insn; U = btrace_insn; <template-parameter-1-3> = void; size_t = long unsigned int]â
memmove (slot_ + 1, slot_, (vec_->num++ - ix_) * sizeof (T)); \
^
src/gdb/common/vec.h:436:1: note: in expansion of macro âDEF_VEC_FUNC_Oâ
DEF_VEC_FUNC_O(T) \
^
src/gdb/btrace.h:84:1: note: in expansion of macro âDEF_VEC_Oâ
DEF_VEC_O (btrace_insn_s);
^
[...]
src/gdb/common/vec.h:1060:31: error: use of deleted function âvoid* memcpy(T*, const U*, size_t) [with T = btrace_insn; U = btrace_insn; <template-parameter-1-3> = void; size_t = long unsigned int]â
sizeof (T) * vec2_->num); \
^
src/gdb/common/vec.h:437:1: note: in expansion of macro âDEF_VEC_ALLOC_FUNC_Oâ
DEF_VEC_ALLOC_FUNC_O(T) \
^
src/gdb/btrace.h:84:1: note: in expansion of macro âDEF_VEC_Oâ
DEF_VEC_O (btrace_insn_s);
^
So, VECs (given it's C roots) rely on memcpy/memcpy of VEC elements to
be well defined, in order to grow/reallocate its internal elements
array. This means that we can only put trivially copyable types in
VECs. E.g., if a type requires using a custom copy/move ctor to
relocate, then we can't put it in a VEC (so we use std::vector
instead). But, as shown above, we're violating that requirement.
btrace_insn is currently not trivially copyable, because it contains
an enum_flags field, and that is itself not trivially copyable. This
patch corrects that, by simply removing the user-provided copy
constructor and assignment operator. The compiler-generated versions
work just fine.
Note that std::vector relies on std::is_trivially_copyable too to know
whether it can reallocate its elements with memcpy/memmove instead of
having to call copy/move ctors and dtors, so if we have types in
std::vectors that weren't trivially copyable because of enum_flags,
this will make such vectors more efficient.
gdb/ChangeLog:
2017-04-25 Pedro Alves <palves@redhat.com>
* common/enum-flags.h (enum_flags): Don't implement copy ctor and
assignment operator.
---
gdb/ChangeLog | 5 +++++
gdb/common/enum-flags.h | 10 ----------
2 files changed, 5 insertions(+), 10 deletions(-)
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 33dd53b..f5419db 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2017-04-25 Pedro Alves <palves@redhat.com>
+
+ * common/enum-flags.h (enum_flags): Don't implement copy ctor and
+ assignment operator.
+
2017-04-24 Yao Qi <yao.qi@linaro.org>
* doublest.c (convert_doublest_to_floatformat): Call
diff --git a/gdb/common/enum-flags.h b/gdb/common/enum-flags.h
index e63c8a4..ddfcddf 100644
--- a/gdb/common/enum-flags.h
+++ b/gdb/common/enum-flags.h
@@ -120,16 +120,6 @@ public:
: m_enum_value ((enum_type) 0)
{}
- enum_flags (const enum_flags &other)
- : m_enum_value (other.m_enum_value)
- {}
-
- enum_flags &operator= (const enum_flags &other)
- {
- m_enum_value = other.m_enum_value;
- return *this;
- }
-
/* If you get an error saying these two overloads are ambiguous,
then you tried to mix values of different enum types. */
enum_flags (enum_type e)
--
2.5.5
next prev parent reply other threads:[~2017-04-25 1:10 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-13 2:27 [PATCH 0/4] Poison non-POD memset & non-trivially-copyable memcpy/memmove Pedro Alves
2017-04-13 2:28 ` [PATCH 2/5] Don't memcpy non-trivially-copyable types: Make enum_flags triv. copyable Pedro Alves
2017-04-20 3:34 ` Simon Marchi
2017-04-25 1:10 ` Pedro Alves [this message]
2017-04-13 2:28 ` [PATCH 1/5] Poison non-POD memset & non-trivially-copyable memcpy/memmove Pedro Alves
2017-04-20 3:27 ` Simon Marchi
2017-04-25 1:14 ` Pedro Alves
2017-04-25 1:19 ` Pedro Alves
2017-04-25 8:24 ` Yao Qi
2017-04-25 9:24 ` Pedro Alves
2017-04-25 10:02 ` Pedro Alves
2017-04-24 1:12 ` Simon Marchi
2017-04-24 1:53 ` Simon Marchi
2017-04-27 13:58 ` Pedro Alves
2017-04-30 1:51 ` Simon Marchi
2017-05-17 11:35 ` Pedro Alves
2017-05-17 13:11 ` Simon Marchi
2017-05-17 13:20 ` Pedro Alves
2017-04-27 13:57 ` Pedro Alves
2017-04-13 2:28 ` [PATCH 4/5] Don't memset non-POD types: struct btrace_insn Pedro Alves
2017-04-13 7:57 ` Metzger, Markus T
2017-04-25 1:11 ` Pedro Alves
2017-04-13 2:28 ` [PATCH 3/5] Don't memset non-POD types: struct bp_location Pedro Alves
2017-04-13 2:35 ` [PATCH 5/5] Don't memset non-POD types: struct breakpoint Pedro Alves
2017-04-20 4:00 ` Simon Marchi
2017-04-25 1:11 ` Pedro Alves
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=add31750-8d80-f319-70ef-6def584a465c@redhat.com \
--to=palves@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=simon.marchi@polymtl.ca \
/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