dlist.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * dlist.h - macros for handling doubly linked lists
  3. *
  4. * Copyright © 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ADNS_DLIST_H_INCLUDED
  20. #define ADNS_DLIST_H_INCLUDED
  21. #define LIST_INIT(list) ((list).head = (list).tail = NULL)
  22. #define LINK_INIT(link) ((link).next = (link).back = NULL)
  23. #define LIST_UNLINK_PART(list, node, part) \
  24. do { \
  25. if ((node)->part back) \
  26. (node)->part back->part next = (node)->part next; \
  27. else \
  28. (list).head = (node)->part next; \
  29. if ((node)->part next) \
  30. (node)->part next->part back = (node)->part back; \
  31. else \
  32. (list).tail = (node)->part back; \
  33. } while (0)
  34. #define LIST_LINK_TAIL_PART(list, node, part) \
  35. do { \
  36. (node)->part next = NULL; \
  37. (node)->part back = (list).tail; \
  38. if ((list).tail) \
  39. (list).tail->part next = (node); \
  40. else (list).head = (node); \
  41. (list).tail = (node); \
  42. } while (0)
  43. #define LIST_CHECKNODE_PART(list, node, part) \
  44. do { \
  45. if ((node)->next) \
  46. assert((node)->part next->part back == (node)); \
  47. else \
  48. assert((node) == (list).tail); \
  49. if ((node)->back) \
  50. assert((node)->part back->part next == (node)); \
  51. else \
  52. assert((node) == (list).head); \
  53. } while (0)
  54. #define LIST_UNLINK(list, node) LIST_UNLINK_PART(list, node,)
  55. #define LIST_LINK_TAIL(list, node) LIST_LINK_TAIL_PART(list, node,)
  56. #define LIST_CHECKNODE(list, node) LIST_CHECKNODE_PART(list, node,)
  57. #endif