memmove.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Copy memory to memory until the specified number of bytes
  2. has been copied. Overlap is handled correctly.
  3. Copyright (C) 1991-2019 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, see
  16. <http://www.gnu.org/licenses/>. */
  17. #include <string.h>
  18. #include <memcopy.h>
  19. /* All this is so that bcopy.c can #include
  20. this file after defining some things. */
  21. #ifndef a1
  22. #define a1 dest /* First arg is DEST. */
  23. #define a1const
  24. #define a2 src /* Second arg is SRC. */
  25. #define a2const const
  26. #undef memmove
  27. #endif
  28. #if !defined(RETURN) || !defined(rettype)
  29. #define RETURN(s) return (s) /* Return DEST. */
  30. #define rettype void *
  31. #endif
  32. #ifndef MEMMOVE
  33. #define MEMMOVE memmove
  34. #endif
  35. rettype
  36. inhibit_loop_to_libcall
  37. MEMMOVE (a1const void *a1, a2const void *a2, size_t len)
  38. {
  39. unsigned long int dstp = (long int) dest;
  40. unsigned long int srcp = (long int) src;
  41. /* This test makes the forward copying code be used whenever possible.
  42. Reduces the working set. */
  43. if (dstp - srcp >= len) /* *Unsigned* compare! */
  44. {
  45. /* Copy from the beginning to the end. */
  46. #if MEMCPY_OK_FOR_FWD_MEMMOVE
  47. dest = memcpy (dest, src, len);
  48. #else
  49. /* If there not too few bytes to copy, use word copy. */
  50. if (len >= OP_T_THRES)
  51. {
  52. /* Copy just a few bytes to make DSTP aligned. */
  53. len -= (-dstp) % OPSIZ;
  54. BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
  55. /* Copy whole pages from SRCP to DSTP by virtual address
  56. manipulation, as much as possible. */
  57. PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len);
  58. /* Copy from SRCP to DSTP taking advantage of the known
  59. alignment of DSTP. Number of bytes remaining is put
  60. in the third argument, i.e. in LEN. This number may
  61. vary from machine to machine. */
  62. WORD_COPY_FWD (dstp, srcp, len, len);
  63. /* Fall out and copy the tail. */
  64. }
  65. /* There are just a few bytes to copy. Use byte memory operations. */
  66. BYTE_COPY_FWD (dstp, srcp, len);
  67. #endif /* MEMCPY_OK_FOR_FWD_MEMMOVE */
  68. }
  69. else
  70. {
  71. /* Copy from the end to the beginning. */
  72. srcp += len;
  73. dstp += len;
  74. /* If there not too few bytes to copy, use word copy. */
  75. if (len >= OP_T_THRES)
  76. {
  77. /* Copy just a few bytes to make DSTP aligned. */
  78. len -= dstp % OPSIZ;
  79. BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ);
  80. /* Copy from SRCP to DSTP taking advantage of the known
  81. alignment of DSTP. Number of bytes remaining is put
  82. in the third argument, i.e. in LEN. This number may
  83. vary from machine to machine. */
  84. WORD_COPY_BWD (dstp, srcp, len, len);
  85. /* Fall out and copy the tail. */
  86. }
  87. /* There are just a few bytes to copy. Use byte memory operations. */
  88. BYTE_COPY_BWD (dstp, srcp, len);
  89. }
  90. RETURN (dest);
  91. }
  92. #ifndef memmove
  93. libc_hidden_builtin_def (memmove)
  94. #endif