Update copyright notices with scripts/update-copyrights
[glibc.git] / string / memmove.c
blob3373401721c73168ea7d4e5204d6a2e405a831b5
1 /* Copy memory to memory until the specified number of bytes
2 has been copied. Overlap is handled correctly.
3 Copyright (C) 1991-2014 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Torbjorn Granlund (tege@sics.se).
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.
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.
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/>. */
21 #include <string.h>
22 #include <memcopy.h>
23 #include <pagecopy.h>
25 /* All this is so that bcopy.c can #include
26 this file after defining some things. */
27 #ifndef a1
28 #define a1 dest /* First arg is DEST. */
29 #define a1const
30 #define a2 src /* Second arg is SRC. */
31 #define a2const const
32 #undef memmove
33 #endif
34 #if !defined(RETURN) || !defined(rettype)
35 #define RETURN(s) return (s) /* Return DEST. */
36 #define rettype void *
37 #endif
39 #ifndef MEMMOVE
40 #define MEMMOVE memmove
41 #endif
43 rettype
44 inhibit_loop_to_libcall
45 MEMMOVE (a1, a2, len)
46 a1const void *a1;
47 a2const void *a2;
48 size_t len;
50 unsigned long int dstp = (long int) dest;
51 unsigned long int srcp = (long int) src;
53 /* This test makes the forward copying code be used whenever possible.
54 Reduces the working set. */
55 if (dstp - srcp >= len) /* *Unsigned* compare! */
57 /* Copy from the beginning to the end. */
59 #if MEMCPY_OK_FOR_FWD_MEMMOVE
60 dest = memcpy (dest, src, len);
61 #else
62 /* If there not too few bytes to copy, use word copy. */
63 if (len >= OP_T_THRES)
65 /* Copy just a few bytes to make DSTP aligned. */
66 len -= (-dstp) % OPSIZ;
67 BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
69 /* Copy whole pages from SRCP to DSTP by virtual address
70 manipulation, as much as possible. */
72 PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len);
74 /* Copy from SRCP to DSTP taking advantage of the known
75 alignment of DSTP. Number of bytes remaining is put
76 in the third argument, i.e. in LEN. This number may
77 vary from machine to machine. */
79 WORD_COPY_FWD (dstp, srcp, len, len);
81 /* Fall out and copy the tail. */
84 /* There are just a few bytes to copy. Use byte memory operations. */
85 BYTE_COPY_FWD (dstp, srcp, len);
86 #endif /* MEMCPY_OK_FOR_FWD_MEMMOVE */
88 else
90 /* Copy from the end to the beginning. */
91 srcp += len;
92 dstp += len;
94 /* If there not too few bytes to copy, use word copy. */
95 if (len >= OP_T_THRES)
97 /* Copy just a few bytes to make DSTP aligned. */
98 len -= dstp % OPSIZ;
99 BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ);
101 /* Copy from SRCP to DSTP taking advantage of the known
102 alignment of DSTP. Number of bytes remaining is put
103 in the third argument, i.e. in LEN. This number may
104 vary from machine to machine. */
106 WORD_COPY_BWD (dstp, srcp, len, len);
108 /* Fall out and copy the tail. */
111 /* There are just a few bytes to copy. Use byte memory operations. */
112 BYTE_COPY_BWD (dstp, srcp, len);
115 RETURN (dest);
117 #ifndef memmove
118 libc_hidden_builtin_def (memmove)
119 #endif