GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / include / linux / radix-tree.h
blob9f38fe50217e0d54d01865efa93840dff92c58bf
1 /*
2 * Copyright (C) 2001 Momchil Velikov
3 * Portions Copyright (C) 2001 Christoph Hellwig
4 * Copyright (C) 2006 Nick Piggin
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef _LINUX_RADIX_TREE_H
21 #define _LINUX_RADIX_TREE_H
23 #include <linux/preempt.h>
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/rcupdate.h>
29 * An indirect pointer (root->rnode pointing to a radix_tree_node, rather
30 * than a data item) is signalled by the low bit set in the root->rnode
31 * pointer.
33 * In this case root->height is > 0, but the indirect pointer tests are
34 * needed for RCU lookups (because root->height is unreliable). The only
35 * time callers need worry about this is when doing a lookup_slot under
36 * RCU.
38 #define RADIX_TREE_INDIRECT_PTR 1
40 static inline int radix_tree_is_indirect_ptr(void *ptr)
42 return (int)((unsigned long)ptr & RADIX_TREE_INDIRECT_PTR);
45 /*** radix-tree API starts here ***/
47 #define RADIX_TREE_MAX_TAGS 3
49 /* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */
50 struct radix_tree_root {
51 unsigned int height;
52 gfp_t gfp_mask;
53 struct radix_tree_node *rnode;
56 #define RADIX_TREE_INIT(mask) { \
57 .height = 0, \
58 .gfp_mask = (mask), \
59 .rnode = NULL, \
62 #define RADIX_TREE(name, mask) \
63 struct radix_tree_root name = RADIX_TREE_INIT(mask)
65 #define INIT_RADIX_TREE(root, mask) \
66 do { \
67 (root)->height = 0; \
68 (root)->gfp_mask = (mask); \
69 (root)->rnode = NULL; \
70 } while (0)
72 /**
73 * Radix-tree synchronization
75 * The radix-tree API requires that users provide all synchronisation (with
76 * specific exceptions, noted below).
78 * Synchronization of access to the data items being stored in the tree, and
79 * management of their lifetimes must be completely managed by API users.
81 * For API usage, in general,
82 * - any function _modifying_ the tree or tags (inserting or deleting
83 * items, setting or clearing tags) must exclude other modifications, and
84 * exclude any functions reading the tree.
85 * - any function _reading_ the tree or tags (looking up items or tags,
86 * gang lookups) must exclude modifications to the tree, but may occur
87 * concurrently with other readers.
89 * The notable exceptions to this rule are the following functions:
90 * radix_tree_lookup
91 * radix_tree_lookup_slot
92 * radix_tree_tag_get
93 * radix_tree_gang_lookup
94 * radix_tree_gang_lookup_slot
95 * radix_tree_gang_lookup_tag
96 * radix_tree_gang_lookup_tag_slot
97 * radix_tree_tagged
99 * The first 7 functions are able to be called locklessly, using RCU. The
100 * caller must ensure calls to these functions are made within rcu_read_lock()
101 * regions. Other readers (lock-free or otherwise) and modifications may be
102 * running concurrently.
104 * It is still required that the caller manage the synchronization and lifetimes
105 * of the items. So if RCU lock-free lookups are used, typically this would mean
106 * that the items have their own locks, or are amenable to lock-free access; and
107 * that the items are freed by RCU (or only freed after having been deleted from
108 * the radix tree *and* a synchronize_rcu() grace period).
110 * (Note, rcu_assign_pointer and rcu_dereference are not needed to control
111 * access to data items when inserting into or looking up from the radix tree)
113 * Note that the value returned by radix_tree_tag_get() may not be relied upon
114 * if only the RCU read lock is held. Functions to set/clear tags and to
115 * delete nodes running concurrently with it may affect its result such that
116 * two consecutive reads in the same locked section may return different
117 * values. If reliability is required, modification functions must also be
118 * excluded from concurrency.
120 * radix_tree_tagged is able to be called without locking or RCU.
124 * radix_tree_deref_slot - dereference a slot
125 * @pslot: pointer to slot, returned by radix_tree_lookup_slot
126 * Returns: item that was stored in that slot with any direct pointer flag
127 * removed.
129 * For use with radix_tree_lookup_slot(). Caller must hold tree at least read
130 * locked across slot lookup and dereference. Not required if write lock is
131 * held (ie. items cannot be concurrently inserted).
133 * radix_tree_deref_retry must be used to confirm validity of the pointer if
134 * only the read lock is held.
136 static inline void *radix_tree_deref_slot(void **pslot)
138 return rcu_dereference(*pslot);
142 * radix_tree_deref_retry - check radix_tree_deref_slot
143 * @arg: pointer returned by radix_tree_deref_slot
144 * Returns: 0 if retry is not required, otherwise retry is required
146 * radix_tree_deref_retry must be used with radix_tree_deref_slot.
148 static inline int radix_tree_deref_retry(void *arg)
150 return unlikely((unsigned long)arg & RADIX_TREE_INDIRECT_PTR);
154 * radix_tree_replace_slot - replace item in a slot
155 * @pslot: pointer to slot, returned by radix_tree_lookup_slot
156 * @item: new item to store in the slot.
158 * For use with radix_tree_lookup_slot(). Caller must hold tree write locked
159 * across slot lookup and replacement.
161 static inline void radix_tree_replace_slot(void **pslot, void *item)
163 BUG_ON(radix_tree_is_indirect_ptr(item));
164 rcu_assign_pointer(*pslot, item);
167 int radix_tree_insert(struct radix_tree_root *, unsigned long, void *);
168 void *radix_tree_lookup(struct radix_tree_root *, unsigned long);
169 void **radix_tree_lookup_slot(struct radix_tree_root *, unsigned long);
170 void *radix_tree_delete(struct radix_tree_root *, unsigned long);
171 unsigned int
172 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
173 unsigned long first_index, unsigned int max_items);
174 unsigned int
175 radix_tree_gang_lookup_slot(struct radix_tree_root *root, void ***results,
176 unsigned long first_index, unsigned int max_items);
177 unsigned long radix_tree_next_hole(struct radix_tree_root *root,
178 unsigned long index, unsigned long max_scan);
179 unsigned long radix_tree_prev_hole(struct radix_tree_root *root,
180 unsigned long index, unsigned long max_scan);
181 int radix_tree_preload(gfp_t gfp_mask);
182 void radix_tree_init(void);
183 void *radix_tree_tag_set(struct radix_tree_root *root,
184 unsigned long index, unsigned int tag);
185 void *radix_tree_tag_clear(struct radix_tree_root *root,
186 unsigned long index, unsigned int tag);
187 int radix_tree_tag_get(struct radix_tree_root *root,
188 unsigned long index, unsigned int tag);
189 unsigned int
190 radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
191 unsigned long first_index, unsigned int max_items,
192 unsigned int tag);
193 unsigned int
194 radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
195 unsigned long first_index, unsigned int max_items,
196 unsigned int tag);
197 unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
198 unsigned long *first_indexp, unsigned long last_index,
199 unsigned long nr_to_tag,
200 unsigned int fromtag, unsigned int totag);
201 int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);
203 static inline void radix_tree_preload_end(void)
205 preempt_enable();
208 #endif /* _LINUX_RADIX_TREE_H */