This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / kernel / ckrm / rbce / bitvector.h
1 /*
2  * Copyright (C) Hubertus Franke, IBM Corp. 2003
3  * 
4  * Bitvector package
5  *
6  * Latest version, more details at http://ckrm.sf.net
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  */
14
15 /* Changes
16  *
17  * 15 Nov 2003
18  *        Created
19  */
20
21 #ifndef BITVECTOR_H
22 #define BITVECTOR_H
23
24 typedef struct {
25         int size;               // maxsize in longs
26         unsigned long bits[0];  // bit vector
27 } bitvector_t;
28
29 #define BITS_2_LONGS(sz)  (((sz)+BITS_PER_LONG-1)/BITS_PER_LONG)
30 #define BITS_2_BYTES(sz)  (((sz)+7)/8)
31
32 #if 0
33 #define CHECK_VEC(vec) (vec)    /* check against NULL */
34 #else
35 #define CHECK_VEC(vec) (1)      /* assume no problem */
36 #endif
37
38 #define CHECK_VEC_VOID(vec)   do { if (!CHECK_VEC(vec)) return; } while(0)
39 #define CHECK_VEC_RC(vec, val) \
40 do { if (!CHECK_VEC(vec)) return (val); } while(0)
41
42 inline static void bitvector_zero(bitvector_t * bitvec)
43 {
44         int sz;
45
46         CHECK_VEC_VOID(bitvec);
47         sz = BITS_2_BYTES(bitvec->size);
48         memset(bitvec->bits, 0, sz);
49         return;
50 }
51
52 inline static unsigned long bitvector_bytes(unsigned long size)
53 {
54         return sizeof(bitvector_t) + BITS_2_BYTES(size);
55 }
56
57 inline static void bitvector_init(bitvector_t * bitvec, unsigned long size)
58 {
59         bitvec->size = size;
60         bitvector_zero(bitvec);
61         return;
62 }
63
64 inline static bitvector_t *bitvector_alloc(unsigned long size)
65 {
66         bitvector_t *vec =
67             (bitvector_t *) kmalloc(bitvector_bytes(size), GFP_KERNEL);
68         if (vec) {
69                 vec->size = size;
70                 bitvector_zero(vec);
71         }
72         return vec;
73 }
74
75 inline static void bitvector_free(bitvector_t * bitvec)
76 {
77         CHECK_VEC_VOID(bitvec);
78         kfree(bitvec);
79         return;
80 }
81
82 #define def_bitvec_op(name,mod1,op,mod2)                        \
83 inline static int name(bitvector_t *res, bitvector_t *op1,      \
84                        bitvector_t *op2)                        \
85 {                                                               \
86         unsigned int i, size;                                   \
87                                                                 \
88         CHECK_VEC_RC(res, 0);                                   \
89         CHECK_VEC_RC(op1, 0);                                   \
90         CHECK_VEC_RC(op2, 0);                                   \
91         size = res->size;                                       \
92         if (((size != (op1)->size) || (size != (op2)->size))) { \
93                 return 0;                                       \
94         }                                                       \
95         size = BITS_2_LONGS(size);                              \
96         for (i = 0; i < size; i++) {                            \
97                 (res)->bits[i] = (mod1 (op1)->bits[i]) op       \
98                                         (mod2 (op2)->bits[i]);  \
99         }                                                       \
100         return 1;                                               \
101 }
102
103 def_bitvec_op(bitvector_or,, |,);
104 def_bitvec_op(bitvector_and,, &,);
105 def_bitvec_op(bitvector_xor,, ^,);
106 def_bitvec_op(bitvector_or_not,, |, ~);
107 def_bitvec_op(bitvector_not_or, ~, |,);
108 def_bitvec_op(bitvector_and_not,, &, ~);
109 def_bitvec_op(bitvector_not_and, ~, &,);
110
111 inline static void bitvector_set(int idx, bitvector_t * vec)
112 {
113         set_bit(idx, vec->bits);
114         return;
115 }
116
117 inline static void bitvector_clear(int idx, bitvector_t * vec)
118 {
119         clear_bit(idx, vec->bits);
120         return;
121 }
122
123 inline static int bitvector_test(int idx, bitvector_t * vec)
124 {
125         return test_bit(idx, vec->bits);
126 }
127
128 #ifdef DEBUG
129 inline static void bitvector_print(int flag, bitvector_t * vec)
130 {
131         unsigned int i;
132         int sz;
133         extern int rbcedebug;
134
135         if ((rbcedebug & flag) == 0) {
136                 return;
137         }
138         if (vec == NULL) {
139                 printk("v<0>-NULL\n");
140                 return;
141         }
142         printk("v<%d>-", sz = vec->size);
143         for (i = 0; i < sz; i++) {
144                 printk("%c", test_bit(i, vec->bits) ? '1' : '0');
145         }
146         return;
147 }
148 #else
149 #define bitvector_print(x, y)
150 #endif
151
152 #endif                          // BITVECTOR_H