utils.h 961 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. struct list_head {
  2. struct list_head *next;
  3. };
  4. #define LIST_HEAD(name) \
  5. struct list_head name = { &(name) }
  6. static inline int list_empty(const struct list_head *head)
  7. {
  8. return head->next == head;
  9. }
  10. static inline void list_add(struct list_head *new, struct list_head *head)
  11. {
  12. new->next = head->next;
  13. head->next = new;
  14. }
  15. static inline void list_del(struct list_head *entry, struct list_head *prev)
  16. {
  17. prev->next = entry->next;
  18. entry->next = entry;
  19. }
  20. #define list_for_each_safe(pos, n, head) \
  21. for (n = (head), pos = (head)->next; pos != (head); \
  22. n = pos, pos = n->next)
  23. #undef offsetof
  24. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  25. #define container_of(ptr, type, member) ({ \
  26. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  27. (type *)( (char *)__mptr - offsetof(type,member) );})
  28. #ifdef DEBUG
  29. #define pynl_dbg(fmt, ...) \
  30. fprintf(stderr, "%s: " fmt, __func__, __VA_ARGS__)
  31. #else
  32. #define pynl_dbg(fmt, ...)
  33. #endif