ulist.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. /*
  24. * element of the list
  25. */
  26. struct ulist_node {
  27. u64 val; /* value to store */
  28. unsigned long aux; /* auxiliary value saved along with the val */
  29. };
  30. struct ulist {
  31. /*
  32. * number of elements stored in list
  33. */
  34. unsigned long nnodes;
  35. /*
  36. * number of nodes we already have room for
  37. */
  38. unsigned long nodes_alloced;
  39. /*
  40. * pointer to the array storing the elements. The first ULIST_SIZE
  41. * elements are stored inline. In this case the it points to int_nodes.
  42. * After exceeding ULIST_SIZE, dynamic memory is allocated.
  43. */
  44. struct ulist_node *nodes;
  45. /*
  46. * inline storage space for the first ULIST_SIZE entries
  47. */
  48. struct ulist_node int_nodes[ULIST_SIZE];
  49. };
  50. void ulist_init(struct ulist *ulist);
  51. void ulist_fini(struct ulist *ulist);
  52. void ulist_reinit(struct ulist *ulist);
  53. struct ulist *ulist_alloc(gfp_t gfp_mask);
  54. void ulist_free(struct ulist *ulist);
  55. int ulist_add(struct ulist *ulist, u64 val, unsigned long aux,
  56. gfp_t gfp_mask);
  57. struct ulist_node *ulist_next(struct ulist *ulist, struct ulist_node *prev);
  58. #endif