Back to index

lightning-sunbird  0.9+nobinonly
memmove.c
Go to the documentation of this file.
00001 #if defined(__sun) && !defined(__SVR4)
00002 /*-
00003  * Copyright (c) 1990, 1993
00004  *     The Regents of the University of California.  All rights reserved.
00005  *
00006  * This code is derived from software contributed to Berkeley by
00007  * Chris Torek.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer.
00014  * 2. Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in the
00016  *    documentation and/or other materials provided with the distribution.
00017  * 3. ***REMOVED*** - see 
00018  *    ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
00019  * 4. 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 #if defined(LIBC_SCCS) && !defined(lint)
00037 static char sccsid[] = "@(#)bcopy.c       8.1 (Berkeley) 6/4/93";
00038 #endif /* LIBC_SCCS and not lint */
00039 
00040 #include "watcomfx.h"
00041 
00042 #ifdef HAVE_SYS_CDEFS_H
00043 #include <sys/cdefs.h>
00044 #else
00045 #include "cdefs.h"
00046 #endif
00047 #include <string.h>
00048 
00049 /*
00050  * sizeof(word) MUST BE A POWER OF TWO
00051  * SO THAT wmask BELOW IS ALL ONES
00052  */
00053 typedef       int word;            /* "word" used for optimal copy speed */
00054 
00055 #define       wsize  sizeof(word)
00056 #define       wmask  (wsize - 1)
00057 
00058 /*
00059  * Copy a block of memory, handling overlap.
00060  * This is the routine that actually implements
00061  * (the portable versions of) bcopy, memcpy, and memmove.
00062  */
00063 #ifdef MEMCOPY
00064 void *
00065 memcpy(dst0, src0, length)
00066 #else
00067 #ifdef MEMMOVE
00068 void *
00069 memmove(dst0, src0, length)
00070 #else
00071 void
00072 bcopy(src0, dst0, length)
00073 #endif
00074 #endif
00075        void *dst0;
00076        const void *src0;
00077        register size_t length;
00078 {
00079        register char *dst = dst0;
00080        register const char *src = src0;
00081        register size_t t;
00082 
00083        if (length == 0 || dst == src)            /* nothing to do */
00084               goto done;
00085 
00086        /*
00087         * Macros: loop-t-times; and loop-t-times, t>0
00088         */
00089 #define       TLOOP(s) if (t) TLOOP1(s)
00090 #define       TLOOP1(s) do { s; } while (--t)
00091 
00092        if ((unsigned long)dst < (unsigned long)src) {
00093               /*
00094                * Copy forward.
00095                */
00096               t = (int)src; /* only need low bits */
00097               if ((t | (int)dst) & wmask) {
00098                      /*
00099                       * Try to align operands.  This cannot be done
00100                       * unless the low bits match.
00101                       */
00102                      if ((t ^ (int)dst) & wmask || length < wsize)
00103                             t = length;
00104                      else
00105                             t = wsize - (t & wmask);
00106                      length -= t;
00107                      TLOOP1(*dst++ = *src++);
00108               }
00109               /*
00110                * Copy whole words, then mop up any trailing bytes.
00111                */
00112               t = length / wsize;
00113               TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
00114               t = length & wmask;
00115               TLOOP(*dst++ = *src++);
00116        } else {
00117               /*
00118                * Copy backwards.  Otherwise essentially the same.
00119                * Alignment works as before, except that it takes
00120                * (t&wmask) bytes to align, not wsize-(t&wmask).
00121                */
00122               src += length;
00123               dst += length;
00124               t = (int)src;
00125               if ((t | (int)dst) & wmask) {
00126                      if ((t ^ (int)dst) & wmask || length <= wsize)
00127                             t = length;
00128                      else
00129                             t &= wmask;
00130                      length -= t;
00131                      TLOOP1(*--dst = *--src);
00132               }
00133               t = length / wsize;
00134               TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src);
00135               t = length & wmask;
00136               TLOOP(*--dst = *--src);
00137        }
00138 done:
00139 #if defined(MEMCOPY) || defined(MEMMOVE)
00140        return (dst0);
00141 #else
00142        return;
00143 #endif
00144 }
00145 #endif /* no __sgi */
00146 
00147 /* Some compilers don't like an empty source file. */
00148 static int dummy = 0;