public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Joe Buck <Joe.Buck@synopsys.COM>
To: Richard Guenther <richard.guenther@gmail.com>
Cc: Mark Mielke <mark@mark.mielke.cc>,
	Jan-Benedict Glaw <jbglaw@lug-owl.de>,
	       Joakim Tjernlund <joakim.tjernlund@transmode.se>,
	       "gcc@gcc.gnu.org" <gcc@gcc.gnu.org>
Subject: Re: memcpy(p,p,len)
Date: Sat, 01 May 2010 00:07:00 -0000	[thread overview]
Message-ID: <20100501000658.GA25881@synopsys.com> (raw)
In-Reply-To: <y2g84fc9c001004300829vf5be2234h19c40508fecdabd3@mail.gmail.com>

On Fri, Apr 30, 2010 at 08:29:19AM -0700, Richard Guenther wrote:
> On Fri, Apr 30, 2010 at 5:05 PM, Joe Buck <Joe.Buck@synopsys.com> wrote:
> > On Fri, Apr 30, 2010 at 07:30:33AM -0700, Mark Mielke wrote:
> >> Just a quick comment than Jan-Benedict's opinion is widely shared by the
> >> specification and by the Linux glibc manpage:
> >>
> >> DESCRIPTION
> >>         The  memcpy()  function  copies  n bytes from memory area src to
> >> memory
> >>         area dest.  The memory areas should not overlap.  Use memmove(3)
> >> if the
> >>         memory areas do overlap.
> >>
> >> It doesn't matter if it sometimes works. Sometimes works programs are
> >> sometimes doesn't work programs. :-)
> >
> > The typical memcpy function will fail for overlapping but unequal memory
> > ranges, but will work for src == dst.  Switching to memmove would degrade
> > performance, and that should only be done if there is an actual, rather
> > than a theoretical bug.  Note that for this use, it's not possible (if
> > the program is valid) for the ranges to overlap but be unequal.
> >
> > Another alternative is that instead of using memcpy, a specialized
> > function could be used that has the required property (the glibc
> > memcpy does).
> 
> Note that language semantics come in here as well.  The middle-end
> assumes that when an assignment is not BLKmode that the RHS
> will be read before the lhs will be written.  It does not assume so
> otherwise and the behavior is undefined for overlapping *p and *q
> if you do *p = *q.  Thus it is up to the frontend to emit a call to
> memmove in this case (the C++ frontend got bitten by this and
> was fixed).

If the only possibilities are that p == q, or *p and *q do not overlap,
then
  if (p != q)
     memcpy(p, q, n);
would be cheaper than memmove, which has to choose between forward and
backward copying to handle overlap.  However, some memcpy implementations
(including the one in glibc) will do the right thing even without the
test.

If structure copying suddenly produces memmove calls, that would not
be good.

  reply	other threads:[~2010-05-01  0:07 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-30 14:10 memcpy(p,p,len) Joakim Tjernlund
2010-04-30 14:13 ` memcpy(p,p,len) Jan-Benedict Glaw
2010-04-30 14:19   ` memcpy(p,p,len) Joakim Tjernlund
2010-04-30 14:30     ` memcpy(p,p,len) Jan-Benedict Glaw
2010-04-30 14:32       ` memcpy(p,p,len) Mark Mielke
2010-04-30 15:14         ` memcpy(p,p,len) Joe Buck
2010-04-30 15:52           ` memcpy(p,p,len) Richard Guenther
2010-05-01  0:07             ` Joe Buck [this message]
2010-04-30 14:37     ` memcpy(p,p,len) Paul Koning

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=20100501000658.GA25881@synopsys.com \
    --to=joe.buck@synopsys.com \
    --cc=gcc@gcc.gnu.org \
    --cc=jbglaw@lug-owl.de \
    --cc=joakim.tjernlund@transmode.se \
    --cc=mark@mark.mielke.cc \
    --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).