You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.1 KiB

  1. /* Public domain. */
  2. #include <stddef.h>
  3. /*
  4. ** Copyright 2001, Travis Geiselbrecht. All rights reserved.
  5. ** Distributed under the terms of the NewOS License.
  6. */
  7. /*
  8. * Copyright (c) 2008 Travis Geiselbrecht
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining
  11. * a copy of this software and associated documentation files
  12. * (the "Software"), to deal in the Software without restriction,
  13. * including without limitation the rights to use, copy, modify, merge,
  14. * publish, distribute, sublicense, and/or sell copies of the Software,
  15. * and to permit persons to whom the Software is furnished to do so,
  16. * subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be
  19. * included in all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  24. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  25. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  26. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  27. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. */
  29. #include <string.h>
  30. #include <sys/types.h>
  31. typedef long word;
  32. #define lsize sizeof(word)
  33. #define lmask (lsize - 1)
  34. void *memcpy(void *dest, const void *src, size_t count)
  35. {
  36. char *d = (char *)dest;
  37. const char *s = (const char *)src;
  38. int len;
  39. if(count == 0 || dest == src)
  40. return dest;
  41. if(((long)d | (long)s) & lmask) {
  42. // src and/or dest do not align on word boundary
  43. if((((long)d ^ (long)s) & lmask) || (count < lsize))
  44. len = count; // copy the rest of the buffer with the byte mover
  45. else
  46. len = lsize - ((long)d & lmask); // move the ptrs up to a word boundary
  47. count -= len;
  48. for(; len > 0; len--)
  49. *d++ = *s++;
  50. }
  51. for(len = count / lsize; len > 0; len--) {
  52. *(word *)d = *(word *)s;
  53. d += lsize;
  54. s += lsize;
  55. }
  56. for(len = count & lmask; len > 0; len--)
  57. *d++ = *s++;
  58. return dest;
  59. }