OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bitvector_private.h
Go to the documentation of this file.
1 /* $Id: bitvector_private.h 346474 2009-11-14 10:18:58Z ssiano $
2  *
3  * bitvector_private.h - Bit vector manipulation private definitions
4  *
5  * Dave Katz, March 2008
6  *
7  * Copyright (c) 2008, Juniper Networks, Inc.
8  * All rights reserved.
9  */
10 
11 #ifndef __BITVECTOR_PRIVATE_H__
12 #define __BITVECTOR_PRIVATE_H__
13 
14 /*
15  * This module defines a bit vector and a set of operations thereon.
16  *
17  * A bit vector is constructed out of a patricia tree of bit vector entries,
18  * each of which contains a block of bits and some housekeeping information.
19  *
20  * The patricia tree allows for rapid access to a bit vector entries, and
21  * also allows for sparse vectors (missing entries are equivalent to a set
22  * of all zero bits.)
23  *
24  * The size of a bit vector entry is defined in the environment file at
25  * compile time. Larger entries are more memory-efficient for larger
26  * vectors (less overhead), but less so for smaller vectors (more wasted
27  * space.) The computational cost is slightly higher for smaller entries
28  * when vectors are large.
29  */
30 
31 
32 /*
33  * Size definitions
34  *
35  * Sizing is driven by the definition of BV_BITSIZE_LOG2, the base 2
36  * log of the number of bits in a vector entry. The size of each word
37  * in the vector is defined by bv_word_t. All are defined in the
38  * environment file.
39  */
40 #define BV_BITSPERWORD (sizeof(bv_word_t) * 8) /* Bits per word */
41 #define BV_BITSIZE (1 << (BV_BITSIZE_LOG2)) /* Number of bits per entry */
42 #define BV_WORDSIZE ((BV_BITSIZE + BV_BITSPERWORD - 1) / BV_BITSPERWORD)
43 
44 
45 /*
46  * Bit vector entry
47  *
48  * A bit vector entry contains a block of bits with a position offset.
49  * The bits are numbered from the low order position upward.
50  *
51  * Each block has a starting bit number (its offset into the overall
52  * bit vector) and is stored in a patricia tree with the starting bit
53  * number as the key.
54  *
55  * Entries that aren't completely full are threaded from the bit
56  * vector head to make finding a free bit easier.
57  *
58  * In general we try to keep a count of how many bits are set in this
59  * entry, since it speeds up some operations. However, in order to do
60  * fast vector operations we can't afford to keep track of the bit count,
61  * so once we lose track of the bit count we set it to BV_UNKNOWN_COUNT
62  * to flag this case.
63  */
64 typedef struct bv_entry_ {
65  task_thread bv_ent_nonfull_thread; /* Entry on non-full-entry thread */
66  bvx_patnode bv_ent_node; /* Patricia node */
67  uint8_t bv_key[sizeof(bv_bitnum_t)]; /* Patricia key */
68  bv_bitnum_t bv_start; /* Starting bit number */
69  int bv_setcount; /* Number of set bits */
70  bv_word_t bv_bits[BV_WORDSIZE]; /* Actual bits */
71 } bv_entry;
72 
73 BVX_PATNODE_TO_STRUCT(bv_patnode_to_bv_entry, bv_entry, bv_ent_node);
74 THREAD_TO_STRUCT(bv_thread_to_bv_entry, bv_entry, bv_ent_nonfull_thread);
75 
76 #define BV_UNKNOWN_COUNT -1 /* bv_setcount unknown */
77 #define BV_ALLSET ((bv_word_t)(~0)) /* All-ones constant */
78 #define BV_MAX_BITNUM ((0x7fffffff - BV_BITSIZE) + 1) /* Max bit num + 1 */
79 
80 
81 /*
82  * bv_start_bit
83  *
84  * Returns the starting bit number of the bit vector entry corresponding
85  * to a particular bit number.
86  */
87 static inline bv_bitnum_t
89 {
90  return bit_number & (~(BV_BITSIZE - 1));
91 }
92 
93 
94 /*
95  * bv_word_offset
96  *
97  * Returns the word offset into a vector entry of a bit vector word,
98  * given the bit number.
99  *
100  * Don't worry, all that integer math should be optimized to a shift and
101  * a mask.
102  */
103 static inline uint32_t
105 {
106  return ((bit_number % (BV_BITSIZE)) / BV_BITSPERWORD);
107 }
108 
109 /*
110  * bv_word_mask
111  *
112  * Returns the word mask corresponding to a bit, given the bit number.
113  */
114 static inline bv_word_t
116 {
117  return 1 << (bit_number & (BV_BITSPERWORD - 1));
118 }
119 
120 #endif /* __BITVECTOR_PRIVATE_H__ */
bvx_patnode bv_ent_node
task_thread bv_ent_nonfull_thread
#define THREAD_TO_STRUCT(function, structure, member)
uint8_t bv_key[sizeof(bv_bitnum_t)]
bv_word_t bv_bits[BV_WORDSIZE]
int bv_setcount
#define BV_BITSPERWORD
struct bv_entry_ bv_entry
static bv_word_t bv_word_mask(bv_bitnum_t bit_number)
static bv_bitnum_t bv_start_bit(bv_bitnum_t bit_number)
uint32_t bv_word_t
bv_bitnum_t bv_start
#define BV_WORDSIZE
static uint32_t bv_word_offset(bv_bitnum_t bit_number)
#define bvx_patnode
#define BV_BITSIZE
BVX_PATNODE_TO_STRUCT(bv_patnode_to_bv_entry, bv_entry, bv_ent_node)
uint32_t bv_bitnum_t
Definition: bitvector.h:373