Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / string / memccpy.c
blobd6b2f8beabe1fc0a1cba0d4a9aeb6c15f2bf4791
1 /*
2 FUNCTION
3 <<memccpy>>---copy memory regions with end-token check
5 SYNOPSIS
6 #include <string.h>
7 void* memccpy(void *restrict <[out]>, const void *restrict <[in]>,
8 int <[endchar]>, size_t <[n]>);
10 DESCRIPTION
11 This function copies up to <[n]> bytes from the memory region
12 pointed to by <[in]> to the memory region pointed to by
13 <[out]>. If a byte matching the <[endchar]> is encountered,
14 the byte is copied and copying stops.
16 If the regions overlap, the behavior is undefined.
18 RETURNS
19 <<memccpy>> returns a pointer to the first byte following the
20 <[endchar]> in the <[out]> region. If no byte matching
21 <[endchar]> was copied, then <<NULL>> is returned.
23 PORTABILITY
24 <<memccpy>> is a GNU extension.
26 <<memccpy>> requires no supporting OS subroutines.
30 #include <_ansi.h>
31 #include <stddef.h>
32 #include <string.h>
33 #include <limits.h>
35 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
36 #define UNALIGNED(X, Y) \
37 (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
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) < LITTLEBLOCKSIZE)
45 /* Macros for detecting endchar */
46 #if LONG_MAX == 2147483647L
47 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
48 #else
49 #if LONG_MAX == 9223372036854775807L
50 /* Nonzero if X (a long int) contains a NULL byte. */
51 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
52 #else
53 #error long int is not a 32bit or 64bit type.
54 #endif
55 #endif
58 void *
59 memccpy (void *__restrict dst0,
60 const void *__restrict src0,
61 int endchar0,
62 size_t len0)
65 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
66 void *ptr = NULL;
67 char *dst = (char *) dst0;
68 char *src = (char *) src0;
69 char endchar = endchar0 & 0xff;
71 while (len0--)
73 if ((*dst++ = *src++) == endchar)
75 ptr = dst;
76 break;
80 return ptr;
81 #else
82 void *ptr = NULL;
83 unsigned char *dst = dst0;
84 const unsigned char *src = src0;
85 long *aligned_dst;
86 const long *aligned_src;
87 unsigned char endchar = endchar0 & 0xff;
89 /* If the size is small, or either SRC or DST is unaligned,
90 then punt into the byte copy loop. This should be rare. */
91 if (!TOO_SMALL(len0) && !UNALIGNED (src, dst))
93 unsigned int i;
94 unsigned long mask = 0;
96 aligned_dst = (long*)dst;
97 aligned_src = (long*)src;
99 /* The fast code reads the ASCII one word at a time and only
100 performs the bytewise search on word-sized segments if they
101 contain the search character, which is detected by XORing
102 the word-sized segment with a word-sized block of the search
103 character and then detecting for the presence of NULL in the
104 result. */
105 for (i = 0; i < LITTLEBLOCKSIZE; i++)
106 mask = (mask << 8) + endchar;
109 /* Copy one long word at a time if possible. */
110 while (len0 >= LITTLEBLOCKSIZE)
112 unsigned long buffer = (unsigned long)(*aligned_src);
113 buffer ^= mask;
114 if (DETECTNULL (buffer))
115 break; /* endchar is found, go byte by byte from here */
116 *aligned_dst++ = *aligned_src++;
117 len0 -= LITTLEBLOCKSIZE;
120 /* Pick up any residual with a byte copier. */
121 dst = (unsigned char*)aligned_dst;
122 src = (unsigned char*)aligned_src;
125 while (len0--)
127 if ((*dst++ = *src++) == endchar)
129 ptr = dst;
130 break;
134 return ptr;
135 #endif /* not PREFER_SIZE_OVER_SPEED */