memcpy.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Copy memory to memory until the specified number of bytes
  2. has been copied. Overlap is NOT handled correctly.
  3. Copyright (C) 1991, 1997, 2003 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Torbjorn Granlund (tege@sics.se).
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, write to the Free
  16. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  17. 02111-1307 USA. */
  18. #include "api.h"
  19. #include <sysdeps/generic/memcopy.h>
  20. void * memcpy (void *dstpp, const void *srcpp, size_t len)
  21. {
  22. unsigned long int dstp = (long int) dstpp;
  23. unsigned long int srcp = (long int) srcpp;
  24. /* Copy from the beginning to the end. */
  25. /* If there not too few bytes to copy, use word copy. */
  26. if (len >= OP_T_THRES) {
  27. /* Copy just a few bytes to make DSTP aligned. */
  28. len -= (-dstp) % OPSIZ;
  29. BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
  30. /* Copy from SRCP to DSTP taking advantage of the known alignment of
  31. DSTP. Number of bytes remaining is put in the third argument,
  32. i.e. in LEN. This number may vary from machine to machine. */
  33. WORD_COPY_FWD (dstp, srcp, len, len);
  34. /* Fall out and copy the tail. */
  35. }
  36. /* There are just a few bytes to copy. Use byte memory operations. */
  37. BYTE_COPY_FWD (dstp, srcp, len);
  38. return dstpp;
  39. }
  40. void * memmove (void * destpp, const void * srcpp, size_t len)
  41. {
  42. unsigned long int dstp = (long int) destpp;
  43. unsigned long int srcp = (long int) srcpp;
  44. /* This test makes the forward copying code be used whenever possible.
  45. Reduces the working set. */
  46. if (dstp - srcp >= len) { /* *Unsigned* compare! */
  47. /* Copy from the beginning to the end. */
  48. /* If there not too few bytes to copy, use word copy. */
  49. if (len >= OP_T_THRES) {
  50. /* Copy just a few bytes to make DSTP aligned. */
  51. len -= (-dstp) % OPSIZ;
  52. BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
  53. /* Copy from SRCP to DSTP taking advantage of the known
  54. alignment of DSTP. Number of bytes remaining is put
  55. in the third argument, i.e. in LEN. This number may
  56. vary from machine to machine. */
  57. WORD_COPY_FWD (dstp, srcp, len, len);
  58. /* Fall out and copy the tail. */
  59. }
  60. /* There are just a few bytes to copy. Use byte memory operations. */
  61. BYTE_COPY_FWD (dstp, srcp, len);
  62. } else {
  63. /* Copy from the end to the beginning. */
  64. srcp += len;
  65. dstp += len;
  66. /* If there not too few bytes to copy, use word copy. */
  67. if (len >= OP_T_THRES) {
  68. /* Copy just a few bytes to make DSTP aligned. */
  69. len -= dstp % OPSIZ;
  70. BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ);
  71. /* Copy from SRCP to DSTP taking advantage of the known
  72. alignment of DSTP. Number of bytes remaining is put
  73. in the third argument, i.e. in LEN. This number may
  74. vary from machine to machine. */
  75. WORD_COPY_BWD (dstp, srcp, len, len);
  76. /* Fall out and copy the tail. */
  77. }
  78. /* There are just a few bytes to copy. Use byte memory operations. */
  79. BYTE_COPY_BWD (dstp, srcp, len);
  80. }
  81. return destpp;
  82. }