diff options
| author | Matthew Wilcox <willy@infradead.org> | 2017-11-07 16:30:10 -0500 |
|---|---|---|
| committer | Matthew Wilcox <willy@infradead.org> | 2018-10-21 10:45:53 -0400 |
| commit | f8d5d0cc145cc21bfc56ef807dc28102aebbf228 (patch) | |
| tree | 0fcba575e83fb02fd2cb49df1ce1cc55bcd927d3 /include/linux/xarray.h | |
| parent | 02c02bf12c5d838603eed44195d3e91f094e2ab2 (diff) | |
xarray: Add definition of struct xarray
This is a direct replacement for struct radix_tree_root. Some of the
struct members have changed name; convert those, and use a #define so
that radix_tree users continue to work without change.
Signed-off-by: Matthew Wilcox <willy@infradead.org>
Reviewed-by: Josef Bacik <jbacik@fb.com>
Diffstat (limited to 'include/linux/xarray.h')
| -rw-r--r-- | include/linux/xarray.h | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/include/linux/xarray.h b/include/linux/xarray.h index 4d1cd7a083e8..9122cf8bf52a 100644 --- a/include/linux/xarray.h +++ b/include/linux/xarray.h | |||
| @@ -10,6 +10,8 @@ | |||
| 10 | */ | 10 | */ |
| 11 | 11 | ||
| 12 | #include <linux/bug.h> | 12 | #include <linux/bug.h> |
| 13 | #include <linux/compiler.h> | ||
| 14 | #include <linux/kconfig.h> | ||
| 13 | #include <linux/spinlock.h> | 15 | #include <linux/spinlock.h> |
| 14 | #include <linux/types.h> | 16 | #include <linux/types.h> |
| 15 | 17 | ||
| @@ -153,6 +155,74 @@ static inline bool xa_is_internal(const void *entry) | |||
| 153 | return ((unsigned long)entry & 3) == 2; | 155 | return ((unsigned long)entry & 3) == 2; |
| 154 | } | 156 | } |
| 155 | 157 | ||
| 158 | /** | ||
| 159 | * struct xarray - The anchor of the XArray. | ||
| 160 | * @xa_lock: Lock that protects the contents of the XArray. | ||
| 161 | * | ||
| 162 | * To use the xarray, define it statically or embed it in your data structure. | ||
| 163 | * It is a very small data structure, so it does not usually make sense to | ||
| 164 | * allocate it separately and keep a pointer to it in your data structure. | ||
| 165 | * | ||
| 166 | * You may use the xa_lock to protect your own data structures as well. | ||
| 167 | */ | ||
| 168 | /* | ||
| 169 | * If all of the entries in the array are NULL, @xa_head is a NULL pointer. | ||
| 170 | * If the only non-NULL entry in the array is at index 0, @xa_head is that | ||
| 171 | * entry. If any other entry in the array is non-NULL, @xa_head points | ||
| 172 | * to an @xa_node. | ||
| 173 | */ | ||
| 174 | struct xarray { | ||
| 175 | spinlock_t xa_lock; | ||
| 176 | /* private: The rest of the data structure is not to be used directly. */ | ||
| 177 | gfp_t xa_flags; | ||
| 178 | void __rcu * xa_head; | ||
| 179 | }; | ||
| 180 | |||
| 181 | #define XARRAY_INIT(name, flags) { \ | ||
| 182 | .xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock), \ | ||
| 183 | .xa_flags = flags, \ | ||
| 184 | .xa_head = NULL, \ | ||
| 185 | } | ||
| 186 | |||
| 187 | /** | ||
| 188 | * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags. | ||
| 189 | * @name: A string that names your XArray. | ||
| 190 | * @flags: XA_FLAG values. | ||
| 191 | * | ||
| 192 | * This is intended for file scope definitions of XArrays. It declares | ||
| 193 | * and initialises an empty XArray with the chosen name and flags. It is | ||
| 194 | * equivalent to calling xa_init_flags() on the array, but it does the | ||
| 195 | * initialisation at compiletime instead of runtime. | ||
| 196 | */ | ||
| 197 | #define DEFINE_XARRAY_FLAGS(name, flags) \ | ||
| 198 | struct xarray name = XARRAY_INIT(name, flags) | ||
| 199 | |||
| 200 | /** | ||
| 201 | * DEFINE_XARRAY() - Define an XArray. | ||
| 202 | * @name: A string that names your XArray. | ||
| 203 | * | ||
| 204 | * This is intended for file scope definitions of XArrays. It declares | ||
| 205 | * and initialises an empty XArray with the chosen name. It is equivalent | ||
| 206 | * to calling xa_init() on the array, but it does the initialisation at | ||
| 207 | * compiletime instead of runtime. | ||
| 208 | */ | ||
| 209 | #define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0) | ||
| 210 | |||
| 211 | void xa_init_flags(struct xarray *, gfp_t flags); | ||
| 212 | |||
| 213 | /** | ||
| 214 | * xa_init() - Initialise an empty XArray. | ||
| 215 | * @xa: XArray. | ||
| 216 | * | ||
| 217 | * An empty XArray is full of NULL entries. | ||
| 218 | * | ||
| 219 | * Context: Any context. | ||
| 220 | */ | ||
| 221 | static inline void xa_init(struct xarray *xa) | ||
| 222 | { | ||
| 223 | xa_init_flags(xa, 0); | ||
| 224 | } | ||
| 225 | |||
| 156 | #define xa_trylock(xa) spin_trylock(&(xa)->xa_lock) | 226 | #define xa_trylock(xa) spin_trylock(&(xa)->xa_lock) |
| 157 | #define xa_lock(xa) spin_lock(&(xa)->xa_lock) | 227 | #define xa_lock(xa) spin_lock(&(xa)->xa_lock) |
| 158 | #define xa_unlock(xa) spin_unlock(&(xa)->xa_lock) | 228 | #define xa_unlock(xa) spin_unlock(&(xa)->xa_lock) |
