ring.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (C) 2006-2009 Free Software Foundation
  3. *
  4. * This program is free software ; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation ; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program 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
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with the program ; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <sys/types.h>
  19. #include <string.h>
  20. #include "ring.h"
  21. /* dest is ring */
  22. void hyp_ring_store(void *dest, const void *src, size_t size, void *start, void *end)
  23. {
  24. if (dest + size > end) {
  25. size_t first_size = end - dest;
  26. memcpy(dest, src, first_size);
  27. src += first_size;
  28. dest = start;
  29. size -= first_size;
  30. }
  31. memcpy(dest, src, size);
  32. }
  33. /* src is ring */
  34. void hyp_ring_fetch(void *dest, const void *src, size_t size, void *start, void *end)
  35. {
  36. if (src + size > end) {
  37. size_t first_size = end - src;
  38. memcpy(dest, src, first_size);
  39. dest += first_size;
  40. src = start;
  41. size -= first_size;
  42. }
  43. memcpy(dest, src, size);
  44. }
  45. size_t hyp_ring_next_word(char **c, void *start, void *end)
  46. {
  47. size_t n = 0;
  48. while (**c) {
  49. n++;
  50. if (++(*c) == end)
  51. *c = start;
  52. }
  53. (*c)++;
  54. return n;
  55. }