btree.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #ifndef BTREE_H
  2. #define BTREE_H
  3. #include <linux/kernel.h>
  4. #include <linux/mempool.h>
  5. /**
  6. * DOC: B+Tree basics
  7. *
  8. * A B+Tree is a data structure for looking up arbitrary (currently allowing
  9. * unsigned long, u32, u64 and 2 * u64) keys into pointers. The data structure
  10. * is described at http://en.wikipedia.org/wiki/B-tree, we currently do not
  11. * use binary search to find the key on lookups.
  12. *
  13. * Each B+Tree consists of a head, that contains bookkeeping information and
  14. * a variable number (starting with zero) nodes. Each node contains the keys
  15. * and pointers to sub-nodes, or, for leaf nodes, the keys and values for the
  16. * tree entries.
  17. *
  18. * Each node in this implementation has the following layout:
  19. * [key1, key2, ..., keyN] [val1, val2, ..., valN]
  20. *
  21. * Each key here is an array of unsigned longs, geo->no_longs in total. The
  22. * number of keys and values (N) is geo->no_pairs.
  23. */
  24. /**
  25. * struct btree_head - btree head
  26. *
  27. * @node: the first node in the tree
  28. * @mempool: mempool used for node allocations
  29. * @height: current of the tree
  30. */
  31. struct btree_head {
  32. unsigned long *node;
  33. mempool_t *mempool;
  34. int height;
  35. };
  36. /* btree geometry */
  37. struct btree_geo;
  38. /**
  39. * btree_alloc - allocate function for the mempool
  40. * @gfp_mask: gfp mask for the allocation
  41. * @pool_data: unused
  42. */
  43. void *btree_alloc(gfp_t gfp_mask, void *pool_data);
  44. /**
  45. * btree_free - free function for the mempool
  46. * @element: the element to free
  47. * @pool_data: unused
  48. */
  49. void btree_free(void *element, void *pool_data);
  50. /**
  51. * btree_init_mempool - initialise a btree with given mempool
  52. *
  53. * @head: the btree head to initialise
  54. * @mempool: the mempool to use
  55. *
  56. * When this function is used, there is no need to destroy
  57. * the mempool.
  58. */
  59. void btree_init_mempool(struct btree_head *head, mempool_t *mempool);
  60. /**
  61. * btree_init - initialise a btree
  62. *
  63. * @head: the btree head to initialise
  64. *
  65. * This function allocates the memory pool that the
  66. * btree needs. Returns zero or a negative error code
  67. * (-%ENOMEM) when memory allocation fails.
  68. *
  69. */
  70. int __must_check btree_init(struct btree_head *head);
  71. /**
  72. * btree_destroy - destroy mempool
  73. *
  74. * @head: the btree head to destroy
  75. *
  76. * This function destroys the internal memory pool, use only
  77. * when using btree_init(), not with btree_init_mempool().
  78. */
  79. void btree_destroy(struct btree_head *head);
  80. /**
  81. * btree_lookup - look up a key in the btree
  82. *
  83. * @head: the btree to look in
  84. * @geo: the btree geometry
  85. * @key: the key to look up
  86. *
  87. * This function returns the value for the given key, or %NULL.
  88. */
  89. void *btree_lookup(struct btree_head *head, struct btree_geo *geo,
  90. unsigned long *key);
  91. /**
  92. * btree_insert - insert an entry into the btree
  93. *
  94. * @head: the btree to add to
  95. * @geo: the btree geometry
  96. * @key: the key to add (must not already be present)
  97. * @val: the value to add (must not be %NULL)
  98. * @gfp: allocation flags for node allocations
  99. *
  100. * This function returns 0 if the item could be added, or an
  101. * error code if it failed (may fail due to memory pressure).
  102. */
  103. int __must_check btree_insert(struct btree_head *head, struct btree_geo *geo,
  104. unsigned long *key, void *val, gfp_t gfp);
  105. /**
  106. * btree_update - update an entry in the btree
  107. *
  108. * @head: the btree to update
  109. * @geo: the btree geometry
  110. * @key: the key to update
  111. * @val: the value to change it to (must not be %NULL)
  112. *
  113. * This function returns 0 if the update was successful, or
  114. * -%ENOENT if the key could not be found.
  115. */
  116. int btree_update(struct btree_head *head, struct btree_geo *geo,
  117. unsigned long *key, void *val);
  118. /**
  119. * btree_remove - remove an entry from the btree
  120. *
  121. * @head: the btree to update
  122. * @geo: the btree geometry
  123. * @key: the key to remove
  124. *
  125. * This function returns the removed entry, or %NULL if the key
  126. * could not be found.
  127. */
  128. void *btree_remove(struct btree_head *head, struct btree_geo *geo,
  129. unsigned long *key);
  130. /**
  131. * btree_merge - merge two btrees
  132. *
  133. * @target: the tree that gets all the entries
  134. * @victim: the tree that gets merged into @target
  135. * @geo: the btree geometry
  136. * @gfp: allocation flags
  137. *
  138. * The two trees @target and @victim may not contain the same keys,
  139. * that is a bug and triggers a BUG(). This function returns zero
  140. * if the trees were merged successfully, and may return a failure
  141. * when memory allocation fails, in which case both trees might have
  142. * been partially merged, i.e. some entries have been moved from
  143. * @victim to @target.
  144. */
  145. int btree_merge(struct btree_head *target, struct btree_head *victim,
  146. struct btree_geo *geo, gfp_t gfp);
  147. /**
  148. * btree_last - get last entry in btree
  149. *
  150. * @head: btree head
  151. * @geo: btree geometry
  152. * @key: last key
  153. *
  154. * Returns the last entry in the btree, and sets @key to the key
  155. * of that entry; returns NULL if the tree is empty, in that case
  156. * key is not changed.
  157. */
  158. void *btree_last(struct btree_head *head, struct btree_geo *geo,
  159. unsigned long *key);
  160. /**
  161. * btree_get_prev - get previous entry
  162. *
  163. * @head: btree head
  164. * @geo: btree geometry
  165. * @key: pointer to key
  166. *
  167. * The function returns the next item right before the value pointed to by
  168. * @key, and updates @key with its key, or returns %NULL when there is no
  169. * entry with a key smaller than the given key.
  170. */
  171. void *btree_get_prev(struct btree_head *head, struct btree_geo *geo,
  172. unsigned long *key);
  173. /* internal use, use btree_visitor{l,32,64,128} */
  174. size_t btree_visitor(struct btree_head *head, struct btree_geo *geo,
  175. unsigned long opaque,
  176. void (*func)(void *elem, unsigned long opaque,
  177. unsigned long *key, size_t index,
  178. void *func2),
  179. void *func2);
  180. /* internal use, use btree_grim_visitor{l,32,64,128} */
  181. size_t btree_grim_visitor(struct btree_head *head, struct btree_geo *geo,
  182. unsigned long opaque,
  183. void (*func)(void *elem, unsigned long opaque,
  184. unsigned long *key,
  185. size_t index, void *func2),
  186. void *func2);
  187. #include <linux/btree-128.h>
  188. extern struct btree_geo btree_geo32;
  189. #define BTREE_TYPE_SUFFIX l
  190. #define BTREE_TYPE_BITS BITS_PER_LONG
  191. #define BTREE_TYPE_GEO &btree_geo32
  192. #define BTREE_KEYTYPE unsigned long
  193. #include <linux/btree-type.h>
  194. #define btree_for_each_safel(head, key, val) \
  195. for (val = btree_lastl(head, &key); \
  196. val; \
  197. val = btree_get_prevl(head, &key))
  198. #define BTREE_TYPE_SUFFIX 32
  199. #define BTREE_TYPE_BITS 32
  200. #define BTREE_TYPE_GEO &btree_geo32
  201. #define BTREE_KEYTYPE u32
  202. #include <linux/btree-type.h>
  203. #define btree_for_each_safe32(head, key, val) \
  204. for (val = btree_last32(head, &key); \
  205. val; \
  206. val = btree_get_prev32(head, &key))
  207. extern struct btree_geo btree_geo64;
  208. #define BTREE_TYPE_SUFFIX 64
  209. #define BTREE_TYPE_BITS 64
  210. #define BTREE_TYPE_GEO &btree_geo64
  211. #define BTREE_KEYTYPE u64
  212. #include <linux/btree-type.h>
  213. #define btree_for_each_safe64(head, key, val) \
  214. for (val = btree_last64(head, &key); \
  215. val; \
  216. val = btree_get_prev64(head, &key))
  217. #endif