assoc_array_priv.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* Private definitions for the 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 ([email protected])
  8. */
  9. #ifndef _LINUX_ASSOC_ARRAY_PRIV_H
  10. #define _LINUX_ASSOC_ARRAY_PRIV_H
  11. #ifdef CONFIG_ASSOCIATIVE_ARRAY
  12. #include <linux/assoc_array.h>
  13. #define ASSOC_ARRAY_FAN_OUT 16 /* Number of slots per node */
  14. #define ASSOC_ARRAY_FAN_MASK (ASSOC_ARRAY_FAN_OUT - 1)
  15. #define ASSOC_ARRAY_LEVEL_STEP (ilog2(ASSOC_ARRAY_FAN_OUT))
  16. #define ASSOC_ARRAY_LEVEL_STEP_MASK (ASSOC_ARRAY_LEVEL_STEP - 1)
  17. #define ASSOC_ARRAY_KEY_CHUNK_MASK (ASSOC_ARRAY_KEY_CHUNK_SIZE - 1)
  18. #define ASSOC_ARRAY_KEY_CHUNK_SHIFT (ilog2(BITS_PER_LONG))
  19. /*
  20. * Undefined type representing a pointer with type information in the bottom
  21. * two bits.
  22. */
  23. struct assoc_array_ptr;
  24. /*
  25. * An N-way node in the tree.
  26. *
  27. * Each slot contains one of four things:
  28. *
  29. * (1) Nothing (NULL).
  30. *
  31. * (2) A leaf object (pointer types 0).
  32. *
  33. * (3) A next-level node (pointer type 1, subtype 0).
  34. *
  35. * (4) A shortcut (pointer type 1, subtype 1).
  36. *
  37. * The tree is optimised for search-by-ID, but permits reasonable iteration
  38. * also.
  39. *
  40. * The tree is navigated by constructing an index key consisting of an array of
  41. * segments, where each segment is ilog2(ASSOC_ARRAY_FAN_OUT) bits in size.
  42. *
  43. * The segments correspond to levels of the tree (the first segment is used at
  44. * level 0, the second at level 1, etc.).
  45. */
  46. struct assoc_array_node {
  47. struct assoc_array_ptr *back_pointer;
  48. u8 parent_slot;
  49. struct assoc_array_ptr *slots[ASSOC_ARRAY_FAN_OUT];
  50. unsigned long nr_leaves_on_branch;
  51. };
  52. /*
  53. * A shortcut through the index space out to where a collection of nodes/leaves
  54. * with the same IDs live.
  55. */
  56. struct assoc_array_shortcut {
  57. struct assoc_array_ptr *back_pointer;
  58. int parent_slot;
  59. int skip_to_level;
  60. struct assoc_array_ptr *next_node;
  61. unsigned long index_key[];
  62. };
  63. /*
  64. * Preallocation cache.
  65. */
  66. struct assoc_array_edit {
  67. struct rcu_head rcu;
  68. struct assoc_array *array;
  69. const struct assoc_array_ops *ops;
  70. const struct assoc_array_ops *ops_for_excised_subtree;
  71. struct assoc_array_ptr *leaf;
  72. struct assoc_array_ptr **leaf_p;
  73. struct assoc_array_ptr *dead_leaf;
  74. struct assoc_array_ptr *new_meta[3];
  75. struct assoc_array_ptr *excised_meta[1];
  76. struct assoc_array_ptr *excised_subtree;
  77. struct assoc_array_ptr **set_backpointers[ASSOC_ARRAY_FAN_OUT];
  78. struct assoc_array_ptr *set_backpointers_to;
  79. struct assoc_array_node *adjust_count_on;
  80. long adjust_count_by;
  81. struct {
  82. struct assoc_array_ptr **ptr;
  83. struct assoc_array_ptr *to;
  84. } set[2];
  85. struct {
  86. u8 *p;
  87. u8 to;
  88. } set_parent_slot[1];
  89. u8 segment_cache[ASSOC_ARRAY_FAN_OUT + 1];
  90. };
  91. /*
  92. * Internal tree member pointers are marked in the bottom one or two bits to
  93. * indicate what type they are so that we don't have to look behind every
  94. * pointer to see what it points to.
  95. *
  96. * We provide functions to test type annotations and to create and translate
  97. * the annotated pointers.
  98. */
  99. #define ASSOC_ARRAY_PTR_TYPE_MASK 0x1UL
  100. #define ASSOC_ARRAY_PTR_LEAF_TYPE 0x0UL /* Points to leaf (or nowhere) */
  101. #define ASSOC_ARRAY_PTR_META_TYPE 0x1UL /* Points to node or shortcut */
  102. #define ASSOC_ARRAY_PTR_SUBTYPE_MASK 0x2UL
  103. #define ASSOC_ARRAY_PTR_NODE_SUBTYPE 0x0UL
  104. #define ASSOC_ARRAY_PTR_SHORTCUT_SUBTYPE 0x2UL
  105. static inline bool assoc_array_ptr_is_meta(const struct assoc_array_ptr *x)
  106. {
  107. return (unsigned long)x & ASSOC_ARRAY_PTR_TYPE_MASK;
  108. }
  109. static inline bool assoc_array_ptr_is_leaf(const struct assoc_array_ptr *x)
  110. {
  111. return !assoc_array_ptr_is_meta(x);
  112. }
  113. static inline bool assoc_array_ptr_is_shortcut(const struct assoc_array_ptr *x)
  114. {
  115. return (unsigned long)x & ASSOC_ARRAY_PTR_SUBTYPE_MASK;
  116. }
  117. static inline bool assoc_array_ptr_is_node(const struct assoc_array_ptr *x)
  118. {
  119. return !assoc_array_ptr_is_shortcut(x);
  120. }
  121. static inline void *assoc_array_ptr_to_leaf(const struct assoc_array_ptr *x)
  122. {
  123. return (void *)((unsigned long)x & ~ASSOC_ARRAY_PTR_TYPE_MASK);
  124. }
  125. static inline
  126. unsigned long __assoc_array_ptr_to_meta(const struct assoc_array_ptr *x)
  127. {
  128. return (unsigned long)x &
  129. ~(ASSOC_ARRAY_PTR_SUBTYPE_MASK | ASSOC_ARRAY_PTR_TYPE_MASK);
  130. }
  131. static inline
  132. struct assoc_array_node *assoc_array_ptr_to_node(const struct assoc_array_ptr *x)
  133. {
  134. return (struct assoc_array_node *)__assoc_array_ptr_to_meta(x);
  135. }
  136. static inline
  137. struct assoc_array_shortcut *assoc_array_ptr_to_shortcut(const struct assoc_array_ptr *x)
  138. {
  139. return (struct assoc_array_shortcut *)__assoc_array_ptr_to_meta(x);
  140. }
  141. static inline
  142. struct assoc_array_ptr *__assoc_array_x_to_ptr(const void *p, unsigned long t)
  143. {
  144. return (struct assoc_array_ptr *)((unsigned long)p | t);
  145. }
  146. static inline
  147. struct assoc_array_ptr *assoc_array_leaf_to_ptr(const void *p)
  148. {
  149. return __assoc_array_x_to_ptr(p, ASSOC_ARRAY_PTR_LEAF_TYPE);
  150. }
  151. static inline
  152. struct assoc_array_ptr *assoc_array_node_to_ptr(const struct assoc_array_node *p)
  153. {
  154. return __assoc_array_x_to_ptr(
  155. p, ASSOC_ARRAY_PTR_META_TYPE | ASSOC_ARRAY_PTR_NODE_SUBTYPE);
  156. }
  157. static inline
  158. struct assoc_array_ptr *assoc_array_shortcut_to_ptr(const struct assoc_array_shortcut *p)
  159. {
  160. return __assoc_array_x_to_ptr(
  161. p, ASSOC_ARRAY_PTR_META_TYPE | ASSOC_ARRAY_PTR_SHORTCUT_SUBTYPE);
  162. }
  163. #endif /* CONFIG_ASSOCIATIVE_ARRAY */
  164. #endif /* _LINUX_ASSOC_ARRAY_PRIV_H */