Files
glibc/string/memmove.c
T

116 lines
3.5 KiB
C
Raw Normal View History

1997-05-26 23:01:17 +00:00
/* Copy memory to memory until the specified number of bytes
1995-02-18 01:27:10 +00:00
has been copied. Overlap is handled correctly.
Copyright (C) 1991-2018 Free Software Foundation, Inc.
2001-07-06 04:58:11 +00:00
This file is part of the GNU C Library.
1995-02-18 01:27:10 +00:00
Contributed by Torbjorn Granlund (tege@sics.se).
1996-12-20 01:39:50 +00:00
The GNU C Library is free software; you can redistribute it and/or
2001-07-06 04:58:11 +00:00
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
1995-02-18 01:27:10 +00:00
1996-12-20 01:39:50 +00:00
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2001-07-06 04:58:11 +00:00
Lesser General Public License for more details.
1995-02-18 01:27:10 +00:00
2001-07-06 04:58:11 +00:00
You should have received a copy of the GNU Lesser General Public
2012-02-09 23:18:22 +00:00
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
1995-02-18 01:27:10 +00:00
#include <string.h>
#include <memcopy.h>
/* All this is so that bcopy.c can #include
this file after defining some things. */
#ifndef a1
#define a1 dest /* First arg is DEST. */
#define a1const
#define a2 src /* Second arg is SRC. */
1997-05-26 23:01:17 +00:00
#define a2const const
1997-09-11 12:09:10 +00:00
#undef memmove
1995-02-18 01:27:10 +00:00
#endif
#if !defined(RETURN) || !defined(rettype)
#define RETURN(s) return (s) /* Return DEST. */
1997-05-26 23:01:17 +00:00
#define rettype void *
1995-02-18 01:27:10 +00:00
#endif
#ifndef MEMMOVE
#define MEMMOVE memmove
#endif
1997-09-11 12:09:10 +00:00
1995-02-18 01:27:10 +00:00
rettype
2013-06-20 19:40:55 -05:00
inhibit_loop_to_libcall
MEMMOVE (a1const void *a1, a2const void *a2, size_t len)
1995-02-18 01:27:10 +00:00
{
unsigned long int dstp = (long int) dest;
unsigned long int srcp = (long int) src;
/* This test makes the forward copying code be used whenever possible.
Reduces the working set. */
if (dstp - srcp >= len) /* *Unsigned* compare! */
{
/* Copy from the beginning to the end. */
2012-11-06 17:47:55 -08:00
#if MEMCPY_OK_FOR_FWD_MEMMOVE
dest = memcpy (dest, src, len);
#else
1995-02-18 01:27:10 +00:00
/* If there not too few bytes to copy, use word copy. */
if (len >= OP_T_THRES)
{
/* Copy just a few bytes to make DSTP aligned. */
len -= (-dstp) % OPSIZ;
BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
/* Copy whole pages from SRCP to DSTP by virtual address
manipulation, as much as possible. */
PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len);
1995-02-18 01:27:10 +00:00
/* Copy from SRCP to DSTP taking advantage of the known
alignment of DSTP. Number of bytes remaining is put
1996-12-20 01:39:50 +00:00
in the third argument, i.e. in LEN. This number may
1995-02-18 01:27:10 +00:00
vary from machine to machine. */
WORD_COPY_FWD (dstp, srcp, len, len);
/* Fall out and copy the tail. */
}
/* There are just a few bytes to copy. Use byte memory operations. */
BYTE_COPY_FWD (dstp, srcp, len);
2012-11-06 17:47:55 -08:00
#endif /* MEMCPY_OK_FOR_FWD_MEMMOVE */
1995-02-18 01:27:10 +00:00
}
else
{
/* Copy from the end to the beginning. */
srcp += len;
dstp += len;
/* If there not too few bytes to copy, use word copy. */
if (len >= OP_T_THRES)
{
/* Copy just a few bytes to make DSTP aligned. */
len -= dstp % OPSIZ;
BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ);
/* Copy from SRCP to DSTP taking advantage of the known
alignment of DSTP. Number of bytes remaining is put
1996-12-20 01:39:50 +00:00
in the third argument, i.e. in LEN. This number may
1995-02-18 01:27:10 +00:00
vary from machine to machine. */
WORD_COPY_BWD (dstp, srcp, len, len);
/* Fall out and copy the tail. */
}
/* There are just a few bytes to copy. Use byte memory operations. */
BYTE_COPY_BWD (dstp, srcp, len);
}
1997-05-26 23:01:17 +00:00
RETURN (dest);
1995-02-18 01:27:10 +00:00
}
2003-04-29 22:49:58 +00:00
#ifndef memmove
libc_hidden_builtin_def (memmove)
#endif