ptr_ring.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #define _GNU_SOURCE
  2. #include "main.h"
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <pthread.h>
  7. #include <malloc.h>
  8. #include <assert.h>
  9. #include <errno.h>
  10. #include <limits.h>
  11. #define SMP_CACHE_BYTES 64
  12. #define cache_line_size() SMP_CACHE_BYTES
  13. #define ____cacheline_aligned_in_smp __attribute__ ((aligned (SMP_CACHE_BYTES)))
  14. #define unlikely(x) (__builtin_expect(!!(x), 0))
  15. #define likely(x) (__builtin_expect(!!(x), 1))
  16. #define ALIGN(x, a) (((x) + (a) - 1) / (a) * (a))
  17. typedef pthread_spinlock_t spinlock_t;
  18. typedef int gfp_t;
  19. static void *kmalloc(unsigned size, gfp_t gfp)
  20. {
  21. return memalign(64, size);
  22. }
  23. static void *kzalloc(unsigned size, gfp_t gfp)
  24. {
  25. void *p = memalign(64, size);
  26. if (!p)
  27. return p;
  28. memset(p, 0, size);
  29. return p;
  30. }
  31. static void kfree(void *p)
  32. {
  33. if (p)
  34. free(p);
  35. }
  36. static void spin_lock_init(spinlock_t *lock)
  37. {
  38. int r = pthread_spin_init(lock, 0);
  39. assert(!r);
  40. }
  41. static void spin_lock(spinlock_t *lock)
  42. {
  43. int ret = pthread_spin_lock(lock);
  44. assert(!ret);
  45. }
  46. static void spin_unlock(spinlock_t *lock)
  47. {
  48. int ret = pthread_spin_unlock(lock);
  49. assert(!ret);
  50. }
  51. static void spin_lock_bh(spinlock_t *lock)
  52. {
  53. spin_lock(lock);
  54. }
  55. static void spin_unlock_bh(spinlock_t *lock)
  56. {
  57. spin_unlock(lock);
  58. }
  59. static void spin_lock_irq(spinlock_t *lock)
  60. {
  61. spin_lock(lock);
  62. }
  63. static void spin_unlock_irq(spinlock_t *lock)
  64. {
  65. spin_unlock(lock);
  66. }
  67. static void spin_lock_irqsave(spinlock_t *lock, unsigned long f)
  68. {
  69. spin_lock(lock);
  70. }
  71. static void spin_unlock_irqrestore(spinlock_t *lock, unsigned long f)
  72. {
  73. spin_unlock(lock);
  74. }
  75. #include "../../../include/linux/ptr_ring.h"
  76. static unsigned long long headcnt, tailcnt;
  77. static struct ptr_ring array ____cacheline_aligned_in_smp;
  78. /* implemented by ring */
  79. void alloc_ring(void)
  80. {
  81. int ret = ptr_ring_init(&array, ring_size, 0);
  82. assert(!ret);
  83. }
  84. /* guest side */
  85. int add_inbuf(unsigned len, void *buf, void *datap)
  86. {
  87. int ret;
  88. ret = __ptr_ring_produce(&array, buf);
  89. if (ret >= 0) {
  90. ret = 0;
  91. headcnt++;
  92. }
  93. return ret;
  94. }
  95. /*
  96. * ptr_ring API provides no way for producer to find out whether a given
  97. * buffer was consumed. Our tests merely require that a successful get_buf
  98. * implies that add_inbuf succeed in the past, and that add_inbuf will succeed,
  99. * fake it accordingly.
  100. */
  101. void *get_buf(unsigned *lenp, void **bufp)
  102. {
  103. void *datap;
  104. if (tailcnt == headcnt || __ptr_ring_full(&array))
  105. datap = NULL;
  106. else {
  107. datap = "Buffer\n";
  108. ++tailcnt;
  109. }
  110. return datap;
  111. }
  112. bool used_empty()
  113. {
  114. return (tailcnt == headcnt || __ptr_ring_full(&array));
  115. }
  116. void disable_call()
  117. {
  118. assert(0);
  119. }
  120. bool enable_call()
  121. {
  122. assert(0);
  123. }
  124. void kick_available(void)
  125. {
  126. assert(0);
  127. }
  128. /* host side */
  129. void disable_kick()
  130. {
  131. assert(0);
  132. }
  133. bool enable_kick()
  134. {
  135. assert(0);
  136. }
  137. bool avail_empty()
  138. {
  139. return !__ptr_ring_peek(&array);
  140. }
  141. bool use_buf(unsigned *lenp, void **bufp)
  142. {
  143. void *ptr;
  144. ptr = __ptr_ring_consume(&array);
  145. return ptr;
  146. }
  147. void call_used(void)
  148. {
  149. assert(0);
  150. }