diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/Kconfig | 3 | ||||
| -rw-r--r-- | lib/Makefile | 1 | ||||
| -rw-r--r-- | lib/btree.c | 797 |
3 files changed, 801 insertions, 0 deletions
diff --git a/lib/Kconfig b/lib/Kconfig index bb1326d3839c..277fbfb233b9 100644 --- a/lib/Kconfig +++ b/lib/Kconfig | |||
| @@ -156,6 +156,9 @@ config TEXTSEARCH_BM | |||
| 156 | config TEXTSEARCH_FSM | 156 | config TEXTSEARCH_FSM |
| 157 | tristate | 157 | tristate |
| 158 | 158 | ||
| 159 | config BTREE | ||
| 160 | boolean | ||
| 161 | |||
| 159 | config HAS_IOMEM | 162 | config HAS_IOMEM |
| 160 | boolean | 163 | boolean |
| 161 | depends on !NO_IOMEM | 164 | depends on !NO_IOMEM |
diff --git a/lib/Makefile b/lib/Makefile index 2e78277eff9d..cff82612e98b 100644 --- a/lib/Makefile +++ b/lib/Makefile | |||
| @@ -41,6 +41,7 @@ lib-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o | |||
| 41 | obj-$(CONFIG_GENERIC_FIND_LAST_BIT) += find_last_bit.o | 41 | obj-$(CONFIG_GENERIC_FIND_LAST_BIT) += find_last_bit.o |
| 42 | obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o | 42 | obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o |
| 43 | obj-$(CONFIG_LOCK_KERNEL) += kernel_lock.o | 43 | obj-$(CONFIG_LOCK_KERNEL) += kernel_lock.o |
| 44 | obj-$(CONFIG_BTREE) += btree.o | ||
| 44 | obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o | 45 | obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o |
| 45 | obj-$(CONFIG_DEBUG_LIST) += list_debug.o | 46 | obj-$(CONFIG_DEBUG_LIST) += list_debug.o |
| 46 | obj-$(CONFIG_DEBUG_OBJECTS) += debugobjects.o | 47 | obj-$(CONFIG_DEBUG_OBJECTS) += debugobjects.o |
diff --git a/lib/btree.c b/lib/btree.c new file mode 100644 index 000000000000..41859a820218 --- /dev/null +++ b/lib/btree.c | |||
| @@ -0,0 +1,797 @@ | |||
| 1 | /* | ||
| 2 | * lib/btree.c - Simple In-memory B+Tree | ||
| 3 | * | ||
| 4 | * As should be obvious for Linux kernel code, license is GPLv2 | ||
| 5 | * | ||
| 6 | * Copyright (c) 2007-2008 Joern Engel <joern@logfs.org> | ||
| 7 | * Bits and pieces stolen from Peter Zijlstra's code, which is | ||
| 8 | * Copyright 2007, Red Hat Inc. Peter Zijlstra <pzijlstr@redhat.com> | ||
| 9 | * GPLv2 | ||
| 10 | * | ||
| 11 | * see http://programming.kicks-ass.net/kernel-patches/vma_lookup/btree.patch | ||
| 12 | * | ||
| 13 | * A relatively simple B+Tree implementation. I have written it as a learning | ||
| 14 | * excercise to understand how B+Trees work. Turned out to be useful as well. | ||
| 15 | * | ||
| 16 | * B+Trees can be used similar to Linux radix trees (which don't have anything | ||
| 17 | * in common with textbook radix trees, beware). Prerequisite for them working | ||
| 18 | * well is that access to a random tree node is much faster than a large number | ||
| 19 | * of operations within each node. | ||
| 20 | * | ||
| 21 | * Disks have fulfilled the prerequisite for a long time. More recently DRAM | ||
| 22 | * has gained similar properties, as memory access times, when measured in cpu | ||
| 23 | * cycles, have increased. Cacheline sizes have increased as well, which also | ||
| 24 | * helps B+Trees. | ||
| 25 | * | ||
| 26 | * Compared to radix trees, B+Trees are more efficient when dealing with a | ||
| 27 | * sparsely populated address space. Between 25% and 50% of the memory is | ||
| 28 | * occupied with valid pointers. When densely populated, radix trees contain | ||
| 29 | * ~98% pointers - hard to beat. Very sparse radix trees contain only ~2% | ||
| 30 | * pointers. | ||
| 31 | * | ||
| 32 | * This particular implementation stores pointers identified by a long value. | ||
| 33 | * Storing NULL pointers is illegal, lookup will return NULL when no entry | ||
| 34 | * was found. | ||
| 35 | * | ||
| 36 | * A tricks was used that is not commonly found in textbooks. The lowest | ||
| 37 | * values are to the right, not to the left. All used slots within a node | ||
| 38 | * are on the left, all unused slots contain NUL values. Most operations | ||
| 39 | * simply loop once over all slots and terminate on the first NUL. | ||
| 40 | */ | ||
| 41 | |||
| 42 | #include <linux/btree.h> | ||
| 43 | #include <linux/cache.h> | ||
| 44 | #include <linux/kernel.h> | ||
| 45 | #include <linux/slab.h> | ||
| 46 | #include <linux/module.h> | ||
| 47 | |||
| 48 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) | ||
| 49 | #define NODESIZE MAX(L1_CACHE_BYTES, 128) | ||
| 50 | |||
| 51 | struct btree_geo { | ||
| 52 | int keylen; | ||
| 53 | int no_pairs; | ||
| 54 | int no_longs; | ||
| 55 | }; | ||
| 56 | |||
| 57 | struct btree_geo btree_geo32 = { | ||
| 58 | .keylen = 1, | ||
| 59 | .no_pairs = NODESIZE / sizeof(long) / 2, | ||
| 60 | .no_longs = NODESIZE / sizeof(long) / 2, | ||
| 61 | }; | ||
| 62 | EXPORT_SYMBOL_GPL(btree_geo32); | ||
| 63 | |||
| 64 | #define LONG_PER_U64 (64 / BITS_PER_LONG) | ||
| 65 | struct btree_geo btree_geo64 = { | ||
| 66 | .keylen = LONG_PER_U64, | ||
| 67 | .no_pairs = NODESIZE / sizeof(long) / (1 + LONG_PER_U64), | ||
| 68 | .no_longs = LONG_PER_U64 * (NODESIZE / sizeof(long) / (1 + LONG_PER_U64)), | ||
| 69 | }; | ||
| 70 | EXPORT_SYMBOL_GPL(btree_geo64); | ||
| 71 | |||
| 72 | struct btree_geo btree_geo128 = { | ||
| 73 | .keylen = 2 * LONG_PER_U64, | ||
| 74 | .no_pairs = NODESIZE / sizeof(long) / (1 + 2 * LONG_PER_U64), | ||
| 75 | .no_longs = 2 * LONG_PER_U64 * (NODESIZE / sizeof(long) / (1 + 2 * LONG_PER_U64)), | ||
| 76 | }; | ||
| 77 | EXPORT_SYMBOL_GPL(btree_geo128); | ||
| 78 | |||
| 79 | static struct kmem_cache *btree_cachep; | ||
| 80 | |||
| 81 | void *btree_alloc(gfp_t gfp_mask, void *pool_data) | ||
| 82 | { | ||
| 83 | return kmem_cache_alloc(btree_cachep, gfp_mask); | ||
| 84 | } | ||
| 85 | EXPORT_SYMBOL_GPL(btree_alloc); | ||
| 86 | |||
| 87 | void btree_free(void *element, void *pool_data) | ||
| 88 | { | ||
| 89 | kmem_cache_free(btree_cachep, element); | ||
| 90 | } | ||
| 91 | EXPORT_SYMBOL_GPL(btree_free); | ||
| 92 | |||
| 93 | static unsigned long *btree_node_alloc(struct btree_head *head, gfp_t gfp) | ||
| 94 | { | ||
| 95 | unsigned long *node; | ||
| 96 | |||
| 97 | node = mempool_alloc(head->mempool, gfp); | ||
| 98 | memset(node, 0, NODESIZE); | ||
| 99 | return node; | ||
| 100 | } | ||
| 101 | |||
| 102 | static int longcmp(const unsigned long *l1, const unsigned long *l2, size_t n) | ||
| 103 | { | ||
| 104 | size_t i; | ||
| 105 | |||
| 106 | for (i = 0; i < n; i++) { | ||
| 107 | if (l1[i] < l2[i]) | ||
| 108 | return -1; | ||
| 109 | if (l1[i] > l2[i]) | ||
| 110 | return 1; | ||
| 111 | } | ||
| 112 | return 0; | ||
| 113 | } | ||
| 114 | |||
| 115 | static unsigned long *longcpy(unsigned long *dest, const unsigned long *src, | ||
| 116 | size_t n) | ||
| 117 | { | ||
| 118 | size_t i; | ||
| 119 | |||
| 120 | for (i = 0; i < n; i++) | ||
| 121 | dest[i] = src[i]; | ||
| 122 | return dest; | ||
| 123 | } | ||
| 124 | |||
| 125 | static unsigned long *longset(unsigned long *s, unsigned long c, size_t n) | ||
| 126 | { | ||
| 127 | size_t i; | ||
| 128 | |||
| 129 | for (i = 0; i < n; i++) | ||
| 130 | s[i] = c; | ||
| 131 | return s; | ||
| 132 | } | ||
| 133 | |||
| 134 | static void dec_key(struct btree_geo *geo, unsigned long *key) | ||
| 135 | { | ||
| 136 | unsigned long val; | ||
| 137 | int i; | ||
| 138 | |||
| 139 | for (i = geo->keylen - 1; i >= 0; i--) { | ||
| 140 | val = key[i]; | ||
| 141 | key[i] = val - 1; | ||
| 142 | if (val) | ||
| 143 | break; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | static unsigned long *bkey(struct btree_geo *geo, unsigned long *node, int n) | ||
| 148 | { | ||
| 149 | return &node[n * geo->keylen]; | ||
| 150 | } | ||
| 151 | |||
| 152 | static void *bval(struct btree_geo *geo, unsigned long *node, int n) | ||
| 153 | { | ||
| 154 | return (void *)node[geo->no_longs + n]; | ||
| 155 | } | ||
| 156 | |||
| 157 | static void setkey(struct btree_geo *geo, unsigned long *node, int n, | ||
| 158 | unsigned long *key) | ||
| 159 | { | ||
| 160 | longcpy(bkey(geo, node, n), key, geo->keylen); | ||
| 161 | } | ||
| 162 | |||
| 163 | static void setval(struct btree_geo *geo, unsigned long *node, int n, | ||
| 164 | void *val) | ||
| 165 | { | ||
| 166 | node[geo->no_longs + n] = (unsigned long) val; | ||
| 167 | } | ||
| 168 | |||
| 169 | static void clearpair(struct btree_geo *geo, unsigned long *node, int n) | ||
| 170 | { | ||
| 171 | longset(bkey(geo, node, n), 0, geo->keylen); | ||
| 172 | node[geo->no_longs + n] = 0; | ||
| 173 | } | ||
| 174 | |||
| 175 | static inline void __btree_init(struct btree_head *head) | ||
| 176 | { | ||
| 177 | head->node = NULL; | ||
| 178 | head->height = 0; | ||
| 179 | } | ||
| 180 | |||
| 181 | void btree_init_mempool(struct btree_head *head, mempool_t *mempool) | ||
| 182 | { | ||
| 183 | __btree_init(head); | ||
| 184 | head->mempool = mempool; | ||
| 185 | } | ||
| 186 | EXPORT_SYMBOL_GPL(btree_init_mempool); | ||
| 187 | |||
