memmove.c 3.4 KB

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