ulist.h 1.9 KB

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