ulist.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * The implementation is preliminary and can probably be sped up
  18. * significantly. A first step would be to store the values in an rbtree
  19. * as soon as ULIST_SIZE is exceeded.
  20. */
  21. /*
  22. * number of elements statically allocated inside struct ulist
  23. */
  24. #define ULIST_SIZE 16
  25. struct ulist_iterator {
  26. int i;
  27. };
  28. /*
  29. * element of the list
  30. */
  31. struct ulist_node {
  32. u64 val; /* value to store */
  33. u64 aux; /* auxiliary value saved along with the val */
  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. /*
  42. * number of nodes we already have room for
  43. */
  44. unsigned long nodes_alloced;
  45. /*
  46. * pointer to the array storing the elements. The first ULIST_SIZE
  47. * elements are stored inline. In this case the it points to int_nodes.
  48. * After exceeding ULIST_SIZE, dynamic memory is allocated.
  49. */
  50. struct ulist_node *nodes;
  51. struct rb_root root;
  52. /*
  53. * inline storage space for the first ULIST_SIZE entries
  54. */
  55. struct ulist_node int_nodes[ULIST_SIZE];
  56. };
  57. void ulist_init(struct ulist *ulist);
  58. void ulist_fini(struct ulist *ulist);
  59. void ulist_reinit(struct ulist *ulist);
  60. struct ulist *ulist_alloc(gfp_t gfp_mask);
  61. void ulist_free(struct ulist *ulist);
  62. int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask);
  63. int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux,
  64. u64 *old_aux, gfp_t gfp_mask);
  65. struct ulist_node *ulist_next(struct ulist *ulist,
  66. struct ulist_iterator *uiter);
  67. #define ULIST_ITER_INIT(uiter) ((uiter)->i = 0)
  68. #endif