Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /* Private definitions for the generic associative array implementation.
0003  *
0004  * See Documentation/core-api/assoc_array.rst for information.
0005  *
0006  * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
0007  * Written by David Howells (dhowells@redhat.com)
0008  */
0009 
0010 #ifndef _LINUX_ASSOC_ARRAY_PRIV_H
0011 #define _LINUX_ASSOC_ARRAY_PRIV_H
0012 
0013 #ifdef CONFIG_ASSOCIATIVE_ARRAY
0014 
0015 #include <linux/assoc_array.h>
0016 
0017 #define ASSOC_ARRAY_FAN_OUT     16  /* Number of slots per node */
0018 #define ASSOC_ARRAY_FAN_MASK        (ASSOC_ARRAY_FAN_OUT - 1)
0019 #define ASSOC_ARRAY_LEVEL_STEP      (ilog2(ASSOC_ARRAY_FAN_OUT))
0020 #define ASSOC_ARRAY_LEVEL_STEP_MASK (ASSOC_ARRAY_LEVEL_STEP - 1)
0021 #define ASSOC_ARRAY_KEY_CHUNK_MASK  (ASSOC_ARRAY_KEY_CHUNK_SIZE - 1)
0022 #define ASSOC_ARRAY_KEY_CHUNK_SHIFT (ilog2(BITS_PER_LONG))
0023 
0024 /*
0025  * Undefined type representing a pointer with type information in the bottom
0026  * two bits.
0027  */
0028 struct assoc_array_ptr;
0029 
0030 /*
0031  * An N-way node in the tree.
0032  *
0033  * Each slot contains one of four things:
0034  *
0035  *  (1) Nothing (NULL).
0036  *
0037  *  (2) A leaf object (pointer types 0).
0038  *
0039  *  (3) A next-level node (pointer type 1, subtype 0).
0040  *
0041  *  (4) A shortcut (pointer type 1, subtype 1).
0042  *
0043  * The tree is optimised for search-by-ID, but permits reasonable iteration
0044  * also.
0045  *
0046  * The tree is navigated by constructing an index key consisting of an array of
0047  * segments, where each segment is ilog2(ASSOC_ARRAY_FAN_OUT) bits in size.
0048  *
0049  * The segments correspond to levels of the tree (the first segment is used at
0050  * level 0, the second at level 1, etc.).
0051  */
0052 struct assoc_array_node {
0053     struct assoc_array_ptr  *back_pointer;
0054     u8          parent_slot;
0055     struct assoc_array_ptr  *slots[ASSOC_ARRAY_FAN_OUT];
0056     unsigned long       nr_leaves_on_branch;
0057 };
0058 
0059 /*
0060  * A shortcut through the index space out to where a collection of nodes/leaves
0061  * with the same IDs live.
0062  */
0063 struct assoc_array_shortcut {
0064     struct assoc_array_ptr  *back_pointer;
0065     int         parent_slot;
0066     int         skip_to_level;
0067     struct assoc_array_ptr  *next_node;
0068     unsigned long       index_key[];
0069 };
0070 
0071 /*
0072  * Preallocation cache.
0073  */
0074 struct assoc_array_edit {
0075     struct rcu_head         rcu;
0076     struct assoc_array      *array;
0077     const struct assoc_array_ops    *ops;
0078     const struct assoc_array_ops    *ops_for_excised_subtree;
0079     struct assoc_array_ptr      *leaf;
0080     struct assoc_array_ptr      **leaf_p;
0081     struct assoc_array_ptr      *dead_leaf;
0082     struct assoc_array_ptr      *new_meta[3];
0083     struct assoc_array_ptr      *excised_meta[1];
0084     struct assoc_array_ptr      *excised_subtree;
0085     struct assoc_array_ptr      **set_backpointers[ASSOC_ARRAY_FAN_OUT];
0086     struct assoc_array_ptr      *set_backpointers_to;
0087     struct assoc_array_node     *adjust_count_on;
0088     long                adjust_count_by;
0089     struct {
0090         struct assoc_array_ptr  **ptr;
0091         struct assoc_array_ptr  *to;
0092     } set[2];
0093     struct {
0094         u8          *p;
0095         u8          to;
0096     } set_parent_slot[1];
0097     u8              segment_cache[ASSOC_ARRAY_FAN_OUT + 1];
0098 };
0099 
0100 /*
0101  * Internal tree member pointers are marked in the bottom one or two bits to
0102  * indicate what type they are so that we don't have to look behind every
0103  * pointer to see what it points to.
0104  *
0105  * We provide functions to test type annotations and to create and translate
0106  * the annotated pointers.
0107  */
0108 #define ASSOC_ARRAY_PTR_TYPE_MASK 0x1UL
0109 #define ASSOC_ARRAY_PTR_LEAF_TYPE 0x0UL /* Points to leaf (or nowhere) */
0110 #define ASSOC_ARRAY_PTR_META_TYPE 0x1UL /* Points to node or shortcut */
0111 #define ASSOC_ARRAY_PTR_SUBTYPE_MASK    0x2UL
0112 #define ASSOC_ARRAY_PTR_NODE_SUBTYPE    0x0UL
0113 #define ASSOC_ARRAY_PTR_SHORTCUT_SUBTYPE 0x2UL
0114 
0115 static inline bool assoc_array_ptr_is_meta(const struct assoc_array_ptr *x)
0116 {
0117     return (unsigned long)x & ASSOC_ARRAY_PTR_TYPE_MASK;
0118 }
0119 static inline bool assoc_array_ptr_is_leaf(const struct assoc_array_ptr *x)
0120 {
0121     return !assoc_array_ptr_is_meta(x);
0122 }
0123 static inline bool assoc_array_ptr_is_shortcut(const struct assoc_array_ptr *x)
0124 {
0125     return (unsigned long)x & ASSOC_ARRAY_PTR_SUBTYPE_MASK;
0126 }
0127 static inline bool assoc_array_ptr_is_node(const struct assoc_array_ptr *x)
0128 {
0129     return !assoc_array_ptr_is_shortcut(x);
0130 }
0131 
0132 static inline void *assoc_array_ptr_to_leaf(const struct assoc_array_ptr *x)
0133 {
0134     return (void *)((unsigned long)x & ~ASSOC_ARRAY_PTR_TYPE_MASK);
0135 }
0136 
0137 static inline
0138 unsigned long __assoc_array_ptr_to_meta(const struct assoc_array_ptr *x)
0139 {
0140     return (unsigned long)x &
0141         ~(ASSOC_ARRAY_PTR_SUBTYPE_MASK | ASSOC_ARRAY_PTR_TYPE_MASK);
0142 }
0143 static inline
0144 struct assoc_array_node *assoc_array_ptr_to_node(const struct assoc_array_ptr *x)
0145 {
0146     return (struct assoc_array_node *)__assoc_array_ptr_to_meta(x);
0147 }
0148 static inline
0149 struct assoc_array_shortcut *assoc_array_ptr_to_shortcut(const struct assoc_array_ptr *x)
0150 {
0151     return (struct assoc_array_shortcut *)__assoc_array_ptr_to_meta(x);
0152 }
0153 
0154 static inline
0155 struct assoc_array_ptr *__assoc_array_x_to_ptr(const void *p, unsigned long t)
0156 {
0157     return (struct assoc_array_ptr *)((unsigned long)p | t);
0158 }
0159 static inline
0160 struct assoc_array_ptr *assoc_array_leaf_to_ptr(const void *p)
0161 {
0162     return __assoc_array_x_to_ptr(p, ASSOC_ARRAY_PTR_LEAF_TYPE);
0163 }
0164 static inline
0165 struct assoc_array_ptr *assoc_array_node_to_ptr(const struct assoc_array_node *p)
0166 {
0167     return __assoc_array_x_to_ptr(
0168         p, ASSOC_ARRAY_PTR_META_TYPE | ASSOC_ARRAY_PTR_NODE_SUBTYPE);
0169 }
0170 static inline
0171 struct assoc_array_ptr *assoc_array_shortcut_to_ptr(const struct assoc_array_shortcut *p)
0172 {
0173     return __assoc_array_x_to_ptr(
0174         p, ASSOC_ARRAY_PTR_META_TYPE | ASSOC_ARRAY_PTR_SHORTCUT_SUBTYPE);
0175 }
0176 
0177 #endif /* CONFIG_ASSOCIATIVE_ARRAY */
0178 #endif /* _LINUX_ASSOC_ARRAY_PRIV_H */