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