home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / string / memmove.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-23  |  3.2 KB  |  108 lines

  1. /* memmove -- copy memory to memory until the specified number of bytes
  2.    has been copied.  Overlap is handled correctly.
  3.    Copyright (C) 1991 Free Software Foundation, Inc.
  4.    Contributed by Torbjorn Granlund (tege@sics.se).
  5.  
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public License as
  8. published by the Free Software Foundation; either version 2 of the
  9. License, or (at your option) any later version.
  10.  
  11. The GNU C Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. Library General Public License for more details.
  15.  
  16. You should have received a copy of the GNU Library General Public
  17. License along with the GNU C Library; see the file COPYING.LIB.  If
  18. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  19. Cambridge, MA 02139, USA.  */
  20.  
  21. #include <ansidecl.h>
  22. #include <string.h>
  23. #include <memcopy.h>
  24.  
  25. /* All this is so that bcopy.c can #include
  26.    this file after defining some things.  */
  27. #ifndef    a1
  28. #define    a1    dest    /* First arg is DEST.  */
  29. #define    a1const
  30. #define    a2    src    /* Second arg is SRC.  */
  31. #define    a2const    CONST
  32. #endif
  33. #if    !defined(RETURN) || !defined(rettype)
  34. #define    RETURN(s)    return (s)    /* Return DEST.  */
  35. #define    rettype        PTR
  36. #endif
  37.  
  38. #ifdef isbcopy
  39. #define    size_t    int
  40. #endif
  41.  
  42. rettype
  43. DEFUN(memmove, (a1, a2, len),
  44.       a1const PTR a1 AND a2const PTR a2 AND size_t len)
  45. {
  46.   unsigned long int dstp = (long int) dest;
  47.   unsigned long int srcp = (long int) src;
  48.  
  49. #ifdef isbcopy
  50.   if (len <= 0) return;
  51. #endif
  52.  
  53.   /* This test makes the forward copying code be used whenever possible.
  54.      Reduces the working set.  */
  55.   if (dstp - srcp >= len)    /* *Unsigned* compare!  */
  56.     {
  57.       /* Copy from the beginning to the end.  */
  58.  
  59.       /* If there not too few bytes to copy, use word copy.  */
  60.       if (len >= OP_T_THRES)
  61.     {
  62.       /* Copy just a few bytes to make DSTP aligned.  */
  63.       len -= (-dstp) % OPSIZ;
  64.       BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
  65.  
  66.       /* Copy from SRCP to DSTP taking advantage of the known
  67.          alignment of DSTP.  Number of bytes remaining is put
  68.          in the third argumnet, i.e. in LEN.  This number may
  69.          vary from machine to machine.  */
  70.  
  71.       WORD_COPY_FWD (dstp, srcp, len, len);
  72.  
  73.       /* Fall out and copy the tail.  */
  74.     }
  75.  
  76.       /* There are just a few bytes to copy.  Use byte memory operations.  */
  77.       BYTE_COPY_FWD (dstp, srcp, len);
  78.     }
  79.   else
  80.     {
  81.       /* Copy from the end to the beginning.  */
  82.       srcp += len;
  83.       dstp += len;
  84.  
  85.       /* If there not too few bytes to copy, use word copy.  */
  86.       if (len >= OP_T_THRES)
  87.     {
  88.       /* Copy just a few bytes to make DSTP aligned.  */
  89.       len -= dstp % OPSIZ;
  90.       BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ);
  91.  
  92.       /* Copy from SRCP to DSTP taking advantage of the known
  93.          alignment of DSTP.  Number of bytes remaining is put
  94.          in the third argumnet, i.e. in LEN.  This number may
  95.          vary from machine to machine.  */
  96.  
  97.       WORD_COPY_BWD (dstp, srcp, len, len);
  98.  
  99.       /* Fall out and copy the tail.  */
  100.     }
  101.  
  102.       /* There are just a few bytes to copy.  Use byte memory operations.  */
  103.       BYTE_COPY_BWD (dstp, srcp, len);
  104.     }
  105.  
  106.   RETURN(dest);
  107. }
  108.