clib

memmove2.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_MEMMOVE
00045 /*
00046  * sizeof(word) MUST BE A POWER OF TWO
00047  * SO THAT wmask BELOW IS ALL ONES
00048  */
00049 typedef int word; /* "word" used for optimal copy speed */
00050 
00051 #undef wsize
00052 #define wsize sizeof(word)
00053 #undef wmask
00054 #define wmask (wsize - 1)
00055 
00056 /*
00057  * Copy a block of memory, handling overlap.
00058  * This is the routine that actually implements
00059  * (the portable versions of) bcopy, memcpy, and memmove.
00060  */
00061 /*
00062  * PUBLIC: #ifndef HAVE_MEMMOVE
00063  * PUBLIC: void *memmove __P((void *, const void *, size_t));
00064  * PUBLIC: #endif
00065  */
00066 void *
00067 memmove(dst0, src0, length)
00068 void *dst0;
00069 const void *src0;
00070 register size_t length;
00071 {
00072 register char *dst = dst0;
00073 register const char *src = src0;
00074 register size_t t;
00075 
00076 if (length == 0 || dst == src) /* nothing to do */
00077 goto done;
00078 
00079 /*
00080  * Macros: loop-t-times; and loop-t-times, t>0
00081  */
00082 #undef TLOOP
00083 #define TLOOP(s) if (t) TLOOP1(s)
00084 #undef TLOOP1
00085 #define TLOOP1(s) do { s; } while (--t)
00086 
00087 if ((unsigned long)dst < (unsigned long)src) {
00088 /*
00089  * Copy forward.
00090  */
00091 t = (int)src; /* only need low bits */
00092 if ((t | (int)dst) & wmask) {
00093 /*
00094  * Try to align operands. This cannot be done
00095  * unless the low bits match.
00096  */
00097 if ((t ^ (int)dst) & wmask || length < wsize)
00098 t = length;
00099 else
00100 t = wsize - (t & wmask);
00101 length -= t;
00102 TLOOP1(*dst++ = *src++);
00103 }
00104 /*
00105  * Copy whole words, then mop up any trailing bytes.
00106  */
00107 t = length / wsize;
00108 TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
00109 t = length & wmask;
00110 TLOOP(*dst++ = *src++);
00111 } else {
00112 /*
00113  * Copy backwards. Otherwise essentially the same.
00114  * Alignment works as before, except that it takes
00115  * (t&wmask) bytes to align, not wsize-(t&wmask).
00116  */
00117 src += length;
00118 dst += length;
00119 t = (int)src;
00120 if ((t | (int)dst) & wmask) {
00121 if ((t ^ (int)dst) & wmask || length <= wsize)
00122 t = length;
00123 else
00124 t &= wmask;
00125 length -= t;
00126 TLOOP1(*--dst = *--src);
00127 }
00128 t = length / wsize;
00129 TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src);
00130 t = length & wmask;
00131 TLOOP(*--dst = *--src);
00132 }
00133 done:
00134 return (dst0);
00135 }
00136 #endif /* HAVE_MEMOVE */

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

AltStyle によって変換されたページ (->オリジナル) /