SDL_memmove.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_memmove
  20. #undef SDL_memmove
  21. #endif
  22. #if SDL_DYNAMIC_API
  23. #define SDL_memmove SDL_memmove_REAL
  24. #endif
  25. void *SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len)
  26. {
  27. #ifdef __GNUC__
  28. /* Presumably this is well tuned for speed. */
  29. return __builtin_memmove(dst, src, len);
  30. #elif defined(HAVE_MEMMOVE)
  31. return memmove(dst, src, len);
  32. #else
  33. char *srcp = (char *)src;
  34. char *dstp = (char *)dst;
  35. if (src < dst) {
  36. srcp += len - 1;
  37. dstp += len - 1;
  38. while (len--) {
  39. *dstp-- = *srcp--;
  40. }
  41. } else {
  42. while (len--) {
  43. *dstp++ = *srcp++;
  44. }
  45. }
  46. return dst;
  47. #endif /* HAVE_MEMMOVE */
  48. }