fiq_debugger_ringbuf.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * arch/arm/common/fiq_debugger_ringbuf.c
  3. *
  4. * simple lockless ringbuffer
  5. *
  6. * Copyright (C) 2010 Google, Inc.
  7. *
  8. * This software is licensed under the terms of the GNU General Public
  9. * License version 2, as published by the Free Software Foundation, and
  10. * may be copied, distributed, and modified under those terms.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/slab.h>
  19. struct fiq_debugger_ringbuf {
  20. int len;
  21. int head;
  22. int tail;
  23. u8 buf[];
  24. };
  25. static inline struct fiq_debugger_ringbuf *fiq_debugger_ringbuf_alloc(int len)
  26. {
  27. struct fiq_debugger_ringbuf *rbuf;
  28. rbuf = kzalloc(sizeof(*rbuf) + len, GFP_KERNEL);
  29. if (rbuf == NULL)
  30. return NULL;
  31. rbuf->len = len;
  32. rbuf->head = 0;
  33. rbuf->tail = 0;
  34. smp_mb();
  35. return rbuf;
  36. }
  37. static inline void fiq_debugger_ringbuf_free(struct fiq_debugger_ringbuf *rbuf)
  38. {
  39. kfree(rbuf);
  40. }
  41. static inline int fiq_debugger_ringbuf_level(struct fiq_debugger_ringbuf *rbuf)
  42. {
  43. int level = rbuf->head - rbuf->tail;
  44. if (level < 0)
  45. level = rbuf->len + level;
  46. return level;
  47. }
  48. static inline int fiq_debugger_ringbuf_room(struct fiq_debugger_ringbuf *rbuf)
  49. {
  50. return rbuf->len - fiq_debugger_ringbuf_level(rbuf) - 1;
  51. }
  52. static inline u8
  53. fiq_debugger_ringbuf_peek(struct fiq_debugger_ringbuf *rbuf, int i)
  54. {
  55. return rbuf->buf[(rbuf->tail + i) % rbuf->len];
  56. }
  57. static inline int
  58. fiq_debugger_ringbuf_consume(struct fiq_debugger_ringbuf *rbuf, int count)
  59. {
  60. count = min(count, fiq_debugger_ringbuf_level(rbuf));
  61. rbuf->tail = (rbuf->tail + count) % rbuf->len;
  62. smp_mb();
  63. return count;
  64. }
  65. static inline int
  66. fiq_debugger_ringbuf_push(struct fiq_debugger_ringbuf *rbuf, u8 datum)
  67. {
  68. if (fiq_debugger_ringbuf_room(rbuf) == 0)
  69. return 0;
  70. rbuf->buf[rbuf->head] = datum;
  71. smp_mb();
  72. rbuf->head = (rbuf->head + 1) % rbuf->len;
  73. smp_mb();
  74. return 1;
  75. }