clist.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * $Id$
  3. *
  4. * circular list maintenance macros
  5. *
  6. * Copyright (C) 2005 iptelorg GmbH
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * ser is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /*!
  25. * \file
  26. * \brief SIP-router core :: circular list maintenance macros
  27. *
  28. * \ingroup core
  29. * Module: \ref core
  30. */
  31. /* History:
  32. * --------
  33. * 2005-08-08 created by andrei
  34. */
  35. #ifndef _clist_h
  36. #define _clist_h
  37. /*! \brief circular list */
  38. #define clist_init(c, next, prev) \
  39. do{ \
  40. (c)->next=(void*)(c); \
  41. (c)->prev=(void*)(c); \
  42. } while(0)
  43. /*! \brief adds an entire sublist { s,e } (including s & e )
  44. * after head
  45. *
  46. * \note WARNING: clist_insert_sublist(head, n, n->prev) won't work,
  47. * same for clist_insert_sublist(head, n->next, n)
  48. * (macro!), use e=n->prev; clist_insert_sublist(head, n, e, ...)
  49. * instead!
  50. */
  51. #define clist_insert_sublist(head, s, e, next, prev) \
  52. do{ \
  53. (s)->prev=(void*)(head); \
  54. (e)->next=(head)->next; \
  55. (e)->next->prev=(e); \
  56. (head)->next=s; \
  57. }while(0)
  58. /*! \brief appends an entire sublist { s,e } (including s & e )
  59. * at the end of the list
  60. *
  61. * WARNING: clist_append_sublist(head, n, n->prev, ...) won't work,
  62. * (macro!), use e=n->prev; clist_append_sublist(head, n, e, ...)
  63. * instead!
  64. */
  65. #define clist_append_sublist(head, s, e, next, prev) \
  66. do{ \
  67. (s)->prev=(head)->prev; \
  68. (e)->next=(void*)(head); \
  69. (s)->prev->next=(s); \
  70. (head)->prev=(e); \
  71. }while(0)
  72. /*! \brief remove sublist { s,e } (including s & e )
  73. * always, if start is the beginning of the list use
  74. * clist_rm_sublist(head->next, e, next, prev )
  75. * WARNING: clist_rm_sublist(n, n->prev, ...) won't work,
  76. * (macro!), use e=n->prev; clist_rm_sublist(n, e, ...)
  77. * instead! */
  78. #define clist_rm_sublist(s, e, next, prev) \
  79. do{\
  80. (s)->prev->next=(e)->next; \
  81. (e)->next->prev=(s)->prev ; \
  82. }while(0)
  83. /*! \brief insert after (head) */
  84. #define clist_insert(head, c, next, prev) \
  85. clist_insert_sublist(head, c, c, next, prev)
  86. /*! \brief append at the end of the list (head->prev) */
  87. #define clist_append(head, c, next, prev) \
  88. clist_append_sublist(head, c, c, next, prev)
  89. /*! \brief remove and element */
  90. #define clist_rm(c, next, prev) \
  91. clist_rm_sublist(c, c, next, prev)
  92. /*! \brief iterate on a clist */
  93. #define clist_foreach(head, v, dir) \
  94. for((v)=(head)->dir; (v)!=(void*)(head); (v)=(v)->dir)
  95. /*! \brief iterate on a clist, safe version (requires an extra bak. var)
  96. * (it allows removing of the current element) */
  97. #define clist_foreach_safe(head, v, bak, dir) \
  98. for((v)=(head)->dir, (bak)=(v)->dir; (v)!=(void*)(head); \
  99. (v)=(bak), (bak)=(v)->dir)
  100. #endif