klist.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * klist.h - Some generic list helpers, extending struct list_head a bit.
  3. *
  4. * Implementations are found in lib/klist.c
  5. *
  6. *
  7. * Copyright (C) 2005 Patrick Mochel
  8. *
  9. * This file is rleased under the GPL v2.
  10. */
  11. #ifndef _LINUX_KLIST_H
  12. #define _LINUX_KLIST_H
  13. #include <linux/spinlock.h>
  14. #include <linux/kref.h>
  15. #include <linux/list.h>
  16. struct klist_node;
  17. struct klist {
  18. spinlock_t k_lock;
  19. struct list_head k_list;
  20. void (*get)(struct klist_node *);
  21. void (*put)(struct klist_node *);
  22. } __attribute__ ((aligned (sizeof(void *))));
  23. #define KLIST_INIT(_name, _get, _put) \
  24. { .k_lock = __SPIN_LOCK_UNLOCKED(_name.k_lock), \
  25. .k_list = LIST_HEAD_INIT(_name.k_list), \
  26. .get = _get, \
  27. .put = _put, }
  28. #define DEFINE_KLIST(_name, _get, _put) \
  29. struct klist _name = KLIST_INIT(_name, _get, _put)
  30. extern void klist_init(struct klist *k, void (*get)(struct klist_node *),
  31. void (*put)(struct klist_node *));
  32. struct klist_node {
  33. void *n_klist; /* never access directly */
  34. struct list_head n_node;
  35. struct kref n_ref;
  36. };
  37. extern void klist_add_tail(struct klist_node *n, struct klist *k);
  38. extern void klist_add_head(struct klist_node *n, struct klist *k);
  39. extern void klist_add_behind(struct klist_node *n, struct klist_node *pos);
  40. extern void klist_add_before(struct klist_node *n, struct klist_node *pos);
  41. extern void klist_del(struct klist_node *n);
  42. extern void klist_remove(struct klist_node *n);
  43. extern int klist_node_attached(struct klist_node *n);
  44. struct klist_iter {
  45. struct klist *i_klist;
  46. struct klist_node *i_cur;
  47. };
  48. extern void klist_iter_init(struct klist *k, struct klist_iter *i);
  49. extern void klist_iter_init_node(struct klist *k, struct klist_iter *i,
  50. struct klist_node *n);
  51. extern void klist_iter_exit(struct klist_iter *i);
  52. extern struct klist_node *klist_prev(struct klist_iter *i);
  53. extern struct klist_node *klist_next(struct klist_iter *i);
  54. #endif