3 <<memccpy>>---copy memory regions with end-token check
7 void* memccpy(void *<[out]>, const void *<[in]>,
8 int <[endchar]>, size_t <[n]>);
11 void *memccpy(<[out]>, <[in]>, <[endchar]>, <[n]>
18 This function copies up to <[n]> bytes from the memory region
19 pointed to by <[in]> to the memory region pointed to by
20 <[out]>. If a byte matching the <[endchar]> is encountered,
21 the byte is copied and copying stops.
23 If the regions overlap, the behavior is undefined.
26 <<memccpy>> returns a pointer to the first byte following the
27 <[endchar]> in the <[out]> region. If no byte matching
28 <[endchar]> was copied, then <<NULL>> is returned.
31 <<memccpy>> is a GNU extension.
33 <<memccpy>> requires no supporting OS subroutines.
42 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
43 #define UNALIGNED(X, Y) \
44 (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
46 /* How many bytes are copied each iteration of the word copy loop. */
47 #define LITTLEBLOCKSIZE (sizeof (long))
49 /* Threshhold for punting to the byte copier. */
50 #define TOO_SMALL(LEN) ((LEN) < LITTLEBLOCKSIZE)
52 /* Macros for detecting endchar */
53 #if LONG_MAX == 2147483647L
54 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
56 #if LONG_MAX == 9223372036854775807L
57 /* Nonzero if X (a long int) contains a NULL byte. */
58 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
60 #error long int is not a 32bit or 64bit type.
66 _DEFUN (memccpy
, (dst0
, src0
, endchar
, len0
),
73 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
75 char *dst
= (char *) dst0
;
76 char *src
= (char *) src0
;
77 char endchar
= endchar0
& 0xff;
81 if ((*dst
++ = *src
++) == endchar
)
92 _CONST
char *src
= src0
;
94 _CONST
long *aligned_src
;
96 char endchar
= endchar0
& 0xff;
98 /* If the size is small, or either SRC or DST is unaligned,
99 then punt into the byte copy loop. This should be rare. */
100 if (!TOO_SMALL(len
) && !UNALIGNED (src
, dst
))
103 unsigned long mask
= 0;
105 aligned_dst
= (long*)dst
;
106 aligned_src
= (long*)src
;
108 /* The fast code reads the ASCII one word at a time and only
109 performs the bytewise search on word-sized segments if they
110 contain the search character, which is detected by XORing
111 the word-sized segment with a word-sized block of the search
112 character and then detecting for the presence of NULL in the
114 for (i
= 0; i
< LITTLEBLOCKSIZE
; i
++)
115 mask
= (mask
<< 8) + endchar
;
118 /* Copy one long word at a time if possible. */
119 while (len
>= LITTLEBLOCKSIZE
)
121 unsigned long buffer
= (unsigned long)(*aligned_src
);
123 if (DETECTNULL (buffer
))
124 break; /* endchar is found, go byte by byte from here */
125 *aligned_dst
++ = *aligned_src
++;
126 len
-= LITTLEBLOCKSIZE
;
129 /* Pick up any residual with a byte copier. */
130 dst
= (char*)aligned_dst
;
131 src
= (char*)aligned_src
;
136 if ((*dst
++ = *src
++) == endchar
)
144 #endif /* not PREFER_SIZE_OVER_SPEED */