memcpy2.c

Go to the documentation of this file.
00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996, 1997, 1998, 1999
00005  *      Sleepycat Software.  All rights reserved.
00006  */
00007 /*
00008  * Copyright (c) 1990, 1993
00009  *      The Regents of the University of California.  All rights reserved.
00010  *
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  * 1. Redistributions of source code must retain the above copyright
00015  *    notice, this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright
00017  *    notice, this list of conditions and the following disclaimer in the
00018  *    documentation and/or other materials provided with the distribution.
00019  * 3. Neither the name of the University nor the names of its contributors
00020  *    may be used to endorse or promote products derived from this software
00021  *    without specific prior written permission.
00022  *
00023  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00024  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00027  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00028  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00029  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00032  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00033  * SUCH DAMAGE.
00034  */
00035 
00036 #ifdef HAVE_CONFIG_H
00037 #include "config.h"
00038 #endif /* HAVE_CONFIG_H */
00039 
00040 #ifndef NO_SYSTEM_INCLUDES
00041 #include <sys/types.h>
00042 #endif
00043 
00044 #ifndef HAVE_MEMCPY
00045 
00046 /*
00047  * sizeof(word) MUST BE A POWER OF TWO
00048  * SO THAT wmask BELOW IS ALL ONES
00049  */
00050 typedef int word;               /* "word" used for optimal copy speed */
00051 
00052 #undef  wsize
00053 #define wsize   sizeof(word)
00054 #undef  wmask
00055 #define wmask   (wsize - 1)
00056 
00057 /*
00058  * Copy a block of memory, handling overlap.
00059  * This is the routine that actually implements
00060  * (the portable versions of) bcopy, memcpy, and memmove.
00061  */
00062 /*
00063  * PUBLIC: #ifndef HAVE_MEMCPY
00064  * PUBLIC: void *memcpy __P((void *, const void *, size_t));
00065  * PUBLIC: #endif
00066  */
00067 void *
00068 memcpy(dst0, src0, length)
00069         void *dst0;
00070         const void *src0;
00071         register size_t length;
00072 {
00073         register char *dst = dst0;
00074         register const char *src = src0;
00075         register size_t t;
00076 
00077         if (length == 0 || dst == src)          /* nothing to do */
00078                 goto done;
00079 
00080         /*
00081          * Macros: loop-t-times; and loop-t-times, t>0
00082          */
00083 #undef  TLOOP
00084 #define TLOOP(s) if (t) TLOOP1(s)
00085 #undef  TLOOP1
00086 #define TLOOP1(s) do { s; } while (--t)
00087 
00088         if ((unsigned long)dst < (unsigned long)src) {
00089                 /*
00090                  * Copy forward.
00091                  */
00092                 t = (int)src;   /* only need low bits */
00093                 if ((t | (int)dst) & wmask) {
00094                         /*
00095                          * Try to align operands.  This cannot be done
00096                          * unless the low bits match.
00097                          */
00098                         if ((t ^ (int)dst) & wmask || length < wsize)
00099                                 t = length;
00100                         else
00101                                 t = wsize - (t & wmask);
00102                         length -= t;
00103                         TLOOP1(*dst++ = *src++);
00104                 }
00105                 /*
00106                  * Copy whole words, then mop up any trailing bytes.
00107                  */
00108                 t = length / wsize;
00109                 TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
00110                 t = length & wmask;
00111                 TLOOP(*dst++ = *src++);
00112         } else {
00113                 /*
00114                  * Copy backwards.  Otherwise essentially the same.
00115                  * Alignment works as before, except that it takes
00116                  * (t&wmask) bytes to align, not wsize-(t&wmask).
00117                  */
00118                 src += length;
00119                 dst += length;
00120                 t = (int)src;
00121                 if ((t | (int)dst) & wmask) {
00122                         if ((t ^ (int)dst) & wmask || length <= wsize)
00123                                 t = length;
00124                         else
00125                                 t &= wmask;
00126                         length -= t;
00127                         TLOOP1(*--dst = *--src);
00128                 }
00129                 t = length / wsize;
00130                 TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src);
00131                 t = length & wmask;
00132                 TLOOP(*--dst = *--src);
00133         }
00134 done:
00135         return (dst0);
00136 }
00137 
00138 #endif /* HAVE_MEMCPY */

Generated on Sun Jun 8 10:56:34 2008 for GNUmifluz by  doxygen 1.5.5