queue.h 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #ifndef __QUEUE_H
  2. #define __QUEUE_H
  3. typedef enum
  4. {
  5. Q_OK,
  6. Q_ERR,
  7. Q_FULL,
  8. Q_EMPTY,
  9. } QUEUE_STATUS;
  10. #define queue_entry(type, size) \
  11. struct \
  12. { \
  13. uint8_t head; \
  14. uint8_t tail; \
  15. uint8_t count; \
  16. type message[size]; \
  17. }
  18. #define queue_init(name) \
  19. { \
  20. (name)->head = (name)->tail = 0; \
  21. (name)->count = 0; \
  22. }
  23. /* List functions. */
  24. #define queue_empty(name) ((name)->count == 0)
  25. #define queue_max(name) ((sizeof((name)->message)) / (sizeof((name)->message[0])))
  26. #define queue_full(name) \
  27. ((name)->count == ((sizeof((name)->message)) / (sizeof((name)->message[0]))))
  28. #define en_queue(name, field, ret) \
  29. do \
  30. { \
  31. if (queue_full(name)) \
  32. { \
  33. ret = Q_FULL; \
  34. } \
  35. else \
  36. { \
  37. (name)->count++; \
  38. (name)->message[(name)->head] = field; \
  39. (name)->head = ((name)->head + 1) % queue_max(name); \
  40. ret = Q_OK; \
  41. } \
  42. } while (0)
  43. #define de_queue(name, field, ret) \
  44. do \
  45. { \
  46. if (queue_empty(name)) \
  47. { \
  48. ret = Q_EMPTY; \
  49. } \
  50. else \
  51. { \
  52. field = (name)->message[(name)->tail]; \
  53. (name)->tail = ((name)->tail + 1) % queue_max(name); \
  54. (name)->count--; \
  55. ret = Q_OK; \
  56. } \
  57. } while (0)
  58. #endif