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