dlist.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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, write to the Free Software Foundation,
  18. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #ifndef ADNS_DLIST_H_INCLUDED
  21. #define ADNS_DLIST_H_INCLUDED
  22. #define LIST_INIT(list) ((list).head = (list).tail = NULL)
  23. #define LINK_INIT(link) ((link).next = (link).back = NULL)
  24. #define LIST_UNLINK_PART(list, node, part) \
  25. do { \
  26. if ((node)->part back) \
  27. (node)->part back->part next = (node)->part next; \
  28. else \
  29. (list).head = (node)->part next; \
  30. if ((node)->part next) \
  31. (node)->part next->part back = (node)->part back; \
  32. else \
  33. (list).tail = (node)->part back; \
  34. } while (0)
  35. #define LIST_LINK_TAIL_PART(list, node, part) \
  36. do { \
  37. (node)->part next = NULL; \
  38. (node)->part back = (list).tail; \
  39. if ((list).tail) \
  40. (list).tail->part next = (node); \
  41. else (list).head = (node); \
  42. (list).tail = (node); \
  43. } while (0)
  44. #define LIST_CHECKNODE_PART(list, node, part) \
  45. do { \
  46. if ((node)->next) \
  47. assert((node)->part next->part back == (node)); \
  48. else \
  49. assert((node) == (list).tail); \
  50. if ((node)->back) \
  51. assert((node)->part back->part next == (node)); \
  52. else \
  53. assert((node) == (list).head); \
  54. } while (0)
  55. #define LIST_UNLINK(list, node) LIST_UNLINK_PART(list, node,)
  56. #define LIST_LINK_TAIL(list, node) LIST_LINK_TAIL_PART(list, node,)
  57. #define LIST_CHECKNODE(list, node) LIST_CHECKNODE_PART(list, node,)
  58. #endif