queue.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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) ((name)->count == ((sizeof((name)->message)) / (sizeof((name)->message[0]))))
  27. #define en_queue(name, field, ret) \
  28. do \
  29. { \
  30. if (queue_full(name)) \
  31. { \
  32. ret = Q_FULL; \
  33. } \
  34. else \
  35. { \
  36. (name)->count++; \
  37. (name)->message[(name)->head] = field; \
  38. (name)->head = ((name)->head + 1) % queue_max(name); \
  39. ret = Q_OK; \
  40. } \
  41. } while (0)
  42. #define de_queue(name, field, ret) \
  43. do \
  44. { \
  45. if (queue_empty(name)) \
  46. { \
  47. ret = Q_EMPTY; \
  48. } \
  49. else \
  50. { \
  51. field = (name)->message[(name)->tail]; \
  52. (name)->tail = ((name)->tail + 1) % queue_max(name); \
  53. (name)->count--; \
  54. ret = Q_OK; \
  55. } \
  56. } while (0)
  57. #endif