ulist.h 1.9 KB

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