00001 /*- 00002 * See the file LICENSE for redistribution information. 00003 * 00004 * Copyright (c) 1996-2005 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 * $Id: memmove.c,v 12.1 2005/06/16 20:20:49 bostic Exp $ 00036 */ 00037 00038 #include "db_config.h" 00039 00040 #ifndef NO_SYSTEM_INCLUDES 00041 #include <sys/types.h> 00042 #endif 00043 00044 /* 00045 * sizeof(word) MUST BE A POWER OF TWO 00046 * SO THAT wmask BELOW IS ALL ONES 00047 */ 00048 typedef int word; /* "word" used for optimal copy speed */ 00049 00050 #undef wsize 00051 #define wsize sizeof(word) 00052 #undef wmask 00053 #define wmask (wsize - 1) 00054 00055 /* 00056 * Copy a block of memory, handling overlap. 00057 * This is the routine that actually implements 00058 * (the portable versions of) bcopy, memcpy, and memmove. 00059 */ 00060 #ifdef MEMCOPY 00061 /* 00062 * PUBLIC: #ifndef HAVE_MEMCPY 00063 * PUBLIC: void *memcpy __P((void *, const void *, size_t)); 00064 * PUBLIC: #endif 00065 */ 00066 void * 00067 memcpy(dst0, src0, length) 00068 #else 00069 #ifdef MEMMOVE 00070 /* 00071 * PUBLIC: #ifndef HAVE_MEMMOVE 00072 * PUBLIC: void *memmove __P((void *, const void *, size_t)); 00073 * PUBLIC: #endif 00074 */ 00075 void * 00076 memmove(dst0, src0, length) 00077 #else 00078 void 00079 bcopy(src0, dst0, length) 00080 #endif 00081 #endif 00082 void *dst0; 00083 const void *src0; 00084 register size_t length; 00085 { 00086 register char *dst = dst0; 00087 register const char *src = src0; 00088 register size_t t; 00089 00090 if (length == 0 || dst == src) /* nothing to do */ 00091 goto done; 00092 00093 /* 00094 * Macros: loop-t-times; and loop-t-times, t>0 00095 */ 00096 #undef TLOOP 00097 #define TLOOP(s) if (t) TLOOP1(s) 00098 #undef TLOOP1 00099 #define TLOOP1(s) do { s; } while (--t) 00100 00101 if ((unsigned long)dst < (unsigned long)src) { 00102 /* 00103 * Copy forward. 00104 */ 00105 t = (int)src; /* only need low bits */ 00106 if ((t | (int)dst) & wmask) { 00107 /* 00108 * Try to align operands. This cannot be done 00109 * unless the low bits match. 00110 */ 00111 if ((t ^ (int)dst) & wmask || length < wsize) 00112 t = length; 00113 else 00114 t = wsize - (t & wmask); 00115 length -= t; 00116 TLOOP1(*dst++ = *src++); 00117 } 00118 /* 00119 * Copy whole words, then mop up any trailing bytes. 00120 */ 00121 t = length / wsize; 00122 TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize); 00123 t = length & wmask; 00124 TLOOP(*dst++ = *src++); 00125 } else { 00126 /* 00127 * Copy backwards. Otherwise essentially the same. 00128 * Alignment works as before, except that it takes 00129 * (t&wmask) bytes to align, not wsize-(t&wmask). 00130 */ 00131 src += length; 00132 dst += length; 00133 t = (int)src; 00134 if ((t | (int)dst) & wmask) { 00135 if ((t ^ (int)dst) & wmask || length <= wsize) 00136 t = length; 00137 else 00138 t &= wmask; 00139 length -= t; 00140 TLOOP1(*--dst = *--src); 00141 } 00142 t = length / wsize; 00143 TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src); 00144 t = length & wmask; 00145 TLOOP(*--dst = *--src); 00146 } 00147 done: 00148 #if defined(MEMCOPY) || defined(MEMMOVE) 00149 return (dst0); 00150 #else 00151 return; 00152 #endif 00153 }