Thu Nov 2 19:24:37 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[glibc.git] / sysdeps / generic / memmove.c
blob8ef6f04cbe6b7b78ff1e04a428b433a85620c27a
1 /* memmove -- copy memory to memory until the specified number of bytes
2 has been copied. Overlap is handled correctly.
3 Copyright (C) 1991, 1995 Free Software Foundation, Inc.
4 Contributed by Torbjorn Granlund (tege@sics.se).
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
19 Cambridge, MA 02139, USA. */
21 #include <ansidecl.h>
22 #include <string.h>
23 #include <memcopy.h>
24 #include <pagecopy.h>
26 /* All this is so that bcopy.c can #include
27 this file after defining some things. */
28 #ifndef a1
29 #define a1 dest /* First arg is DEST. */
30 #define a1const
31 #define a2 src /* Second arg is SRC. */
32 #define a2const CONST
33 #endif
34 #if !defined(RETURN) || !defined(rettype)
35 #define RETURN(s) return (s) /* Return DEST. */
36 #define rettype PTR
37 #endif
39 rettype
40 DEFUN(memmove, (a1, a2, len),
41 a1const PTR a1 AND a2const PTR a2 AND size_t len)
43 unsigned long int dstp = (long int) dest;
44 unsigned long int srcp = (long int) src;
46 /* This test makes the forward copying code be used whenever possible.
47 Reduces the working set. */
48 if (dstp - srcp >= len) /* *Unsigned* compare! */
50 /* Copy from the beginning to the end. */
52 /* If there not too few bytes to copy, use word copy. */
53 if (len >= OP_T_THRES)
55 /* Copy just a few bytes to make DSTP aligned. */
56 len -= (-dstp) % OPSIZ;
57 BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
59 /* Copy whole pages from SRCP to DSTP by virtual address
60 manipulation, as much as possible. */
62 PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len);
64 /* Copy from SRCP to DSTP taking advantage of the known
65 alignment of DSTP. Number of bytes remaining is put
66 in the third argumnet, i.e. in LEN. This number may
67 vary from machine to machine. */
69 WORD_COPY_FWD (dstp, srcp, len, len);
71 /* Fall out and copy the tail. */
74 /* There are just a few bytes to copy. Use byte memory operations. */
75 BYTE_COPY_FWD (dstp, srcp, len);
77 else
79 /* Copy from the end to the beginning. */
80 srcp += len;
81 dstp += len;
83 /* If there not too few bytes to copy, use word copy. */
84 if (len >= OP_T_THRES)
86 /* Copy just a few bytes to make DSTP aligned. */
87 len -= dstp % OPSIZ;
88 BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ);
90 /* Copy from SRCP to DSTP taking advantage of the known
91 alignment of DSTP. Number of bytes remaining is put
92 in the third argumnet, i.e. in LEN. This number may
93 vary from machine to machine. */
95 WORD_COPY_BWD (dstp, srcp, len, len);
97 /* Fall out and copy the tail. */
100 /* There are just a few bytes to copy. Use byte memory operations. */
101 BYTE_COPY_BWD (dstp, srcp, len);
104 RETURN(dest);