ulist.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. 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. };
  38. void ulist_init(struct ulist *ulist);
  39. void ulist_release(struct ulist *ulist);
  40. void ulist_reinit(struct ulist *ulist);
  41. struct ulist *ulist_alloc(gfp_t gfp_mask);
  42. void ulist_free(struct ulist *ulist);
  43. int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask);
  44. int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux,
  45. u64 *old_aux, gfp_t gfp_mask);
  46. int ulist_del(struct ulist *ulist, u64 val, u64 aux);
  47. /* just like ulist_add_merge() but take a pointer for the aux data */
  48. static inline int ulist_add_merge_ptr(struct ulist *ulist, u64 val, void *aux,
  49. void **old_aux, gfp_t gfp_mask)
  50. {
  51. #if BITS_PER_LONG == 32
  52. u64 old64 = (uintptr_t)*old_aux;
  53. int ret = ulist_add_merge(ulist, val, (uintptr_t)aux, &old64, gfp_mask);
  54. *old_aux = (void *)((uintptr_t)old64);
  55. return ret;
  56. #else
  57. return ulist_add_merge(ulist, val, (u64)aux, (u64 *)old_aux, gfp_mask);
  58. #endif
  59. }
  60. struct ulist_node *ulist_next(struct ulist *ulist,
  61. struct ulist_iterator *uiter);
  62. #define ULIST_ITER_INIT(uiter) ((uiter)->cur_list = NULL)
  63. #endif