3 <<memccpy>>---copy memory regions with end-token check
7 void* memccpy(void *restrict <[out]>, const void *restrict <[in]>,
8 int <[endchar]>, size_t <[n]>);
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.
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.
24 <<memccpy>> is a GNU extension.
26 <<memccpy>> requires no supporting OS subroutines.
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)
49 #if LONG_MAX == 9223372036854775807L
50 /* Nonzero if X (a long int) contains a NULL byte. */
51 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
53 #error long int is not a 32bit or 64bit type.
59 memccpy (void *__restrict dst0
,
60 const void *__restrict src0
,
65 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
67 char *dst
= (char *) dst0
;
68 char *src
= (char *) src0
;
69 char endchar
= endchar0
& 0xff;
73 if ((*dst
++ = *src
++) == endchar
)
83 unsigned char *dst
= dst0
;
84 const unsigned char *src
= src0
;
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
))
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
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
);
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
;
127 if ((*dst
++ = *src
++) == endchar
)
135 #endif /* not PREFER_SIZE_OVER_SPEED */