OpenSDN source code
bitvector.h
Go to the documentation of this file.
1 /* $Id: bitvector.h 346474 2009-11-14 10:18:58Z ssiano $
2  *
3  * bitvector.h - Bit vector manipulation definitions
4  *
5  * Dave Katz, March 2008
6  *
7  * Copyright (c) 2008, Juniper Networks, Inc.
8  * Copyright Contributors to the OpenSDN project
9  * All rights reserved.
10  */
11 
12 /*
13  * Overview
14  *
15  * This module provides generalized manipulation of arbitrary-size
16  * bit vectors. Single-bit operations are provided (set, clear, test,
17  * find) as well as vector set operations (and, or, xor, clear, copy,
18  * walk).
19  *
20  * When performing vector operations, a callback can optionally
21  * be provided that will be called either for every bit in the result
22  * that changes, or any bit that is set after the operation. This allows
23  * for efficient generalized vector operations for which the bit vectors
24  * represent set elements.
25  *
26  * The bit vector is represented by a bit_vector structure, typically
27  * embedded in another structure, to which are attached zero or more
28  * bit vector entries. This allows for sparse bit vectors, as vector
29  * entries are only allocated for bits that are set.
30  *
31  * Vector operations are normally fairly expensive, as per-bit
32  * analysis must be done in order to perform per-bit callbacks and to
33  * keep track of how many bits in each entry are set in order to do
34  * pruning of the tree as entries are completely cleared. However,
35  * vector operations can be significantly sped up if the "fast_vects"
36  * flag is set on the call to bv_init_vector--the vector operations
37  * then become word-sized and no per-bit operations are done. The
38  * downside to this is that pruning the tree of empty blocks does not
39  * generally occur (which can eat memory), and this in turn can slow
40  * down bit-find operations. It is up to the application to weigh the
41  * tradeoffs.
42  *
43  *
44  * Callbacks
45  *
46  * For vector set operations, an optional callback is provided with
47  * the bit number, the old and new values of the bit, and the context
48  * pointer provided by the original caller. If no callback is
49  * supplied, the only action is to store the results of the set
50  * operation.
51  *
52  * If the callback option BV_CALL_SET is used, the callback will be
53  * called for every bit in the result that is set to one. If
54  * BV_CALL_CHANGE is used, the callback will be called for every bit
55  * in the result that changed.
56  *
57  * If a callback is specified, the result vector pointer may be NULL;
58  * in this case the previous value of the result vector is assumed to
59  * be zero for purposes of deciding which bits should be called back
60  * about (and the results of the operation are otherwise discarded.)
61  * Note that in this case, BV_CALL_SET and BV_CALL_CHANGE will give
62  * the same results.
63  *
64  * A callback can do almost anything *except* attempt any operations
65  * of any sort on the result vector, because it will be in an
66  * inconsistent state at the time of the callback. This is enforced
67  * by assertion. Callbacks must glean any information by other means.
68  * A callback may set or clear the current bit in one of the source
69  * vectors, but may not touch any other bit (for the same reason.)
70  */
71 
72 /*
73  * Calling Sequence
74  *
75  * Each bitvector to be manipulated must be first initialized with a call
76  * to bv_init_vector() prior to use. Once initialized, the contents
77  * cannot be manipulated directly, and the structure containing the vector
78  * must not be freed, lest memory leaks result.
79  *
80  * After initialization, any of the single bit or vector operations may be
81  * performed.
82  *
83  * When the vector is no longer needed, either bv_clean() or
84  * bv_clear_all_bits() must be called to clean up and free memory. Once
85  * this is done, the bitvector can be discarded.
86  *
87  *
88  * Initialization:
89  *
90  * bv_init_vector(bit_vector *bv, boolean fast_vects)
91  * Initialize a new bit vector structure. "fast_vects" specifies whether
92  * the caller wishes to use fast vector operations (see above for details.)
93  *
94  * bv_clean(bit_vector *bv)
95  * Clears all bits and frees all memory associated with a bit vector.
96  * This is a fast way to zero a vector, and is also required to avoid
97  * memory leaks before freeing the structure in which the bit_vector
98  * structure is embedded.
99  *
100  *
101  * Bit operations:
102  *
103  * bv_set_bit(bit_vector *bv, bv_bitnum_t bit_number)
104  * Sets the specified bit number. Returns the previous state of the
105  * bit (0 or 1), or -1 of out of memory.
106  *
107  * bv_clear_bit(bit_vector *bv, bv_bitnum_t bit_number)
108  * Clears the specified bit number. Returns the previous state of the
109  * bit (0 or 1), or -1 of out of memory.
110  *
111  * bv_bit_is_set(bit_vector *bv, bv_bitnum_t bit_number)
112  * Returns TRUE if the bit is set, or FALSE if the bit is clear.
113  *
114  * bv_bitnum_t bv_first_set_bit(bit_vector *bv)
115  * Returns the bit number of the first set bit in the vector, or
116  * BV_BAD_BITNUM if no bits are set.
117  *
118  * bv_bitnum_t bv_first_clear_bit(bit_vector *bv)
119  * Returns the bit number of the first clear bit in the vector, or
120  * BV_BAD_BITNUM if the bit vector has all bits set and cannot grow
121  * further. This generally will always succeed.
122  *
123  * bv_bitnum_t bv_find_clear_bit(bit_vector *bv)
124  * Same as bv_first_clear_bit, except that the bit number returned is
125  * not necessarily the first clear bit. This is potentially faster
126  * than bv_first_clear_bit but may be less memory efficient, since
127  * the resulting vector may be more sparse.
128  *
129  * boolean bv_empty(bit_vector *bv)
130  * Returns TRUE if the bit vector is empty (no set bits), or FALSE if
131  * not. This is cheaper than using bv_first_set_bit(), for example.
132  *
133  *
134  * Bit vector vperations
135  *
136  * The following calls allow the manipulation of bit vectors as sets. All
137  * of them have the optional callback as described above. A NULL vector
138  * pointer as a source parameter is treated as vector with all bits clear.
139  * A NULL result pointer means that the caller doesn't actually care about
140  * the results as a vector (a callback is required for this to be anything
141  * other than a way to waste CPU.) Each routine returns 0 if all went
142  * OK, or -1 if we ran out of memory.
143  *
144  * The callback must be defined as follows:
145  *
146  * boolean cb_routine(void *context, bv_bitnum_t bit_number, boolean
147  * new_bit_value, boolean old_bit_value)
148  *
149  * Context is the opaque context passed from the original call to
150  * the vector operation. bit_number is the bit number in the
151  * vector being signaled (either because it is set or because it
152  * changed, depending on the original call.) new_bit_value and
153  * old_bit_value are the old and new values of the bit, which may
154  * be changing, depending on the operation. The callback routine
155  * must return TRUE if the operation is to be aborted (which can
156  * save CPU in some cases) or FALSE if it should continue.
157  *
158  *
159  * The operations are:
160  *
161  * int bv_and_vectors(bit_vector *first, bit_vector *second,
162  * bit_vector *result, bv_callback callback,
163  * void *context, bv_callback_option cb_opt)
164  * int bv_or_vectors(bit_vector *first, bit_vector *second,
165  * bit_vector *result, bv_callback callback,
166  * void *context, bv_callback_option cb_opt)
167  * int bv_xor_vectors(bit_vector *first, bit_vector *second,
168  * bit_vector *result, bv_callback callback,
169  * void *context, bv_callback_option cb_opt)
170  * int bv_clear_vectors(bit_vector *first, bit_vector *second,
171  * bit_vector *result, bv_callback callback,
172  * void *context, bv_callback_option cb_opt)
173  * Performs a boolean AND, OR, XOR, or CLEAR operation between
174  * vectors "first" and "second", storing the results in "result",
175  * with optional callback. (A "clear" operation clears any bit
176  * that is set in the second vector.)
177  *
178  * int bv_copy_vector(bit_vector *src, bit_vector *dest,
179  * bv_callback callback, void *context,
180  * bv_callback_option cb_opt)
181  * Copies the contents of vector "src" to vector "dest", with optional
182  * callback.
183  *
184  * int bv_walk_vector(bit_vector *vect, bv_callback callback, void *context,
185  * bv_callback_option cb_opt)
186  * Walks the bit vector, calling back for every bit set in the vector.
187  *
188  * void bv_clear_all_bits(bit_vector *vect, bv_callback callback,
189  * void *context, bv_callback_option cb_opt)
190  * Clears all bits in the vector (like bv_clean()) but does so with a
191  * callback for each bit that's set in the vector.
192  */
193 
194 
195 /*
196  * Environment
197  *
198  * The bitvector code is a portable source library. It requires certain
199  * services from the environment in which it runs. These services are
200  * called through procedures named bvx_<foo> and are listed below.
201  *
202  * The environment is defined by an application-specific file that is
203  * included by the library called "bvx_environment.h". That file typically
204  * contains #defines and typedefs and externs that bind the toolkit's
205  * calls to the local environment.
206  *
207  * The required definitions are as follows. Note that if the exact type
208  * of parameter or return value is not specified, the toolkit treats it in
209  * a generic way and does not explicitly type it (for instance, zero/nonzero
210  * procedure returns.)
211  *
212  * Patricia library:
213  *
214  * bvx_patroot
215  * An opaque type for a patricia root structure
216  *
217  * bvx_patnode
218  * An opaque type for a patricia node
219  *
220  * bvx_patroot *bvx_patroot_init(keylen, offset)
221  * Initializes a patricia root. Keylen is the length of the key in bytes.
222  * Offset is the byte offset from the start of the key to the start of
223  * the patricia node.
224  *
225  * bvx_patnode *bvx_patricia_lookup(bvx_patroot *root, void *key)
226  * Looks up a patricia entry, returns a pointer to the patricia node,
227  * or NULL if not found.
228  *
229  * bvx_patnode *bvx_patricia_lookup_least(bvx_patroot *root)
230  * Looks up the first entry in a patricia tree, returns a pointer to the
231  * patricia node, or NULL if the tree is empty.
232  *
233  * bvx_patnode *bvx_patricia_lookup_greatest(bvx_patroot *root)
234  * Looks up the last entry in a patricia tree, returns a pointer to the
235  * patricia node, or NULL if the tree is empty.
236  *
237  * bvx_patricia_add(bvx_patroot *root, bvx_patnode *node)
238  * Adds the node to the patricia tree. Returns nonzero if successful,
239  * zero if failed (e.g., key already in the tree).
240  *
241  * bvx_patricia_delete(bvx_patroot *root, bvx_patnode *node)
242  * Deletes the node from the patricia tree. Returns nonzero if successful,
243  * zero if failed (e.g., node is not in the tree).
244  *
245  * bvx_patnode *bvx_patricia_get_next(bvx_patroot *root, bvx_patnode *node)
246  * Returns the patricia node following the specified one. If the specified
247  * node is NULL, returns the first node in the tree. Returns NULL if there
248  * are no more nodes in the tree.
249  *
250  * bvx_patroot_destroy(bvx_patroot *root)
251  * Destroys the patricia tree root. The toolkit ensures that all nodes are
252  * first deleted from the tree.
253  *
254  * BVX_PATRICIA_OFFSET(structname, nodefield, keyfield)
255  * Returns the offset in a structure from the patricia node to the
256  * patricia key. This should resolve to a bunch of pointer math. This
257  * is used in calls to bvx_patroot_init().
258  *
259  * BVX_PATNODE_TO_STRUCT(procname, structname, nodefieldname)
260  * Creates an inline procedure that, given a pointer to an embedded
261  * bvx_patnode in a structure, returns a pointer to the enclosing
262  * structure, or returns NULL if the node pointer is NULL.
263  *
264  *
265  * Thread Library:
266  *
267  * The library for doubly-linked-list data structure.
268  *
269  *
270  * Memory management:
271  *
272  * The bitvector toolkit expects a block memory management system, in which
273  * a block size is first defined, and then requests for specific block sizes
274  * are made thereafter (modeled on rpd's memory manager.)
275  *
276  * bvx_block_tag
277  * An opaque type that defines a tag representing a memory block size.
278  * Semantically, this is the size itself (and in fact a sleazy way to
279  * implement this in a naked malloc() environment is to define this as
280  * an int and simply store the block size here.) The tag must be a
281  * scalar (so that it can be compared to zero) and it must be nonzero
282  * when it is valid (since a zero value implies an uninitialized tag.)
283  *
284  * bvx_block_tag bvx_malloc_block_create(size, const char *name)
285  * Create a block tag for memory blocks of a particular size. The name
286  * can be used for diagnostic purposes. Returns the tag.
287  *
288  * void *bvx_malloc_block(bvx_block_tag tag)
289  * Allocate a block of memory of the size denoted by the tag. Returns
290  * a pointer to the block, or NULL if out of memory. The toolkit
291  * does not specify the pointer type of the returned value, but expects
292  * to be able to assign it to any pointer type without coercion (thus
293  * the void *).
294  *
295  * bvx_free_block(bvx_block_tag tag, void *block)
296  * Free a block of memory to the size pool specified by the tag.
297  *
298  *
299  * Miscellaneous stuff:
300  *
301  * bvx_assert(condition)
302  * Crashes if the condition is not true.
303  *
304  * BVX_UNUSED
305  * Used in procedure declarations after unused parameters to avoid
306  * generating compilation warnings, e.g. "int flort(int foo BVX_UNUSED)".
307  *
308  * uint32_t
309  * Must be typedefed to an unsigned, 32 bit scalar.
310  *
311  * boolean, TRUE, FALSE
312  * The boolean type and the TRUE and FALSE constants must be defined.
313  * TRUE must be nonzero, and FALSE must be zero. There is no assumption
314  * about the size of the boolean type.
315  *
316  *
317  * Size definitions:
318  *
319  * The bitvector toolkit requires two items to be specified that impact
320  * how memory is used by the toolkit. They are:
321  *
322  * BV_BITSIZE_LOG2
323  * The base-2 log of the size of a bit vector entry.
324  *
325  * bv_word_t
326  * A type defining the size of a scalar word.
327  *
328  * BV_BITSIZE_LOG2 must be greater than or equal to the base 2 log of the
329  * bit size of bv_word_t. So if bv_word_t is typedefed as a uint32_t,
330  * BV_BITSIZE_LOG2 must be greater than or equal to 5.
331  *
332  * BV_BITSIZE_LOG2 represents the granularity of memory allocated to maintain
333  * the bit vector. Larger values provide lower overhead when using sparse
334  * arrays (since the number of individual memory blocks and the size of the
335  * patricia tree will be smaller), but waste memory if the vector is very
336  * sparse or has very few bits set.
337  *
338  * bv_word_t represents the size of a scalar in which bits are stored. This
339  * type needs to be able to be compared to zero, for example. This ought
340  * to match the largest native scalar type on the processor in use that
341  * is small enough to fit within BV_BITSIZE_LOG2.
342  */
343 
344 #ifndef __BITVECTOR_H__
345 #define __BITVECTOR_H__
346 
347 /*
348  * Bit vector
349  *
350  * A bit vector is a patricia tree of bit vector entries.
351  *
352  * An empty tree may have a null root pointer.
353  *
354  * bv_fastvects is set if the application wants to do fast vector operations
355  * (but see the comments in the front of this file regarding the tradeoffs.)
356  * The side effect of setting bv_fastvects is that the bit count in a vector
357  * entry may become unknown, which in turn means that completely empty
358  * entries may be left in the tree, and that non-full entries may not end
359  * up in the non-full list. This impacts the cost of a search for free
360  * bits.
361  *
362  * bv_nonfull_head is an optimization to help searches for clear bits.
363  * Normally, all entries that aren't full are threaded there in order
364  * to make searching for a free bit cheap. However, if vector
365  * operations are in effect, this list may be incomplete.
366  *
367  * bv_freed_ord is also an optimization to help searches for clear
368  * bits. This is set to be either the starting bit ordinal of a free
369  * (nonexistent) block of bits, or BV_BAD_BITNUM if it is unset. The
370  * code endeavors to keep this set cheaply; it is used as a free bit
371  * number if no allocated blocks with free bits are available.
372  */
373 
374 typedef uint32_t bv_bitnum_t; /* Bit number */
375 #define BV_BAD_BITNUM 0xffffffff /* Illegal bit number */
376 
377 typedef struct bit_vector_ {
378  bvx_patroot *bv_root; /* Patricia root */
379  uint32_t bv_entry_count; /* Number of attached entries */
380  task_thread bv_nonfull_head; /* Head of non-full entries */
381  bv_bitnum_t bv_callback_ord; /* Ordinal of callback bit */
382  bv_bitnum_t bv_freed_ord; /* Ordinal of a freed entry */
383  boolean bv_fastvects; /* Perform fast vector operations */
384  boolean bv_cb_result; /* TRUE if vector is callback result */
385  boolean bv_cb_source; /* TRUE if vector is callback source */
387 
388 /*
389  * Bit vector callback definition. The bit vector routines can call
390  * back to an application as the results of bit vector manipulations
391  * are generated.
392  *
393  * If the callback returns TRUE, the set operation is aborted. This can
394  * save time in some situations.
395  */
396 typedef boolean (*bv_callback)(void *context, bv_bitnum_t bit_number,
397  boolean new_bit_value, boolean old_bit_value);
398 
399 
400 
401 /*
402  * Definitions of bit result callback options.
403  */
404 typedef enum {
405  BV_CALL_CHANGE, /* Call back if result bit changed */
406  BV_CALL_SET /* Call back if result bit is set */
408 
409 /* Externals */
410 
411 extern void bv_clean(bit_vector *bv);
412 extern int bv_set_bit(bit_vector *bv, bv_bitnum_t bit_number);
413 extern boolean bv_clear_bit(bit_vector *bv, bv_bitnum_t bit_number);
414 extern boolean bv_bit_is_set(bit_vector *bv, bv_bitnum_t bit_number);
415 extern void bv_init_vector(bit_vector *bv, boolean fast_vects);
416 extern int bv_and_vectors(bit_vector *first, bit_vector *second,
417  bit_vector *result, bv_callback callback,
418  void *context, bv_callback_option cb_opt);
419 extern int bv_or_vectors(bit_vector *first, bit_vector *second,
420  bit_vector *result, bv_callback callback,
421  void *context, bv_callback_option cb_opt);
422 extern int bv_xor_vectors(bit_vector *first, bit_vector *second,
423  bit_vector *result, bv_callback callback,
424  void *context, bv_callback_option cb_opt);
425 extern int bv_clear_vectors(bit_vector *first, bit_vector *second,
426  bit_vector *result, bv_callback callback,
427  void *context, bv_callback_option cb_opt);
428 extern int bv_copy_vector(bit_vector *src, bit_vector *dest,
429  bv_callback callback, void *context,
430  bv_callback_option cb_opt);
431 extern int bv_walk_vector(bit_vector *vect, bv_callback callback,
432  void *context);
434 extern boolean bv_empty(bit_vector *bv);
437 extern void bv_clear_all_bits(bit_vector *bv, bv_callback callback,
438  void *context, bv_callback_option cb_opt);
439 
440 #endif /* __BITVECTOR_H__ */
bv_callback_option
Definition: bitvector.h:404
@ BV_CALL_SET
Definition: bitvector.h:406
@ BV_CALL_CHANGE
Definition: bitvector.h:405
bv_bitnum_t bv_first_clear_bit(bit_vector *bv)
int bv_or_vectors(bit_vector *first, bit_vector *second, bit_vector *result, bv_callback callback, void *context, bv_callback_option cb_opt)
int bv_walk_vector(bit_vector *vect, bv_callback callback, void *context)
boolean bv_bit_is_set(bit_vector *bv, bv_bitnum_t bit_number)
int bv_and_vectors(bit_vector *first, bit_vector *second, bit_vector *result, bv_callback callback, void *context, bv_callback_option cb_opt)
void bv_init_vector(bit_vector *bv, boolean fast_vects)
boolean bv_empty(bit_vector *bv)
int bv_clear_vectors(bit_vector *first, bit_vector *second, bit_vector *result, bv_callback callback, void *context, bv_callback_option cb_opt)
void bv_clear_all_bits(bit_vector *bv, bv_callback callback, void *context, bv_callback_option cb_opt)
struct bit_vector_ bit_vector
int bv_copy_vector(bit_vector *src, bit_vector *dest, bv_callback callback, void *context, bv_callback_option cb_opt)
bv_bitnum_t bv_find_clear_bit(bit_vector *bv)
boolean(* bv_callback)(void *context, bv_bitnum_t bit_number, boolean new_bit_value, boolean old_bit_value)
Definition: bitvector.h:396
int bv_set_bit(bit_vector *bv, bv_bitnum_t bit_number)
uint32_t bv_bitnum_t
Definition: bitvector.h:374
int bv_xor_vectors(bit_vector *first, bit_vector *second, bit_vector *result, bv_callback callback, void *context, bv_callback_option cb_opt)
void bv_clean(bit_vector *bv)
bv_bitnum_t bv_first_set_bit(bit_vector *bv)
boolean bv_clear_bit(bit_vector *bv, bv_bitnum_t bit_number)
#define bvx_patroot
unsigned int boolean
Definition: mcast_common.h:12
uint32_t bv_entry_count
Definition: bitvector.h:379
boolean bv_cb_result
Definition: bitvector.h:384
bv_bitnum_t bv_callback_ord
Definition: bitvector.h:381
task_thread bv_nonfull_head
Definition: bitvector.h:380
bv_bitnum_t bv_freed_ord
Definition: bitvector.h:382
boolean bv_cb_source
Definition: bitvector.h:385
bvx_patroot * bv_root
Definition: bitvector.h:378
boolean bv_fastvects
Definition: bitvector.h:383