1 /*===========================================================================
5 // ANSI standard memset() routine
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.
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.
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
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.
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.
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.
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####
43 // Author(s): jlarmour
44 // Contributors: jlarmour
46 // Purpose: This file implements the ANSI memset() function
47 // Description: This file implements the memset() function defined in ANSI para
48 // 7.11.6.1. This is implemented in the kernel rather than the
49 // C library due to it being required by gcc whether or not the
50 // C library has been configured in.
52 //####DESCRIPTIONEND####
54 //==========================================================================*/
59 #include <pkgconf/infra.h> /* Configuration of infra package */
61 #include <cyg/infra/cyg_type.h> /* Common type definitions */
62 #include <cyg/infra/cyg_trac.h> /* Tracing support */
63 #include <cyg/infra/cyg_ass.h> /* Assertion support */
64 #include <stddef.h> /* Compiler defns such as size_t, NULL etc. */
68 /* Nonzero if X is not aligned on a word boundary. */
69 #define CYG_STR_UNALIGNED(X) ((CYG_WORD)(X) & (sizeof (CYG_WORD) - 1))
71 /* How many bytes are copied each iteration of the word copy loop in the
72 * optimised string implementation
74 #define CYG_STR_OPT_LITTLEBLOCKSIZE (sizeof (CYG_WORD))
76 /* EXPORTED SYMBOLS */
79 memset( void *s, int c, size_t n ) __attribute__ ((weak, alias("_memset")));
84 _memset( void *s, int c, size_t n )
86 #if defined(CYGIMP_INFRA_PREFER_SMALL_TO_FAST_MEMSET) || defined(__OPTIMIZE_SIZE__)
87 char *char_ptr = (char *)s;
89 #ifdef CYG_TRACING_FIXED
90 CYG_REPORT_FUNCNAMETYPE( "_memset", "returning %08x" );
91 CYG_REPORT_FUNCARG3( "s=%08x, c=%d, n=%d", s, c, n );
95 CYG_CHECK_DATA_PTR( char_ptr, "s is not a valid pointer!" );
96 CYG_CHECK_DATA_PTR( (&char_ptr[n-1]), "s+n-1 is not a valid address!" );
102 *char_ptr++ = (char) c;
105 #ifdef CYG_TRACING_FIXED
106 CYG_REPORT_RETVAL( s );
110 char *char_ptr = (char *)s;
113 CYG_WORD *aligned_addr;
114 char *unaligned_addr;
116 #ifdef CYG_TRACING_FIXED
117 CYG_REPORT_FUNCNAMETYPE( "_memset", "returning %08x" );
118 CYG_REPORT_FUNCARG3( "s=%08x, c=%d, n=%d", s, c, n );
122 CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" );
123 CYG_CHECK_DATA_PTR( (char *)s+n-1, "s+n-1 is not a valid address!" );
127 if (n < sizeof(CYG_WORD) || CYG_STR_UNALIGNED (s))
131 *char_ptr++ = (char) c;
133 #ifdef CYG_TRACING_FIXED
134 CYG_REPORT_RETVAL( s );
139 /* If we get this far, we know that n is large and s is word-aligned. */
141 aligned_addr = (CYG_WORD *)s;
143 /* Store C into each char sized location in BUFFER so that
144 * we can set large blocks quickly.
147 if (CYG_STR_OPT_LITTLEBLOCKSIZE == 4)
149 buffer = (c << 8) | c;
150 buffer |= (buffer << 16);
155 for (count = 0; count < CYG_STR_OPT_LITTLEBLOCKSIZE; count++)
156 buffer = (buffer << 8) | c;
159 while (n >= CYG_STR_OPT_LITTLEBLOCKSIZE*4)
161 *aligned_addr++ = buffer;
162 *aligned_addr++ = buffer;
163 *aligned_addr++ = buffer;
164 *aligned_addr++ = buffer;
165 n -= 4*CYG_STR_OPT_LITTLEBLOCKSIZE;
168 while (n >= CYG_STR_OPT_LITTLEBLOCKSIZE)
170 *aligned_addr++ = buffer;
171 n -= CYG_STR_OPT_LITTLEBLOCKSIZE;
174 /* Pick up the remainder with a bytewise loop. */
175 unaligned_addr = (char*)aligned_addr;
178 *unaligned_addr++ = (char)c;
182 #ifdef CYG_TRACING_FIXED
183 CYG_REPORT_RETVAL( s );
186 #endif /* not defined(CYGIMP_PREFER_SMALL_TO_FAST_MEMSET) ||
187 * defined(__OPTIMIZE_SIZE__) */