assoc_array.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* Generic associative array implementation.
  3. *
  4. * See Documentation/core-api/assoc_array.rst for information.
  5. *
  6. * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
  7. * Written by David Howells (dhowells@redhat.com)
  8. */
  9. #ifndef _LINUX_ASSOC_ARRAY_H
  10. #define _LINUX_ASSOC_ARRAY_H
  11. #ifdef CONFIG_ASSOCIATIVE_ARRAY
  12. #include <linux/types.h>
  13. #define ASSOC_ARRAY_KEY_CHUNK_SIZE BITS_PER_LONG /* Key data retrieved in chunks of this size */
  14. /*
  15. * Generic associative array.
  16. */
  17. struct assoc_array {
  18. struct assoc_array_ptr *root; /* The node at the root of the tree */
  19. unsigned long nr_leaves_on_tree;
  20. };
  21. /*
  22. * Operations on objects and index keys for use by array manipulation routines.
  23. */
  24. struct assoc_array_ops {
  25. /* Method to get a chunk of an index key from caller-supplied data */
  26. unsigned long (*get_key_chunk)(const void *index_key, int level);
  27. /* Method to get a piece of an object's index key */
  28. unsigned long (*get_object_key_chunk)(const void *object, int level);
  29. /* Is this the object we're looking for? */
  30. bool (*compare_object)(const void *object, const void *index_key);
  31. /* How different is an object from an index key, to a bit position in
  32. * their keys? (or -1 if they're the same)
  33. */
  34. int (*diff_objects)(const void *object, const void *index_key);
  35. /* Method to free an object. */
  36. void (*free_object)(void *object);
  37. };
  38. /*
  39. * Access and manipulation functions.
  40. */
  41. struct assoc_array_edit;
  42. static inline void assoc_array_init(struct assoc_array *array)
  43. {
  44. array->root = NULL;
  45. array->nr_leaves_on_tree = 0;
  46. }
  47. extern int assoc_array_iterate(const struct assoc_array *array,
  48. int (*iterator)(const void *object,
  49. void *iterator_data),
  50. void *iterator_data);
  51. extern void *assoc_array_find(const struct assoc_array *array,
  52. const struct assoc_array_ops *ops,
  53. const void *index_key);
  54. extern void assoc_array_destroy(struct assoc_array *array,
  55. const struct assoc_array_ops *ops);
  56. extern struct assoc_array_edit *assoc_array_insert(struct assoc_array *array,
  57. const struct assoc_array_ops *ops,
  58. const void *index_key,
  59. void *object);
  60. extern void assoc_array_insert_set_object(struct assoc_array_edit *edit,
  61. void *object);
  62. extern struct assoc_array_edit *assoc_array_delete(struct assoc_array *array,
  63. const struct assoc_array_ops *ops,
  64. const void *index_key);
  65. extern struct assoc_array_edit *assoc_array_clear(struct assoc_array *array,
  66. const struct assoc_array_ops *ops);
  67. extern void assoc_array_apply_edit(struct assoc_array_edit *edit);
  68. extern void assoc_array_cancel_edit(struct assoc_array_edit *edit);
  69. extern int assoc_array_gc(struct assoc_array *array,
  70. const struct assoc_array_ops *ops,
  71. bool (*iterator)(void *object, void *iterator_data),
  72. void *iterator_data);
  73. #endif /* CONFIG_ASSOCIATIVE_ARRAY */
  74. #endif /* _LINUX_ASSOC_ARRAY_H */