home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / dbm / src / memmove.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.4 KB  |  148 lines

  1. #if defined(__sun) && !defined(__svr4__)
  2. /*-
  3.  * Copyright (c) 1990, 1993
  4.  *    The Regents of the University of California.  All rights reserved.
  5.  *
  6.  * This code is derived from software contributed to Berkeley by
  7.  * Chris Torek.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37.  
  38. #if defined(LIBC_SCCS) && !defined(lint)
  39. static char sccsid[] = "@(#)bcopy.c    8.1 (Berkeley) 6/4/93";
  40. #endif /* LIBC_SCCS and not lint */
  41.  
  42. #include "watcomfx.h"
  43.  
  44. #ifndef _WINDOWS
  45. #include <sys/cdefs.h>
  46. #else
  47. #include "cdefs.h"
  48. #endif
  49. #include <string.h>
  50.  
  51. /*
  52.  * sizeof(word) MUST BE A POWER OF TWO
  53.  * SO THAT wmask BELOW IS ALL ONES
  54.  */
  55. typedef    int word;        /* "word" used for optimal copy speed */
  56.  
  57. #define    wsize    sizeof(word)
  58. #define    wmask    (wsize - 1)
  59.  
  60. /*
  61.  * Copy a block of memory, handling overlap.
  62.  * This is the routine that actually implements
  63.  * (the portable versions of) bcopy, memcpy, and memmove.
  64.  */
  65. #ifdef MEMCOPY
  66. void *
  67. memcpy(dst0, src0, length)
  68. #else
  69. #ifdef MEMMOVE
  70. void *
  71. memmove(dst0, src0, length)
  72. #else
  73. void
  74. bcopy(src0, dst0, length)
  75. #endif
  76. #endif
  77.     void *dst0;
  78.     const void *src0;
  79.     register size_t length;
  80. {
  81.     register char *dst = dst0;
  82.     register const char *src = src0;
  83.     register size_t t;
  84.  
  85.     if (length == 0 || dst == src)        /* nothing to do */
  86.         goto done;
  87.  
  88.     /*
  89.      * Macros: loop-t-times; and loop-t-times, t>0
  90.      */
  91. #define    TLOOP(s) if (t) TLOOP1(s)
  92. #define    TLOOP1(s) do { s; } while (--t)
  93.  
  94.     if ((unsigned long)dst < (unsigned long)src) {
  95.         /*
  96.          * Copy forward.
  97.          */
  98.         t = (int)src;    /* only need low bits */
  99.         if ((t | (int)dst) & wmask) {
  100.             /*
  101.              * Try to align operands.  This cannot be done
  102.              * unless the low bits match.
  103.              */
  104.             if ((t ^ (int)dst) & wmask || length < wsize)
  105.                 t = length;
  106.             else
  107.                 t = wsize - (t & wmask);
  108.             length -= t;
  109.             TLOOP1(*dst++ = *src++);
  110.         }
  111.         /*
  112.          * Copy whole words, then mop up any trailing bytes.
  113.          */
  114.         t = length / wsize;
  115.         TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
  116.         t = length & wmask;
  117.         TLOOP(*dst++ = *src++);
  118.     } else {
  119.         /*
  120.          * Copy backwards.  Otherwise essentially the same.
  121.          * Alignment works as before, except that it takes
  122.          * (t&wmask) bytes to align, not wsize-(t&wmask).
  123.          */
  124.         src += length;
  125.         dst += length;
  126.         t = (int)src;
  127.         if ((t | (int)dst) & wmask) {
  128.             if ((t ^ (int)dst) & wmask || length <= wsize)
  129.                 t = length;
  130.             else
  131.                 t &= wmask;
  132.             length -= t;
  133.             TLOOP1(*--dst = *--src);
  134.         }
  135.         t = length / wsize;
  136.         TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src);
  137.         t = length & wmask;
  138.         TLOOP(*--dst = *--src);
  139.     }
  140. done:
  141. #if defined(MEMCOPY) || defined(MEMMOVE)
  142.     return (dst0);
  143. #else
  144.     return;
  145. #endif
  146. }
  147. #endif /* no __sgi */
  148.