llist.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
  3. * All rights reserved
  4. *
  5. * "THE BEER-WARE LICENSE" (Revision 42):
  6. * Sergey Lyubka wrote this file. As long as you retain this notice you
  7. * can do whatever you want with this stuff. If we meet some day, and you think
  8. * this stuff is worth it, you can buy me a beer in return.
  9. */
  10. #ifndef LLIST_HEADER_INCLUDED
  11. #define LLIST_HEADER_INCLUDED
  12. /*
  13. * Linked list macros.
  14. */
  15. struct llhead {
  16. struct llhead *prev;
  17. struct llhead *next;
  18. };
  19. #define LL_INIT(N) ((N)->next = (N)->prev = (N))
  20. #define LL_HEAD(H) struct llhead H = { &H, &H }
  21. #define LL_ENTRY(P,T,N) ((T *)((char *)(P) - offsetof(T, N)))
  22. #define LL_ADD(H, N) \
  23. do { \
  24. ((H)->next)->prev = (N); \
  25. (N)->next = ((H)->next); \
  26. (N)->prev = (H); \
  27. (H)->next = (N); \
  28. } while (0)
  29. #define LL_TAIL(H, N) \
  30. do { \
  31. ((H)->prev)->next = (N); \
  32. (N)->prev = ((H)->prev); \
  33. (N)->next = (H); \
  34. (H)->prev = (N); \
  35. } while (0)
  36. #define LL_DEL(N) \
  37. do { \
  38. ((N)->next)->prev = ((N)->prev); \
  39. ((N)->prev)->next = ((N)->next); \
  40. LL_INIT(N); \
  41. } while (0)
  42. #define LL_EMPTY(N) ((N)->next == (N))
  43. #define LL_FOREACH(H,N) for (N = (H)->next; N != (H); N = (N)->next)
  44. #define LL_FOREACH_SAFE(H,N,T) \
  45. for (N = (H)->next, T = (N)->next; N != (H); \
  46. N = (T), T = (N)->next)
  47. #endif /* LLIST_HEADER_INCLUDED */