queue.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifndef __QUEUE_H
  2. #define __QUEUE_H
  3. #include "stdio.h"
  4. typedef struct
  5. {
  6. struct
  7. {
  8. uint8_t ide;
  9. uint8_t rtr;
  10. uint8_t dlc;
  11. } reg;
  12. union
  13. {
  14. uint32_t r;
  15. struct
  16. {
  17. uint8_t sa : 8;
  18. uint8_t ps : 8;
  19. uint8_t pf : 8;
  20. uint8_t dp : 1;
  21. uint8_t r : 1;
  22. uint8_t p : 3;
  23. } b;
  24. } id;
  25. union
  26. {
  27. uint8_t u8_buf[8];
  28. uint16_t u16_buf[4];
  29. uint32_t u32_buf[2];
  30. uint64_t u64_buf;
  31. } data;
  32. } pdu_tag, *p_pdu_tag;
  33. typedef enum
  34. {
  35. Q_OK,
  36. Q_ERR,
  37. Q_FULL,
  38. Q_EMPTY,
  39. } QUEUE_STATUS;
  40. #define queue_entry(type, size) \
  41. struct \
  42. { \
  43. uint8_t head; \
  44. uint8_t tail; \
  45. uint8_t count; \
  46. type message[size]; \
  47. }
  48. #define queue_init(name) \
  49. { \
  50. (name)->head = (name)->tail = 0; \
  51. (name)->count = 0; \
  52. }
  53. /* List functions. */
  54. #define queue_empty(name) ((name)->count == 0)
  55. #define queue_max(name) ((sizeof((name)->message)) / (sizeof((name)->message[0])))
  56. #define queue_full(name) ((name)->count == ((sizeof((name)->message)) / (sizeof((name)->message[0]))))
  57. #define en_queue(name, field, ret) \
  58. do \
  59. { \
  60. if (queue_full(name)) \
  61. { \
  62. ret = Q_FULL; \
  63. } \
  64. else \
  65. { \
  66. (name)->count++; \
  67. (name)->message[(name)->head] = field; \
  68. (name)->head = ((name)->head + 1) % queue_max(name); \
  69. ret = Q_OK; \
  70. } \
  71. } while (0)
  72. #define de_queue(name, field, ret) \
  73. do \
  74. { \
  75. if (queue_empty(name)) \
  76. { \
  77. ret = Q_EMPTY; \
  78. } \
  79. else \
  80. { \
  81. field = (name)->message[(name)->tail]; \
  82. (name)->tail = ((name)->tail + 1) % queue_max(name); \
  83. (name)->count--; \
  84. ret = Q_OK; \
  85. } \
  86. } while (0)
  87. #endif