ulist.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/types.h>
  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. struct list_head *cur_list; /* hint to start search */
  20. };
  21. /*
  22. * element of the list
  23. */
  24. struct ulist_node {
  25. u64 val; /* value to store */
  26. u64 aux; /* auxiliary value saved along with the val */
  27. struct list_head list; /* used to link node */
  28. struct rb_node rb_node; /* used to speed up search */
  29. };
  30. struct ulist {
  31. /*
  32. * number of elements stored in list
  33. */
  34. unsigned long nnodes;
  35. struct list_head nodes;
  36. struct rb_root root;
  37. struct ulist_node *prealloc;
  38. };
  39. void ulist_init(struct ulist *ulist);
  40. void ulist_release(struct ulist *ulist);
  41. void ulist_reinit(struct ulist *ulist);
  42. struct ulist *ulist_alloc(gfp_t gfp_mask);
  43. void ulist_prealloc(struct ulist *ulist, gfp_t mask);
  44. void ulist_free(struct ulist *ulist);
  45. int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask);
  46. int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux,
  47. u64 *old_aux, gfp_t gfp_mask);
  48. int ulist_del(struct ulist *ulist, u64 val, u64 aux);
  49. /* just like ulist_add_merge() but take a pointer for the aux data */
  50. static inline int ulist_add_merge_ptr(struct ulist *ulist, u64 val, void *aux,
  51. void **old_aux, gfp_t gfp_mask)
  52. {
  53. #if BITS_PER_LONG == 32
  54. u64 old64 = (uintptr_t)*old_aux;
  55. int ret = ulist_add_merge(ulist, val, (uintptr_t)aux, &old64, gfp_mask);
  56. *old_aux = (void *)((uintptr_t)old64);
  57. return ret;
  58. #else
  59. return ulist_add_merge(ulist, val, (u64)aux, (u64 *)old_aux, gfp_mask);
  60. #endif
  61. }
  62. struct ulist_node *ulist_next(const struct ulist *ulist,
  63. struct ulist_iterator *uiter);
  64. #define ULIST_ITER_INIT(uiter) ((uiter)->cur_list = NULL)
  65. #endif