Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / string / mempcpy.c
blob129165603a10d124e74e9e102012f1f0a3b898f2
1 /*
2 FUNCTION
3 <<mempcpy>>---copy memory regions and return end pointer
5 SYNOPSIS
6 #include <string.h>
7 void* mempcpy(void *<[out]>, const void *<[in]>, size_t <[n]>);
9 DESCRIPTION
10 This function copies <[n]> bytes from the memory region
11 pointed to by <[in]> to the memory region pointed to by
12 <[out]>.
14 If the regions overlap, the behavior is undefined.
16 RETURNS
17 <<mempcpy>> returns a pointer to the byte following the
18 last byte copied to the <[out]> region.
20 PORTABILITY
21 <<mempcpy>> is a GNU extension.
23 <<mempcpy>> requires no supporting OS subroutines.
27 #include <_ansi.h>
28 #include <stddef.h>
29 #include <limits.h>
30 #include <string.h>
32 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
33 #define UNALIGNED(X, Y) \
34 (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
36 /* How many bytes are copied each iteration of the 4X unrolled loop. */
37 #define BIGBLOCKSIZE (sizeof (long) << 2)
39 /* How many bytes are copied each iteration of the word copy loop. */
40 #define LITTLEBLOCKSIZE (sizeof (long))
42 /* Threshhold for punting to the byte copier. */
43 #define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE)
45 void *
46 mempcpy (void *dst0,
47 const void *src0,
48 size_t len0)
50 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
51 char *dst = (char *) dst0;
52 char *src = (char *) src0;
54 while (len0--)
56 *dst++ = *src++;
59 return dst;
60 #else
61 char *dst = dst0;
62 const char *src = src0;
63 long *aligned_dst;
64 const long *aligned_src;
66 /* If the size is small, or either SRC or DST is unaligned,
67 then punt into the byte copy loop. This should be rare. */
68 if (!TOO_SMALL(len0) && !UNALIGNED (src, dst))
70 aligned_dst = (long*)dst;
71 aligned_src = (long*)src;
73 /* Copy 4X long words at a time if possible. */
74 while (len0 >= BIGBLOCKSIZE)
76 *aligned_dst++ = *aligned_src++;
77 *aligned_dst++ = *aligned_src++;
78 *aligned_dst++ = *aligned_src++;
79 *aligned_dst++ = *aligned_src++;
80 len0 -= BIGBLOCKSIZE;
83 /* Copy one long word at a time if possible. */
84 while (len0 >= LITTLEBLOCKSIZE)
86 *aligned_dst++ = *aligned_src++;
87 len0 -= LITTLEBLOCKSIZE;
90 /* Pick up any residual with a byte copier. */
91 dst = (char*)aligned_dst;
92 src = (char*)aligned_src;
95 while (len0--)
96 *dst++ = *src++;
98 return dst;
99 #endif /* not PREFER_SIZE_OVER_SPEED */