|
0
|
1 //=========================================================================== |
|
|
2 // |
|
|
3 // memchr.cxx |
|
|
4 // |
|
|
5 // ANSI standard memchr() routine |
|
|
6 // |
|
|
7 //=========================================================================== |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //=========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): jlarmour |
|
2
|
33 // Contributors: jlarmour |
|
0
|
34 // Date: 1998-02-13 |
|
|
35 // Purpose: |
|
|
36 // Description: |
|
|
37 // Usage: |
|
|
38 // |
|
|
39 //####DESCRIPTIONEND#### |
|
|
40 // |
|
|
41 //=========================================================================== |
|
|
42 |
|
|
43 // CONFIGURATION |
|
|
44 |
|
|
45 #include <pkgconf/libc.h> // Configuration header |
|
|
46 |
|
|
47 // Include the C library? |
|
|
48 #ifdef CYGPKG_LIBC |
|
|
49 |
|
|
50 // INCLUDES |
|
|
51 |
|
|
52 #include <cyg/infra/cyg_type.h> // Common type definitions |
|
|
53 #include <cyg/infra/cyg_trac.h> // Tracing support |
|
|
54 #include <cyg/infra/cyg_ass.h> // Assertion support |
|
|
55 #include <string.h> // Header for this file |
|
|
56 #include <stddef.h> // Compiler definitions such as size_t, NULL etc. |
|
|
57 #include "clibincl/stringsupp.hxx" // Useful string function support and |
|
|
58 // prototypes |
|
|
59 |
|
|
60 // EXPORTED SYMBOLS |
|
|
61 |
|
|
62 externC void * |
|
|
63 memchr( const void *s, int c, size_t n ) CYGPRI_LIBC_WEAK_ALIAS("_memchr"); |
|
|
64 |
|
|
65 // FUNCTIONS |
|
|
66 |
|
|
67 void * |
|
|
68 _memchr( const void *s, int c, size_t n ) |
|
|
69 { |
|
|
70 CYG_REPORT_FUNCNAMETYPE( "_memchr", "returning addr %08x" ); |
|
|
71 CYG_REPORT_FUNCARG3( "s=%08x, c=%d, n=%d", s, c, n ); |
|
|
72 |
|
|
73 CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" ); |
|
|
74 |
|
|
75 #if defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || defined(__OPTIMIZE_SIZE__) |
|
|
76 const unsigned char *src = (const unsigned char *) s; |
|
|
77 |
|
2
|
78 c &= 0xff; |
|
|
79 |
|
0
|
80 while (n--) |
|
|
81 { |
|
|
82 if (*src == c) |
|
|
83 { |
|
|
84 CYG_REPORT_RETVAL( src ); |
|
|
85 return (void *) src; |
|
|
86 } // if |
|
|
87 src++; |
|
|
88 } |
|
|
89 CYG_REPORT_RETVAL( NULL ); |
|
|
90 return NULL; |
|
|
91 #else |
|
|
92 const unsigned char *src = (const unsigned char *) s; |
|
|
93 CYG_WORD *aligned_src; |
|
|
94 CYG_WORD buffer; |
|
|
95 CYG_WORD mask; |
|
|
96 cyg_ucount8 i; |
|
2
|
97 |
|
|
98 c &= 0xff; |
|
0
|
99 |
|
|
100 // If the size is small, or src is unaligned, then |
|
|
101 // use the bytewise loop. We can hope this is rare. |
|
|
102 if (CYG_LIBC_STR_OPT_TOO_SMALL (n) || CYG_LIBC_STR_UNALIGNED (src)) |
|
|
103 { |
|
|
104 while (n--) |
|
|
105 { |
|
|
106 if (*src == c) |
|
|
107 { |
|
|
108 CYG_REPORT_RETVAL( src ); |
|
|
109 return (void *) src; |
|
|
110 } // if |
|
|
111 src++; |
|
|
112 } |
|
|
113 CYG_REPORT_RETVAL( NULL ); |
|
|
114 return NULL; |
|
|
115 } |
|
|
116 |
|
|
117 // The fast code reads the ASCII one word at a time and only |
|
|
118 // performs the bytewise search on word-sized segments if they |
|
|
119 // contain the search character, which is detected by XORing |
|
|
120 // the word-sized segment with a word-sized block of the search |
|
|
121 // character and then detecting for the presence of NULL in the |
|
|
122 // result. |
|
|
123 |
|
|
124 aligned_src = (CYG_WORD *) src; |
|
|
125 mask = 0; |
|
|
126 for (i = 0; i < CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE; i++) |
|
|
127 mask = (mask << 8) + c; |
|
|
128 |
|
|
129 while (n > CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE) |
|
|
130 { |
|
|
131 buffer = *aligned_src; |
|
|
132 buffer ^= mask; |
|
|
133 if (CYG_LIBC_STR_DETECTNULL (buffer)) |
|
|
134 { |
|
|
135 src = (unsigned char*) aligned_src; |
|
|
136 for ( i = 0; i < CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE; i++ ) |
|
|
137 { |
|
|
138 if (*src == c) |
|
|
139 { |
|
|
140 CYG_REPORT_RETVAL( src ); |
|
|
141 return (void *) src; |
|
|
142 } // if |
|
|
143 src++; |
|
|
144 } |
|
|
145 } |
|
|
146 n -= CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE; |
|
|
147 aligned_src++; |
|
|
148 } |
|
|
149 |
|
|
150 // If there are fewer than CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE characters |
|
|
151 // left, then we resort to the bytewise loop. |
|
|
152 |
|
|
153 src = (const unsigned char *) aligned_src; |
|
|
154 while (n--) |
|
|
155 { |
|
|
156 if (*src == c) |
|
|
157 { |
|
|
158 CYG_REPORT_RETVAL( src ); |
|
|
159 return (void *) src; |
|
|
160 } // if |
|
|
161 src++; |
|
|
162 } |
|
|
163 |
|
|
164 CYG_REPORT_RETVAL( NULL ); |
|
|
165 return NULL; |
|
|
166 #endif // not defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || |
|
|
167 // defined(__OPTIMIZE_SIZE__) |
|
|
168 } // _memchr() |
|
|
169 |
|
|
170 #endif // ifdef CYGPKG_LIBC |
|
|
171 |
|
|
172 // EOF memchr.cxx |