home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / memmove.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  82 lines

  1. /***
  2. *memmove.c - contains memmove routine
  3. *
  4. *       Copyright (c) 1988-1997, Microsoft Corporation. All right reserved.
  5. *
  6. *Purpose:
  7. *       memmove() copies a source memory buffer to a destination buffer.
  8. *       Overlapping buffers are treated specially, to avoid propogation.
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <string.h>
  14.  
  15. #if defined (_M_ALPHA)
  16. #pragma function(memmove)
  17. #endif  /* defined (_M_ALPHA) */
  18.  
  19. /***
  20. *memmove - Copy source buffer to destination buffer
  21. *
  22. *Purpose:
  23. *       memmove() copies a source memory buffer to a destination memory buffer.
  24. *       This routine recognize overlapping buffers to avoid propogation.
  25. *       For cases where propogation is not a problem, memcpy() can be used.
  26. *
  27. *Entry:
  28. *       void *dst = pointer to destination buffer
  29. *       const void *src = pointer to source buffer
  30. *       size_t count = number of bytes to copy
  31. *
  32. *Exit:
  33. *       Returns a pointer to the destination buffer
  34. *
  35. *Exceptions:
  36. *******************************************************************************/
  37.  
  38. void * __cdecl memmove (
  39.         void * dst,
  40.         const void * src,
  41.         size_t count
  42.         )
  43. {
  44.         void * ret = dst;
  45.  
  46. #if defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC)
  47.         {
  48.         extern void RtlMoveMemory( void *, const void *, size_t count );
  49.  
  50.         RtlMoveMemory( dst, src, count );
  51.         }
  52. #else  /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) */
  53.         if (dst <= src || (char *)dst >= ((char *)src + count)) {
  54.                 /*
  55.                  * Non-Overlapping Buffers
  56.                  * copy from lower addresses to higher addresses
  57.                  */
  58.                 while (count--) {
  59.                         *(char *)dst = *(char *)src;
  60.                         dst = (char *)dst + 1;
  61.                         src = (char *)src + 1;
  62.                 }
  63.         }
  64.         else {
  65.                 /*
  66.                  * Overlapping Buffers
  67.                  * copy from higher addresses to lower addresses
  68.                  */
  69.                 dst = (char *)dst + count - 1;
  70.                 src = (char *)src + count - 1;
  71.  
  72.                 while (count--) {
  73.                         *(char *)dst = *(char *)src;
  74.                         dst = (char *)dst - 1;
  75.                         src = (char *)src - 1;
  76.                 }
  77.         }
  78. #endif  /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) */
  79.  
  80.         return(ret);
  81. }
  82.