|
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 |
|
|
25 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. |
|
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //=========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): jlarmour |
|
|
33 // Contributors: jlarmour@cygnus.co.uk |
|
|
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 |
|
|
78 while (n--) |
|
|
79 { |
|
|
80 if (*src == c) |
|
|
81 { |
|
|
82 CYG_REPORT_RETVAL( src ); |
|
|
83 return (void *) src; |
|
|
84 } // if |
|
|
85 src++; |
|
|
86 } |
|
|
87 CYG_REPORT_RETVAL( NULL ); |
|
|
88 return NULL; |
|
|
89 #else |
|
|
90 const unsigned char *src = (const unsigned char *) s; |
|
|
91 CYG_WORD *aligned_src; |
|
|
92 CYG_WORD buffer; |
|
|
93 CYG_WORD mask; |
|
|
94 cyg_ucount8 i; |
|
|
95 |
|
|
96 // If the size is small, or src is unaligned, then |
|
|
97 // use the bytewise loop. We can hope this is rare. |
|
|
98 if (CYG_LIBC_STR_OPT_TOO_SMALL (n) || CYG_LIBC_STR_UNALIGNED (src)) |
|
|
99 { |
|
|
100 while (n--) |
|
|
101 { |
|
|
102 if (*src == c) |
|
|
103 { |
|
|
104 CYG_REPORT_RETVAL( src ); |
|
|
105 return (void *) src; |
|
|
106 } // if |
|
|
107 src++; |
|
|
108 } |
|
|
109 CYG_REPORT_RETVAL( NULL ); |
|
|
110 return NULL; |
|
|
111 } |
|
|
112 |
|
|
113 // The fast code reads the ASCII one word at a time and only |
|
|
114 // performs the bytewise search on word-sized segments if they |
|
|
115 // contain the search character, which is detected by XORing |
|
|
116 // the word-sized segment with a word-sized block of the search |
|
|
117 // character and then detecting for the presence of NULL in the |
|
|
118 // result. |
|
|
119 |
|
|
120 aligned_src = (CYG_WORD *) src; |
|
|
121 mask = 0; |
|
|
122 for (i = 0; i < CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE; i++) |
|
|
123 mask = (mask << 8) + c; |
|
|
124 |
|
|
125 while (n > CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE) |
|
|
126 { |
|
|
127 buffer = *aligned_src; |
|
|
128 buffer ^= mask; |
|
|
129 if (CYG_LIBC_STR_DETECTNULL (buffer)) |
|
|
130 { |
|
|
131 src = (unsigned char*) aligned_src; |
|
|
132 for ( i = 0; i < CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE; i++ ) |
|
|
133 { |
|
|
134 if (*src == c) |
|
|
135 { |
|
|
136 CYG_REPORT_RETVAL( src ); |
|
|
137 return (void *) src; |
|
|
138 } // if |
|
|
139 src++; |
|
|
140 } |
|
|
141 } |
|
|
142 n -= CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE; |
|
|
143 aligned_src++; |
|
|
144 } |
|
|
145 |
|
|
146 // If there are fewer than CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE characters |
|
|
147 // left, then we resort to the bytewise loop. |
|
|
148 |
|
|
149 src = (const unsigned char *) aligned_src; |
|
|
150 while (n--) |
|
|
151 { |
|
|
152 if (*src == c) |
|
|
153 { |
|
|
154 CYG_REPORT_RETVAL( src ); |
|
|
155 return (void *) src; |
|
|
156 } // if |
|
|
157 src++; |
|
|
158 } |
|
|
159 |
|
|
160 CYG_REPORT_RETVAL( NULL ); |
|
|
161 return NULL; |
|
|
162 #endif // not defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || |
|
|
163 // defined(__OPTIMIZE_SIZE__) |
|
|
164 } // _memchr() |
|
|
165 |
|
|
166 #endif // ifdef CYGPKG_LIBC |
|
|
167 |
|
|
168 // EOF memchr.cxx |