1 |
27 |
unneback |
//===========================================================================
|
2 |
|
|
//
|
3 |
|
|
// memchr.cxx
|
4 |
|
|
//
|
5 |
|
|
// ANSI standard memchr() routine
|
6 |
|
|
//
|
7 |
|
|
//===========================================================================
|
8 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
16 |
|
|
//
|
17 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
18 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
20 |
|
|
// for more details.
|
21 |
|
|
//
|
22 |
|
|
// You should have received a copy of the GNU General Public License along
|
23 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
24 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
25 |
|
|
//
|
26 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
27 |
|
|
// or inline functions from this file, or you compile this file and link it
|
28 |
|
|
// with other works to produce a work based on this file, this file does not
|
29 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
30 |
|
|
// License. However the source code for this file must still be made available
|
31 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
32 |
|
|
//
|
33 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
34 |
|
|
// this file might be covered by the GNU General Public License.
|
35 |
|
|
//
|
36 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
37 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
38 |
|
|
// -------------------------------------------
|
39 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
40 |
|
|
//===========================================================================
|
41 |
|
|
//#####DESCRIPTIONBEGIN####
|
42 |
|
|
//
|
43 |
|
|
// Author(s): jlarmour
|
44 |
|
|
// Contributors:
|
45 |
|
|
// Date: 2000-04-14
|
46 |
|
|
// Purpose:
|
47 |
|
|
// Description:
|
48 |
|
|
// Usage:
|
49 |
|
|
//
|
50 |
|
|
//####DESCRIPTIONEND####
|
51 |
|
|
//
|
52 |
|
|
//===========================================================================
|
53 |
|
|
|
54 |
|
|
// CONFIGURATION
|
55 |
|
|
|
56 |
|
|
#include <pkgconf/libc_string.h> // Configuration header
|
57 |
|
|
|
58 |
|
|
// INCLUDES
|
59 |
|
|
|
60 |
|
|
#include <cyg/infra/cyg_type.h> // Common type definitions
|
61 |
|
|
#include <cyg/infra/cyg_trac.h> // Tracing support
|
62 |
|
|
#include <cyg/infra/cyg_ass.h> // Assertion support
|
63 |
|
|
#include <string.h> // Header for this file
|
64 |
|
|
#include <stddef.h> // Compiler definitions such as size_t, NULL etc.
|
65 |
|
|
#include <cyg/libc/string/stringsupp.hxx> // Useful string function support and
|
66 |
|
|
// prototypes
|
67 |
|
|
|
68 |
|
|
// EXPORTED SYMBOLS
|
69 |
|
|
|
70 |
|
|
externC void *
|
71 |
|
|
memchr( const void *s, int c, size_t n ) CYGBLD_ATTRIB_WEAK_ALIAS(__memchr);
|
72 |
|
|
|
73 |
|
|
// FUNCTIONS
|
74 |
|
|
|
75 |
|
|
void *
|
76 |
|
|
__memchr( const void *s, int c, size_t n )
|
77 |
|
|
{
|
78 |
|
|
CYG_REPORT_FUNCNAMETYPE( "__memchr", "returning addr %08x" );
|
79 |
|
|
CYG_REPORT_FUNCARG3( "s=%08x, c=%d, n=%d", s, c, n );
|
80 |
|
|
|
81 |
|
|
if (n)
|
82 |
|
|
{
|
83 |
|
|
CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" );
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
#if defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || defined(__OPTIMIZE_SIZE__)
|
87 |
|
|
const unsigned char *src = (const unsigned char *) s;
|
88 |
|
|
|
89 |
|
|
c &= 0xff;
|
90 |
|
|
|
91 |
|
|
while (n--)
|
92 |
|
|
{
|
93 |
|
|
if (*src == c)
|
94 |
|
|
{
|
95 |
|
|
CYG_REPORT_RETVAL( src );
|
96 |
|
|
return (void *) src;
|
97 |
|
|
} // if
|
98 |
|
|
src++;
|
99 |
|
|
}
|
100 |
|
|
CYG_REPORT_RETVAL( NULL );
|
101 |
|
|
return NULL;
|
102 |
|
|
#else
|
103 |
|
|
const unsigned char *src = (const unsigned char *) s;
|
104 |
|
|
CYG_WORD *aligned_src;
|
105 |
|
|
CYG_WORD buffer;
|
106 |
|
|
CYG_WORD mask;
|
107 |
|
|
cyg_ucount8 i;
|
108 |
|
|
|
109 |
|
|
c &= 0xff;
|
110 |
|
|
|
111 |
|
|
// If the size is small, or src is unaligned, then
|
112 |
|
|
// use the bytewise loop. We can hope this is rare.
|
113 |
|
|
if (CYG_LIBC_STR_OPT_TOO_SMALL (n) || CYG_LIBC_STR_UNALIGNED (src))
|
114 |
|
|
{
|
115 |
|
|
while (n--)
|
116 |
|
|
{
|
117 |
|
|
if (*src == c)
|
118 |
|
|
{
|
119 |
|
|
CYG_REPORT_RETVAL( src );
|
120 |
|
|
return (void *) src;
|
121 |
|
|
} // if
|
122 |
|
|
src++;
|
123 |
|
|
}
|
124 |
|
|
CYG_REPORT_RETVAL( NULL );
|
125 |
|
|
return NULL;
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
// The fast code reads the ASCII one word at a time and only
|
129 |
|
|
// performs the bytewise search on word-sized segments if they
|
130 |
|
|
// contain the search character, which is detected by XORing
|
131 |
|
|
// the word-sized segment with a word-sized block of the search
|
132 |
|
|
// character and then detecting for the presence of NULL in the
|
133 |
|
|
// result.
|
134 |
|
|
|
135 |
|
|
aligned_src = (CYG_WORD *) src;
|
136 |
|
|
mask = 0;
|
137 |
|
|
for (i = 0; i < CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE; i++)
|
138 |
|
|
mask = (mask << 8) + c;
|
139 |
|
|
|
140 |
|
|
while (n > CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE)
|
141 |
|
|
{
|
142 |
|
|
buffer = *aligned_src;
|
143 |
|
|
buffer ^= mask;
|
144 |
|
|
if (CYG_LIBC_STR_DETECTNULL (buffer))
|
145 |
|
|
{
|
146 |
|
|
src = (unsigned char*) aligned_src;
|
147 |
|
|
for ( i = 0; i < CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE; i++ )
|
148 |
|
|
{
|
149 |
|
|
if (*src == c)
|
150 |
|
|
{
|
151 |
|
|
CYG_REPORT_RETVAL( src );
|
152 |
|
|
return (void *) src;
|
153 |
|
|
} // if
|
154 |
|
|
src++;
|
155 |
|
|
}
|
156 |
|
|
}
|
157 |
|
|
n -= CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE;
|
158 |
|
|
aligned_src++;
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
// If there are fewer than CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE characters
|
162 |
|
|
// left, then we resort to the bytewise loop.
|
163 |
|
|
|
164 |
|
|
src = (const unsigned char *) aligned_src;
|
165 |
|
|
while (n--)
|
166 |
|
|
{
|
167 |
|
|
if (*src == c)
|
168 |
|
|
{
|
169 |
|
|
CYG_REPORT_RETVAL( src );
|
170 |
|
|
return (void *) src;
|
171 |
|
|
} // if
|
172 |
|
|
src++;
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
CYG_REPORT_RETVAL( NULL );
|
176 |
|
|
return NULL;
|
177 |
|
|
#endif // not defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) ||
|
178 |
|
|
// defined(__OPTIMIZE_SIZE__)
|
179 |
|
|
} // __memchr()
|
180 |
|
|
|
181 |
|
|
// EOF memchr.cxx
|