1 #ifndef _LINUX_GENERIC_RADIX_TREE_H 2 #define _LINUX_GENERIC_RADIX_TREE_H 3 4 /** 5 * DOC: Generic radix trees/sparse arrays 6 * 7 * Very simple and minimalistic, supporting arbitrary size entries up to 8 * GENRADIX_NODE_SIZE. 9 * 10 * A genradix is defined with the type it will store, like so: 11 * 12 * static GENRADIX(struct foo) foo_genradix; 13 * 14 * The main operations are: 15 * 16 * - genradix_init(radix) - initialize an empty genradix 17 * 18 * - genradix_free(radix) - free all memory owned by the genradix and 19 * reinitialize it 20 * 21 * - genradix_ptr(radix, idx) - gets a pointer to the entry at idx, returning 22 * NULL if that entry does not exist 23 * 24 * - genradix_ptr_alloc(radix, idx, gfp) - gets a pointer to an entry, 25 * allocating it if necessary 26 * 27 * - genradix_for_each(radix, iter, p) - iterate over each entry in a genradix 28 * 29 * The radix tree allocates one page of entries at a time, so entries may exist 30 * that were never explicitly allocated - they will be initialized to all 31 * zeroes. 32 * 33 * Internally, a genradix is just a radix tree of pages, and indexing works in 34 * terms of byte offsets. The wrappers in this header file use sizeof on the 35 * type the radix contains to calculate a byte offset from the index - see 36 * __idx_to_offset. 37 */ 38 39 #include <asm/page.h> 40 #include <linux/bug.h> 41 #include <linux/limits.h> 42 #include <linux/log2.h> 43 #include <linux/math.h> 44 #include <linux/types.h> 45 46 struct genradix_root; 47 48 #define GENRADIX_NODE_SHIFT 9 49 #define GENRADIX_NODE_SIZE (1U << GENRADIX_NODE_SHIFT) 50 51 struct __genradix { 52 struct genradix_root *root; 53 }; 54 55 /* 56 * NOTE: currently, sizeof(_type) must not be larger than GENRADIX_NODE_SIZE: 57 */ 58 59 #define __GENRADIX_INITIALIZER \ 60 { \ 61 .tree = { \ 62 .root = NULL, \ 63 } \ 64 } 65 66 /* 67 * We use a 0 size array to stash the type we're storing without taking any 68 * space at runtime - then the various accessor macros can use typeof() to get 69 * to it for casts/sizeof - we also force the alignment so that storing a type 70 * with a ridiculous alignment doesn't blow up the alignment or size of the 71 * genradix. 72 */ 73 74 #define GENRADIX(_type) \ 75 struct { \ 76 struct __genradix tree; \ 77 _type type[0] __aligned(1); \ 78 } 79 80 #define DEFINE_GENRADIX(_name, _type) \ 81 GENRADIX(_type) _name = __GENRADIX_INITIALIZER 82 83 /** 84 * genradix_init - initialize a genradix 85 * @_radix: genradix to initialize 86 * 87 * Does not fail 88 */ 89 #define genradix_init(_radix) \ 90 do { \ 91 *(_radix) = (typeof(*_radix)) __GENRADIX_INITIALIZER; \ 92 } while (0) 93 94 void __genradix_free(struct __genradix *); 95 96 /** 97 * genradix_free: free all memory owned by a genradix 98 * @_radix: the genradix to free 99 * 100 * After freeing, @_radix will be reinitialized and empty 101 */ 102 #define genradix_free(_radix) __genradix_free(&(_radix)->tree) 103 104 static inline size_t __idx_to_offset(size_t idx, size_t obj_size) 105 { 106 if (__builtin_constant_p(obj_size)) 107 BUILD_BUG_ON(obj_size > GENRADIX_NODE_SIZE); 108 else 109 BUG_ON(obj_size > GENRADIX_NODE_SIZE); 110 111 if (!is_power_of_2(obj_size)) { 112 size_t objs_per_page = GENRADIX_NODE_SIZE / obj_size; 113 114 return (idx / objs_per_page) * GENRADIX_NODE_SIZE + 115 (idx % objs_per_page) * obj_size; 116 } else { 117 return idx * obj_size; 118 } 119 } 120 121 #define __genradix_cast(_radix) (typeof((_radix)->type[0]) *) 122 #define __genradix_obj_size(_radix) sizeof((_radix)->type[0]) 123 #define __genradix_objs_per_page(_radix) \ 124 (GENRADIX_NODE_SIZE / sizeof((_radix)->type[0])) 125 #define __genradix_page_remainder(_radix) \ 126 (GENRADIX_NODE_SIZE % sizeof((_radix)->type[0])) 127 128 #define __genradix_idx_to_offset(_radix, _idx) \ 129 __idx_to_offset(_idx, __genradix_obj_size(_radix)) 130 131 void *__genradix_ptr(struct __genradix *, size_t); 132 133 /** 134 * genradix_ptr - get a pointer to a genradix entry 135 * @_radix: genradix to access 136 * @_idx: index to fetch 137 * 138 * Returns a pointer to entry at @_idx, or NULL if that entry does not exist. 139 */ 140 #define genradix_ptr(_radix, _idx) \ 141 (__genradix_cast(_radix) \ 142 __genradix_ptr(&(_radix)->tree, \ 143 __genradix_idx_to_offset(_radix, _idx))) 144 145 void *__genradix_ptr_alloc(struct __genradix *, size_t, gfp_t); 146 147 /** 148 * genradix_ptr_alloc - get a pointer to a genradix entry, allocating it 149 * if necessary 150 * @_radix: genradix to access 151 * @_idx: index to fetch 152 * @_gfp: gfp mask 153 * 154 * Returns a pointer to entry at @_idx, or NULL on allocation failure 155 */ 156 #define genradix_ptr_alloc(_radix, _idx, _gfp) \ 157 (__genradix_cast(_radix) \ 158 __genradix_ptr_alloc(&(_radix)->tree, \ 159 __genradix_idx_to_offset(_radix, _idx), \ 160 _gfp)) 161 162 struct genradix_iter { 163 size_t offset; 164 size_t pos; 165 }; 166 167 /** 168 * genradix_iter_init - initialize a genradix_iter 169 * @_radix: genradix that will be iterated over 170 * @_idx: index to start iterating from 171 */ 172 #define genradix_iter_init(_radix, _idx) \ 173 ((struct genradix_iter) { \ 174 .pos = (_idx), \ 175 .offset = __genradix_idx_to_offset((_radix), (_idx)),\ 176 }) 177 178 void *__genradix_iter_peek(struct genradix_iter *, struct __genradix *, size_t); 179 180 /** 181 * genradix_iter_peek - get first entry at or above iterator's current 182 * position 183 * @_iter: a genradix_iter 184 * @_radix: genradix being iterated over 185 * 186 * If no more entries exist at or above @_iter's current position, returns NULL 187 */ 188 #define genradix_iter_peek(_iter, _radix) \ 189 (__genradix_cast(_radix) \ 190 __genradix_iter_peek(_iter, &(_radix)->tree, \ 191 __genradix_objs_per_page(_radix))) 192 193 void *__genradix_iter_peek_prev(struct genradix_iter *, struct __genradix *, 194 size_t, size_t); 195 196 /** 197 * genradix_iter_peek_prev - get first entry at or below iterator's current 198 * position 199 * @_iter: a genradix_iter 200 * @_radix: genradix being iterated over 201 * 202 * If no more entries exist at or below @_iter's current position, returns NULL 203 */ 204 #define genradix_iter_peek_prev(_iter, _radix) \ 205 (__genradix_cast(_radix) \ 206 __genradix_iter_peek_prev(_iter, &(_radix)->tree, \ 207 __genradix_objs_per_page(_radix), \ 208 __genradix_obj_size(_radix) + \ 209 __genradix_page_remainder(_radix))) 210 211 static inline void __genradix_iter_advance(struct genradix_iter *iter, 212 size_t obj_size) 213 { 214 if (iter->offset + obj_size < iter->offset) { 215 iter->offset = SIZE_MAX; 216 iter->pos = SIZE_MAX; 217 return; 218 } 219 220 iter->offset += obj_size; 221 222 if (!is_power_of_2(obj_size) && 223 (iter->offset & (GENRADIX_NODE_SIZE - 1)) + obj_size > GENRADIX_NODE_SIZE) 224 iter->offset = round_up(iter->offset, GENRADIX_NODE_SIZE); 225 226 iter->pos++; 227 } 228 229 #define genradix_iter_advance(_iter, _radix) \ 230 __genradix_iter_advance(_iter, __genradix_obj_size(_radix)) 231 232 static inline void __genradix_iter_rewind(struct genradix_iter *iter, 233 size_t obj_size) 234 { 235 if (iter->offset == 0 || 236 iter->offset == SIZE_MAX) { 237 iter->offset = SIZE_MAX; 238 return; 239 } 240 241 if ((iter->offset & (GENRADIX_NODE_SIZE - 1)) == 0) 242 iter->offset -= GENRADIX_NODE_SIZE % obj_size; 243 244 iter->offset -= obj_size; 245 iter->pos--; 246 } 247 248 #define genradix_iter_rewind(_iter, _radix) \ 249 __genradix_iter_rewind(_iter, __genradix_obj_size(_radix)) 250 251 #define genradix_for_each_from(_radix, _iter, _p, _start) \ 252 for (_iter = genradix_iter_init(_radix, _start); \ 253 (_p = genradix_iter_peek(&_iter, _radix)) != NULL; \ 254 genradix_iter_advance(&_iter, _radix)) 255 256 /** 257 * genradix_for_each - iterate over entry in a genradix 258 * @_radix: genradix to iterate over 259 * @_iter: a genradix_iter to track current position 260 * @_p: pointer to genradix entry type 261 * 262 * On every iteration, @_p will point to the current entry, and @_iter.pos 263 * will be the current entry's index. 264 */ 265 #define genradix_for_each(_radix, _iter, _p) \ 266 genradix_for_each_from(_radix, _iter, _p, 0) 267 268 #define genradix_last_pos(_radix) \ 269 (SIZE_MAX / GENRADIX_NODE_SIZE * __genradix_objs_per_page(_radix) - 1) 270 271 /** 272 * genradix_for_each_reverse - iterate over entry in a genradix, reverse order 273 * @_radix: genradix to iterate over 274 * @_iter: a genradix_iter to track current position 275 * @_p: pointer to genradix entry type 276 * 277 * On every iteration, @_p will point to the current entry, and @_iter.pos 278 * will be the current entry's index. 279 */ 280 #define genradix_for_each_reverse(_radix, _iter, _p) \ 281 for (_iter = genradix_iter_init(_radix, genradix_last_pos(_radix));\ 282 (_p = genradix_iter_peek_prev(&_iter, _radix)) != NULL;\ 283 genradix_iter_rewind(&_iter, _radix)) 284 285 int __genradix_prealloc(struct __genradix *, size_t, gfp_t); 286 287 /** 288 * genradix_prealloc - preallocate entries in a generic radix tree 289 * @_radix: genradix to preallocate 290 * @_nr: number of entries to preallocate 291 * @_gfp: gfp mask 292 * 293 * Returns 0 on success, -ENOMEM on failure 294 */ 295 #define genradix_prealloc(_radix, _nr, _gfp) \ 296 __genradix_prealloc(&(_radix)->tree, \ 297 __genradix_idx_to_offset(_radix, _nr + 1),\ 298 _gfp) 299 300 301 #endif /* _LINUX_GENERIC_RADIX_TREE_H */ 302
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.