ulist.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (C) 2011 STRATO AG
  3. * written by Arne Jansen <sensille@gmx.net>
  4. * Distributed under the GNU GPL license version 2.
  5. *
  6. */
  7. #ifndef __ULIST__
  8. #define __ULIST__
  9. #include <linux/list.h>
  10. #include <linux/rbtree.h>
  11. /*
  12. * ulist is a generic data structure to hold a collection of unique u64
  13. * values. The only operations it supports is adding to the list and
  14. * enumerating it.
  15. * It is possible to store an auxiliary value along with the key.
  16. *
  17. */
  18. struct ulist_iterator {
  19. #ifdef CONFIG_BTRFS_DEBUG
  20. int i;
  21. #endif
  22. struct list_head *cur_list; /* hint to start search */
  23. };
  24. /*
  25. * element of the list
  26. */
  27. struct ulist_node {
  28. u64 val; /* value to store */
  29. u64 aux; /* auxiliary value saved along with the val */
  30. #ifdef CONFIG_BTRFS_DEBUG
  31. int seqnum; /* sequence number this node is added */
  32. #endif
  33. struct list_head list; /* used to link node */
  34. struct rb_node rb_node; /* used to speed up search */
  35. };
  36. struct ulist {
  37. /*
  38. * number of elements stored in list
  39. */
  40. unsigned long nnodes;
  41. struct list_head nodes;
  42. struct rb_root root;
  43. };
  44. void ulist_init(struct ulist *ulist);
  45. void ulist_reinit(struct ulist *ulist);
  46. struct ulist *ulist_alloc(gfp_t gfp_mask);
  47. void ulist_free(struct ulist *ulist);
  48. int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask);
  49. int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux,
  50. u64 *old_aux, gfp_t gfp_mask);
  51. int ulist_del(struct ulist *ulist, u64 val, u64 aux);
  52. /* just like ulist_add_merge() but take a pointer for the aux data */
  53. static inline int ulist_add_merge_ptr(struct ulist *ulist, u64 val, void *aux,
  54. void **old_aux, gfp_t gfp_mask)
  55. {
  56. #if BITS_PER_LONG == 32
  57. u64 old64 = (uintptr_t)*old_aux;
  58. int ret = ulist_add_merge(ulist, val, (uintptr_t)aux, &old64, gfp_mask);
  59. *old_aux = (void *)((uintptr_t)old64);
  60. return ret;
  61. #else
  62. return ulist_add_merge(ulist, val, (u64)aux, (u64 *)old_aux, gfp_mask);
  63. #endif
  64. }
  65. struct ulist_node *ulist_next(struct ulist *ulist,
  66. struct ulist_iterator *uiter);
  67. #define ULIST_ITER_INIT(uiter) ((uiter)->cur_list = NULL)
  68. #endif