mqtt_list.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2019-12-11 22:47:55
  5. * @LastEditTime: 2020-10-17 14:18:02
  6. * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
  7. */
  8. #ifndef _MQTT_LIST_H_
  9. #define _MQTT_LIST_H_
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. typedef struct mqtt_list_node {
  14. struct mqtt_list_node *next;
  15. struct mqtt_list_node *prev;
  16. } mqtt_list_t;
  17. #define OFFSET_OF_FIELD(type, field) \
  18. ((size_t)&(((type *)0)->field))
  19. #define CONTAINER_OF_FIELD(ptr, type, field) \
  20. ((type *)((unsigned char *)(ptr) - OFFSET_OF_FIELD(type, field)))
  21. #define LIST_NODE(node) \
  22. { &(node), &(node) }
  23. #define LIST_DEFINE(list) \
  24. mqtt_list_t list = { &(list), &(list) }
  25. #define LIST_ENTRY(list, type, field) \
  26. CONTAINER_OF_FIELD(list, type, field)
  27. #define LIST_FIRST_ENTRY(list, type, field) \
  28. LIST_ENTRY((list)->next, type, field)
  29. #define LIST_FIRST_ENTRY_OR_NULL(list, type, field) \
  30. (mqtt_list_is_empty(list) ? NULL : LIST_FIRST_ENTRY(list, type, field))
  31. #define LIST_FOR_EACH(curr, list) \
  32. for (curr = (list)->next; curr != (list); curr = curr->next)
  33. #define LIST_FOR_EACH_PREV(curr, list) \
  34. for (curr = (list)->prev; curr != (list); curr = curr->prev)
  35. #define LIST_FOR_EACH_SAFE(curr, next, list) \
  36. for (curr = (list)->next, next = curr->next; curr != (list); \
  37. curr = next, next = curr->next)
  38. #define LIST_FOR_EACH_PREV_SAFE(curr, next, list) \
  39. for (curr = (list)->prev, next = curr->prev; \
  40. curr != (list); \
  41. curr = next, next = curr->prev)
  42. void mqtt_list_init(mqtt_list_t *list);
  43. void mqtt_list_add(mqtt_list_t *node, mqtt_list_t *list);
  44. void mqtt_list_add_tail(mqtt_list_t *node, mqtt_list_t *list);
  45. void mqtt_list_del(mqtt_list_t *entry);
  46. void mqtt_list_del_init(mqtt_list_t *entry);
  47. void mqtt_list_move(mqtt_list_t *node, mqtt_list_t *list);
  48. void mqtt_list_move_tail(mqtt_list_t *node, mqtt_list_t *list);
  49. int mqtt_list_is_empty(mqtt_list_t *list);
  50. #ifdef __cplusplus
  51. }
  52. #endif
  53. #endif /* _LIST_H_ */