listhelp.h
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:3k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. #ifndef _LISTHELP_H
  2. #define _LISTHELP_H
  3. #include <linux/config.h>
  4. #include <linux/list.h>
  5. #include <linux/netfilter_ipv4/lockhelp.h>
  6. /* Header to do more comprehensive job than linux/list.h; assume list
  7.    is first entry in structure. */
  8. /* Return pointer to first true entry, if any, or NULL.  A macro
  9.    required to allow inlining of cmpfn. */
  10. #define LIST_FIND(head, cmpfn, type, args...)
  11. ({
  12. const struct list_head *__i = (head);
  13. ASSERT_READ_LOCK(head);
  14. do {
  15. __i = __i->next;
  16. if (__i == (head)) {
  17. __i = NULL;
  18. break;
  19. }
  20. } while (!cmpfn((const type)__i , ## args));
  21. (type)__i;
  22. })
  23. #define LIST_FIND_W(head, cmpfn, type, args...)
  24. ({
  25. const struct list_head *__i = (head);
  26. ASSERT_WRITE_LOCK(head);
  27. do {
  28. __i = __i->next;
  29. if (__i == (head)) {
  30. __i = NULL;
  31. break;
  32. }
  33. } while (!cmpfn((type)__i , ## args));
  34. (type)__i;
  35. })
  36. static inline int
  37. __list_cmp_same(const void *p1, const void *p2) { return p1 == p2; }
  38. /* Is this entry in the list? */
  39. static inline int
  40. list_inlist(struct list_head *head, const void *entry)
  41. {
  42. return LIST_FIND(head, __list_cmp_same, void *, entry) != NULL;
  43. }
  44. /* Delete from list. */
  45. #ifdef CONFIG_NETFILTER_DEBUG
  46. #define LIST_DELETE(head, oldentry)
  47. do {
  48. ASSERT_WRITE_LOCK(head);
  49. if (!list_inlist(head, oldentry))
  50. printk("LIST_DELETE: %s:%u `%s'(%p) not in %s.n",
  51.        __FILE__, __LINE__, #oldentry, oldentry, #head);
  52.         else list_del((struct list_head *)oldentry);
  53. } while(0)
  54. #else
  55. #define LIST_DELETE(head, oldentry) list_del((struct list_head *)oldentry)
  56. #endif
  57. /* Append. */
  58. static inline void
  59. list_append(struct list_head *head, void *new)
  60. {
  61. ASSERT_WRITE_LOCK(head);
  62. list_add((new), (head)->prev);
  63. }
  64. /* Prepend. */
  65. static inline void
  66. list_prepend(struct list_head *head, void *new)
  67. {
  68. ASSERT_WRITE_LOCK(head);
  69. list_add(new, head);
  70. }
  71. /* Insert according to ordering function; insert before first true. */
  72. #define LIST_INSERT(head, new, cmpfn)
  73. do {
  74. struct list_head *__i;
  75. ASSERT_WRITE_LOCK(head);
  76. for (__i = (head)->next;
  77.      !cmpfn((new), (typeof (new))__i) && __i != (head);
  78.      __i = __i->next);
  79. list_add((struct list_head *)(new), __i->prev);
  80. } while(0)
  81. /* If the field after the list_head is a nul-terminated string, you
  82.    can use these functions. */
  83. static inline int __list_cmp_name(const void *i, const char *name)
  84. {
  85. return strcmp(name, i+sizeof(struct list_head)) == 0;
  86. }
  87. /* Returns false if same name already in list, otherwise does insert. */
  88. static inline int
  89. list_named_insert(struct list_head *head, void *new)
  90. {
  91. if (LIST_FIND(head, __list_cmp_name, void *,
  92.       new + sizeof(struct list_head)))
  93. return 0;
  94. list_prepend(head, new);
  95. return 1;
  96. }
  97. /* Find this named element in the list. */
  98. #define list_named_find(head, name)
  99. LIST_FIND(head, __list_cmp_name, void *, name)
  100. #endif /*_LISTHELP_H*/