Update.
[platform/upstream/glibc.git] / sysdeps / generic / memmove.c
1 /* Copy memory to memory until the specified number of bytes
2    has been copied.  Overlap is handled correctly.
3    Copyright (C) 1991, 1995, 1996, 1997 Free Software Foundation, Inc.
4    Contributed by Torbjorn Granlund (tege@sics.se).
5
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.
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    Library General Public License for more details.
15
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 not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <string.h>
22 #include <memcopy.h>
23 #include <pagecopy.h>
24
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
38
39
40 rettype
41 memmove (a1, a2, len)
42      a1const void *a1;
43      a2const void *a2;
44      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 there not too few bytes to copy, use word copy.  */
56       if (len >= OP_T_THRES)
57         {
58           /* Copy just a few bytes to make DSTP aligned.  */
59           len -= (-dstp) % OPSIZ;
60           BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
61
62           /* Copy whole pages from SRCP to DSTP by virtual address
63              manipulation, as much as possible.  */
64
65           PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len);
66
67           /* Copy from SRCP to DSTP taking advantage of the known
68              alignment of DSTP.  Number of bytes remaining is put
69              in the third argument, i.e. in LEN.  This number may
70              vary from machine to machine.  */
71
72           WORD_COPY_FWD (dstp, srcp, len, len);
73
74           /* Fall out and copy the tail.  */
75         }
76
77       /* There are just a few bytes to copy.  Use byte memory operations.  */
78       BYTE_COPY_FWD (dstp, srcp, len);
79     }
80   else
81     {
82       /* Copy from the end to the beginning.  */
83       srcp += len;
84       dstp += len;
85
86       /* If there not too few bytes to copy, use word copy.  */
87       if (len >= OP_T_THRES)
88         {
89           /* Copy just a few bytes to make DSTP aligned.  */
90           len -= dstp % OPSIZ;
91           BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ);
92
93           /* Copy from SRCP to DSTP taking advantage of the known
94              alignment of DSTP.  Number of bytes remaining is put
95              in the third argument, i.e. in LEN.  This number may
96              vary from machine to machine.  */
97
98           WORD_COPY_BWD (dstp, srcp, len, len);
99
100           /* Fall out and copy the tail.  */
101         }
102
103       /* There are just a few bytes to copy.  Use byte memory operations.  */
104       BYTE_COPY_BWD (dstp, srcp, len);
105     }
106
107   RETURN (dest);
108 }