common.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #ifndef _STX_COMMON_H_
  2. #define _STX_COMMON_H_
  3. #include <stddef.h>
  4. #include <stdlib.h>
  5. #define STX_BEGIN_MACRO {
  6. #define STX_END_MACRO }
  7. /*****************************************
  8. * Circular linked list definitions
  9. */
  10. typedef struct _stx_clist {
  11. struct _stx_clist *next;
  12. struct _stx_clist *prev;
  13. } stx_clist_t;
  14. /* Insert element "_e" into the list, before "_l" */
  15. #define STX_CLIST_INSERT_BEFORE(_e,_l) \
  16. STX_BEGIN_MACRO \
  17. (_e)->next = (_l); \
  18. (_e)->prev = (_l)->prev; \
  19. (_l)->prev->next = (_e); \
  20. (_l)->prev = (_e); \
  21. STX_END_MACRO
  22. /* Insert element "_e" into the list, after "_l" */
  23. #define STX_CLIST_INSERT_AFTER(_e,_l) \
  24. STX_BEGIN_MACRO \
  25. (_e)->next = (_l)->next; \
  26. (_e)->prev = (_l); \
  27. (_l)->next->prev = (_e); \
  28. (_l)->next = (_e); \
  29. STX_END_MACRO
  30. /* Append an element "_e" to the end of the list "_l" */
  31. #define STX_CLIST_APPEND_LINK(_e,_l) STX_CLIST_INSERT_BEFORE(_e,_l)
  32. /* Remove the element "_e" from it's circular list */
  33. #define STX_CLIST_REMOVE_LINK(_e) \
  34. STX_BEGIN_MACRO \
  35. (_e)->prev->next = (_e)->next; \
  36. (_e)->next->prev = (_e)->prev; \
  37. STX_END_MACRO
  38. /* Return the head/tail of the list */
  39. #define STX_CLIST_HEAD(_l) (_l)->next
  40. #define STX_CLIST_TAIL(_l) (_l)->prev
  41. /* Return non-zero if the given circular list "_l" is empty, */
  42. /* zero if the circular list is not empty */
  43. #define STX_CLIST_IS_EMPTY(_l) \
  44. ((_l)->next == (_l))
  45. /* Initialize a circular list */
  46. #define STX_CLIST_INIT_CLIST(_l) \
  47. STX_BEGIN_MACRO \
  48. (_l)->next = (_l); \
  49. (_l)->prev = (_l); \
  50. STX_END_MACRO
  51. /*****************************************
  52. * Useful macros
  53. */
  54. #ifndef offsetof
  55. #define offsetof(type, identifier) ((size_t)&(((type *)0)->identifier))
  56. #endif
  57. #define STX_MIN(a, b) (((a) < (b)) ? (a) : (b))
  58. #endif /* !_STX_COMMON_H_ */