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