assoc_array_priv.h 5.5 KB

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