1 /*===========================================================================
5 // ANSI standard memcpy() 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 memcpy() function
47 // Description: This file implements the memcpy() function defined in ANSI para
48 // 7.11.2.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 either X or Y is not aligned on a word boundary. */
69 #define CYG_STR_UNALIGNED(X, Y) \
70 (((CYG_WORD)(X) & (sizeof (CYG_WORD) - 1)) | \
71 ((CYG_WORD)(Y) & (sizeof (CYG_WORD) - 1)))
73 /* How many bytes are copied each iteration of the 4X unrolled loop in the
74 * optimised string implementation
76 #define CYG_STR_OPT_BIGBLOCKSIZE (sizeof(CYG_WORD) << 2)
79 /* How many bytes are copied each iteration of the word copy loop in the
80 * optimised string implementation
82 #define CYG_STR_OPT_LITTLEBLOCKSIZE (sizeof (CYG_WORD))
84 /* EXPORTED SYMBOLS */
87 memcpy( void *s1, const void *s2, size_t n ) __attribute__((weak,
93 _memcpy( void *s1, const void *s2, size_t n )
95 #if defined(CYGIMP_INFRA_PREFER_SMALL_TO_FAST_MEMCPY) || defined(__OPTIMIZE_SIZE__)
96 char *dst = (char *) s1;
97 const char *src = (const char *) s2;
99 #ifdef CYG_TRACING_FIXED
100 CYG_REPORT_FUNCNAMETYPE( "_memcpy", "returning %08x" );
101 CYG_REPORT_FUNCARG3( "dst=%08x, src=%08x, n=%d", dst, src, n );
105 CYG_CHECK_DATA_PTR( dst, "dst is not a valid pointer!" );
106 CYG_CHECK_DATA_PTR( src, "src is not a valid pointer!" );
107 CYG_CHECK_DATA_PTR( dst+n-1, "dst+n-1 is not a valid address!" );
108 CYG_CHECK_DATA_PTR( src+n-1, "src+n-1 is not a valid address!" );
117 #ifdef CYG_TRACING_FIXED
118 CYG_REPORT_RETVAL( s1 );
124 CYG_WORD *aligned_dst;
125 const CYG_WORD *aligned_src;
127 #ifdef CYG_TRACING_FIXED
128 CYG_REPORT_FUNCNAMETYPE( "_memcpy", "returning %08x" );
132 src = (const char *)s2;
134 #ifdef CYG_TRACING_FIXED
135 CYG_REPORT_FUNCARG3( "dst=%08x, src=%08x, n=%d", dst, src, n );
139 CYG_CHECK_DATA_PTR( dst, "dst is not a valid pointer!" );
140 CYG_CHECK_DATA_PTR( src, "src is not a valid pointer!" );
141 CYG_CHECK_DATA_PTR( dst+n-1, "dst+n-1 is not a valid address!" );
142 CYG_CHECK_DATA_PTR( src+n-1, "src+n-1 is not a valid address!" );
146 /* If the size is small, or either SRC or DST is unaligned,
147 * then punt into the byte copy loop. This should be rare.
149 if (n < sizeof(CYG_WORD) || CYG_STR_UNALIGNED (src, dst))
153 #ifdef CYG_TRACING_FIXED
154 CYG_REPORT_RETVAL( s1 );
159 aligned_dst = (CYG_WORD *)dst;
160 aligned_src = (const CYG_WORD *)src;
162 /* Copy 4X long words at a time if possible. */
163 while (n >= CYG_STR_OPT_BIGBLOCKSIZE)
165 *aligned_dst++ = *aligned_src++;
166 *aligned_dst++ = *aligned_src++;
167 *aligned_dst++ = *aligned_src++;
168 *aligned_dst++ = *aligned_src++;
169 n -= CYG_STR_OPT_BIGBLOCKSIZE;
172 /* Copy one long word at a time if possible. */
173 while (n >= CYG_STR_OPT_LITTLEBLOCKSIZE)
175 *aligned_dst++ = *aligned_src++;
176 n -= CYG_STR_OPT_LITTLEBLOCKSIZE;
179 /* Pick up any residual with a byte copier. */
180 dst = (char*)aligned_dst;
181 src = (const char*)aligned_src;
185 #ifdef CYG_TRACING_FIXED
186 CYG_REPORT_RETVAL( s1 );
189 #endif /* not defined(CYGIMP_PREFER_SMALL_TO_FAST_MEMCPY) ||
190 * defined(__OPTIMIZE_SIZE__) */