clist.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. /* History:
  30. * --------
  31. * 2005-08-08 created by andrei
  32. */
  33. #ifndef _clist_h
  34. #define _clist_h
  35. /* circular list */
  36. #define clist_init(c, next, prev) \
  37. do{ \
  38. (c)->next=(void*)(c); \
  39. (c)->prev=(void*)(c); \
  40. } while(0)
  41. /* adds an entire sublist { s,e } (including s & e )
  42. * after head
  43. * WARNING: clist_insert_sublist(head, n, n->prev) won't work,
  44. * same for clist_insert_sublist(head, n->next, n)
  45. * (macro!), use e=n->prev; clist_insert_sublist(head, n, e, ...)
  46. * instead!
  47. */
  48. #define clist_insert_sublist(head, s, e, next, prev) \
  49. do{ \
  50. (s)->prev=(void*)(head); \
  51. (e)->next=(head)->next; \
  52. (e)->next->prev=(e); \
  53. (head)->next=s; \
  54. }while(0)
  55. /* appends an entire sublist { s,e } (including s & e )
  56. * at the end of the list
  57. * WARNING: clist_append_sublist(head, n, n->prev, ...) won't work,
  58. * (macro!), use e=n->prev; clist_append_sublist(head, n, e, ...)
  59. * instead!
  60. */
  61. #define clist_append_sublist(head, s, e, next, prev) \
  62. do{ \
  63. (s)->prev=(head)->prev; \
  64. (e)->next=(void*)(head); \
  65. (s)->prev->next=(s); \
  66. (head)->prev=(e); \
  67. }while(0)
  68. /* remove sublist { s,e } (including s & e )
  69. * always, if start is the beginning of the list use
  70. * clist_rm_sublist(head->next, e, next, prev )
  71. * WARNING: clist_rm_sublist(n, n->prev, ...) won't work,
  72. * (macro!), use e=n->prev; clist_rm_sublist(n, e, ...)
  73. * instead! */
  74. #define clist_rm_sublist(s, e, next, prev) \
  75. do{\
  76. (s)->prev->next=(e)->next; \
  77. (e)->next->prev=(s)->prev ; \
  78. }while(0)
  79. /* insert after (head) */
  80. #define clist_insert(head, c, next, prev) \
  81. clist_insert_sublist(head, c, c, next, prev)
  82. /* append at the end of the list (head->prev) */
  83. #define clist_append(head, c, next, prev) \
  84. clist_append_sublist(head, c, c, next, prev)
  85. /* remove and element */
  86. #define clist_rm(c, next, prev) \
  87. clist_rm_sublist(c, c, next, prev)
  88. /* iterate on a clist */
  89. #define clist_foreach(head, v, dir) \
  90. for((v)=(head)->dir; (v)!=(void*)(head); (v)=(v)->dir)
  91. /* iterate on a clist, safe version (requires an extra bak. var)
  92. * (it allows removing of the current element) */
  93. #define clist_foreach_safe(head, v, bak, dir) \
  94. for((v)=(head)->dir, (bak)=(v)->dir; (v)!=(void*)(head); \
  95. (v)=(bak), (bak)=(v)->dir)
  96. #endif