]> git.ipfire.org Git - thirdparty/glibc.git/blame - string/memmove.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / string / memmove.c
CommitLineData
ebbad4cc 1/* Copy memory to memory until the specified number of bytes
28f540f4 2 has been copied. Overlap is handled correctly.
b168057a 3 Copyright (C) 1991-2015 Free Software Foundation, Inc.
41bdb6e2 4 This file is part of the GNU C Library.
28f540f4
RM
5 Contributed by Torbjorn Granlund (tege@sics.se).
6
6d52618b 7 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
28f540f4 11
6d52618b
UD
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
41bdb6e2 15 Lesser General Public License for more details.
28f540f4 16
41bdb6e2 17 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
28f540f4 20
28f540f4
RM
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. */
ebbad4cc 30#define a2const const
9a0a462c 31#undef memmove
28f540f4
RM
32#endif
33#if !defined(RETURN) || !defined(rettype)
34#define RETURN(s) return (s) /* Return DEST. */
ebbad4cc 35#define rettype void *
28f540f4
RM
36#endif
37
6fb8cbcb
L
38#ifndef MEMMOVE
39#define MEMMOVE memmove
40#endif
9a0a462c 41
28f540f4 42rettype
85c2e611 43inhibit_loop_to_libcall
6fb8cbcb 44MEMMOVE (a1, a2, len)
ebbad4cc
UD
45 a1const void *a1;
46 a2const void *a2;
47 size_t len;
28f540f4
RM
48{
49 unsigned long int dstp = (long int) dest;
50 unsigned long int srcp = (long int) src;
51
52 /* This test makes the forward copying code be used whenever possible.
53 Reduces the working set. */
54 if (dstp - srcp >= len) /* *Unsigned* compare! */
55 {
56 /* Copy from the beginning to the end. */
57
19218757
MK
58#if MEMCPY_OK_FOR_FWD_MEMMOVE
59 dest = memcpy (dest, src, len);
60#else
28f540f4
RM
61 /* If there not too few bytes to copy, use word copy. */
62 if (len >= OP_T_THRES)
63 {
64 /* Copy just a few bytes to make DSTP aligned. */
65 len -= (-dstp) % OPSIZ;
66 BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
67
67a87b93
RM
68 /* Copy whole pages from SRCP to DSTP by virtual address
69 manipulation, as much as possible. */
70
71 PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len);
72
28f540f4
RM
73 /* Copy from SRCP to DSTP taking advantage of the known
74 alignment of DSTP. Number of bytes remaining is put
6d52618b 75 in the third argument, i.e. in LEN. This number may
28f540f4
RM
76 vary from machine to machine. */
77
78 WORD_COPY_FWD (dstp, srcp, len, len);
79
80 /* Fall out and copy the tail. */
81 }
82
83 /* There are just a few bytes to copy. Use byte memory operations. */
84 BYTE_COPY_FWD (dstp, srcp, len);
19218757 85#endif /* MEMCPY_OK_FOR_FWD_MEMMOVE */
28f540f4
RM
86 }
87 else
88 {
89 /* Copy from the end to the beginning. */
90 srcp += len;
91 dstp += len;
92
93 /* If there not too few bytes to copy, use word copy. */
94 if (len >= OP_T_THRES)
95 {
96 /* Copy just a few bytes to make DSTP aligned. */
97 len -= dstp % OPSIZ;
98 BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ);
99
100 /* Copy from SRCP to DSTP taking advantage of the known
101 alignment of DSTP. Number of bytes remaining is put
6d52618b 102 in the third argument, i.e. in LEN. This number may
28f540f4
RM
103 vary from machine to machine. */
104
105 WORD_COPY_BWD (dstp, srcp, len, len);
106
107 /* Fall out and copy the tail. */
108 }
109
110 /* There are just a few bytes to copy. Use byte memory operations. */
111 BYTE_COPY_BWD (dstp, srcp, len);
112 }
113
ebbad4cc 114 RETURN (dest);
28f540f4 115}
85dd1003
UD
116#ifndef memmove
117libc_hidden_builtin_def (memmove)
118#endif