1 /* Copy memory to memory until the specified number of bytes
2    has been copied.  Overlap is handled correctly.
3    Copyright (C) 1991-2022 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5 
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10 
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15 
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <https://www.gnu.org/licenses/>.  */
19 
20 #include <string.h>
21 #include <memcopy.h>
22 
23 /* All this is so that bcopy.c can #include
24    this file after defining some things.  */
25 #ifndef	a1
26 #define	a1	dest	/* First arg is DEST.  */
27 #define	a1const
28 #define	a2	src	/* Second arg is SRC.  */
29 #define	a2const	const
30 #undef memmove
31 #endif
32 #if	!defined(RETURN) || !defined(rettype)
33 #define	RETURN(s)	return (s)	/* Return DEST.  */
34 #define	rettype		void *
35 #endif
36 
37 #ifndef MEMMOVE
38 #define MEMMOVE memmove
39 #endif
40 
41 rettype
42 inhibit_loop_to_libcall
MEMMOVE(a1const void * a1,a2const void * a2,size_t len)43 MEMMOVE (a1const void *a1, a2const void *a2, size_t len)
44 {
45   unsigned long int dstp = (long int) dest;
46   unsigned long int srcp = (long int) src;
47 
48   /* This test makes the forward copying code be used whenever possible.
49      Reduces the working set.  */
50   if (dstp - srcp >= len)	/* *Unsigned* compare!  */
51     {
52       /* Copy from the beginning to the end.  */
53 
54 #if MEMCPY_OK_FOR_FWD_MEMMOVE
55       dest = memcpy (dest, src, len);
56 #else
57       /* If there not too few bytes to copy, use word copy.  */
58       if (len >= OP_T_THRES)
59 	{
60 	  /* Copy just a few bytes to make DSTP aligned.  */
61 	  len -= (-dstp) % OPSIZ;
62 	  BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
63 
64 	  /* Copy whole pages from SRCP to DSTP by virtual address
65 	     manipulation, as much as possible.  */
66 
67 	  PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len);
68 
69 	  /* Copy from SRCP to DSTP taking advantage of the known
70 	     alignment of DSTP.  Number of bytes remaining is put
71 	     in the third argument, i.e. in LEN.  This number may
72 	     vary from machine to machine.  */
73 
74 	  WORD_COPY_FWD (dstp, srcp, len, len);
75 
76 	  /* Fall out and copy the tail.  */
77 	}
78 
79       /* There are just a few bytes to copy.  Use byte memory operations.  */
80       BYTE_COPY_FWD (dstp, srcp, len);
81 #endif /* MEMCPY_OK_FOR_FWD_MEMMOVE */
82     }
83   else
84     {
85       /* Copy from the end to the beginning.  */
86       srcp += len;
87       dstp += len;
88 
89       /* If there not too few bytes to copy, use word copy.  */
90       if (len >= OP_T_THRES)
91 	{
92 	  /* Copy just a few bytes to make DSTP aligned.  */
93 	  len -= dstp % OPSIZ;
94 	  BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ);
95 
96 	  /* Copy from SRCP to DSTP taking advantage of the known
97 	     alignment of DSTP.  Number of bytes remaining is put
98 	     in the third argument, i.e. in LEN.  This number may
99 	     vary from machine to machine.  */
100 
101 	  WORD_COPY_BWD (dstp, srcp, len, len);
102 
103 	  /* Fall out and copy the tail.  */
104 	}
105 
106       /* There are just a few bytes to copy.  Use byte memory operations.  */
107       BYTE_COPY_BWD (dstp, srcp, len);
108     }
109 
110   RETURN (dest);
111 }
112 #ifndef memmove
113 libc_hidden_builtin_def (memmove)
114 #endif
115