Fix compiler error due to uninitialized variable
[clumanager.git] / librhcm / bitmap.c
blob077d5e7d282c4b43f4d0530756b887bf665efa0e
1 /*
2 Copyright Red Hat, Inc. 2002-2003
4 The Red Hat Cluster Manager API Library is free software; you can
5 redistribute it and/or modify it under the terms of the GNU Lesser
6 General Public License as published by the Free Software Foundation;
7 either version 2.1 of the License, or (at your option) any later
8 version.
10 The Red Hat Cluster Manager API Library is distributed in the hope
11 that it will be useful, but WITHOUT ANY WARRANTY; without even the
12 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE. See the GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 USA.
20 /** @file
21 * Bitmap and membership mask handling routines.
23 #include <stdio.h>
24 #include <cm_api.h>
25 #include <string.h>
26 #include <stdint.h>
29 /**
30 * Clear a bit in a bitmap / bitmask.
32 * @param mask Bitmask to modify.
33 * @param bitidx Bit to modify.
34 * @param masklen Bitmask length (in uint32_t units).
35 * @return -1 if the index exceeds the number of bits in the
36 * bitmap, otherwise 0.
38 int
39 clear_bit(uint32_t *mask, uint32_t bitidx, uint32_t masklen)
41 uint32_t idx;
42 uint32_t bit;
44 /* Index into array */
45 idx = bitidx >> 5;
46 bit = 1 << (bitidx & 0x1F);
48 if (idx >= masklen)
49 return -1;
51 mask[idx] &= ~bit;
53 return 0;
57 /**
58 * Set a bit in a bitmap / bitmask.
60 * @param mask Bitmask to modify.
61 * @param bitidx Bit to modify.
62 * @param masklen Bitmask length (in uint32_t units).
63 * @return -1 if the index exceeds the number of bits in the
64 * bitmap, otherwise 0.
66 int
67 set_bit(uint32_t *mask, uint32_t bitidx, uint32_t masklen)
69 uint32_t idx;
70 uint32_t bit;
72 /* Index into array */
73 idx = bitidx >> 5;
74 bit = 1 << (bitidx & 0x1F);
76 if (idx >= masklen)
77 return -1;
79 mask[idx] |= bit;
81 return 0;
85 /**
86 * Check the status of a bit in a bitmap / bitmask.
88 * @param mask Bitmask to check.
89 * @param bitidx Bit to to check.
90 * @param masklen Bitmask length (in uint32_t units).
91 * @return -1 if the index exceeds the number of bits in the
92 * bitmap, 0 if not set, or 1 if set.
94 int
95 is_bit_set(uint32_t *mask, uint32_t bitidx, uint32_t masklen)
97 uint32_t idx;
98 uint32_t bit;
100 /* Index into array */
101 idx = bitidx >> 5;
102 bit = 1 << (bitidx & 0x1F);
104 if (idx >= masklen)
105 return -1;
107 return !!(mask[idx]&bit);
112 * Check to see if a member is online within a given mask.
114 * @param mask Membership mask.
115 * @param node Member number/ID to check.
116 * @return See is_bit_set.
117 * @see is_bit_set
120 memb_online(memb_mask_t mask, int node)
122 return (is_bit_set((uint32_t *)mask, node, MEMB_MASK_LEN));
127 * Return the lowest-numbered member in a given membership mask
129 * @param mask Membership mask.
130 * @return Index of lowest bit set in mask.
131 * @see is_bit_set
134 memb_low_id(memb_mask_t mask)
136 int x;
138 for (x = 0; x < MAX_NODES; x++)
139 if (is_bit_set((uint32_t *)mask, x, MEMB_MASK_LEN) == 1)
140 return x;
141 return -1;
146 * Return the highest-numbered member in a given membership mask
148 * @param mask Membership mask.
149 * @return Index of highest bit set in mask.
150 * @see is_bit_set
153 memb_high_id(memb_mask_t mask)
155 int x;
157 for (x = MAX_NODES-1; x >= 0; x--)
158 if (is_bit_set((uint32_t *)mask, x, MEMB_MASK_LEN) == 1)
159 return x;
160 return -1;
165 * Return the the number of bits ON in a given membership mask.
167 * @param mask Membership mask.
168 * @return Number of bits ON in mask.
169 * @see is_bit_set
172 memb_count(memb_mask_t mask)
174 int x, rv = 0;
176 for (x = 0; x < MAX_NODES; x++)
177 if (is_bit_set((uint32_t *)mask, x, MEMB_MASK_LEN) == 1)
178 rv++;
180 return rv;
185 * Constructs a formatted string out of a membership mask and returns it.
187 * @param mask Membership mask.
188 * @return Pointer to static format string, or NULL if
189 * mask is NULL.
191 char *
192 memb_mask_str(memb_mask_t mask)
194 static char bufferstr[MEMB_MASK_LEN * 8 + 3];
195 char tmpstr[9];
196 int x;
198 if (!(uint32_t *)mask)
199 return NULL;
201 snprintf(bufferstr, sizeof(bufferstr), "0x");
202 for (x=MEMB_MASK_LEN-1; x>=0; x--) {
203 snprintf(tmpstr,sizeof(tmpstr),"%08x",((uint32_t *)mask)[x]);
204 strcat(bufferstr,tmpstr);
207 return bufferstr;
211 #ifdef STANDALONE
213 main(int argc, char **argv)
215 int a;
216 memb_mask_t membership;
218 memset(membership, 0, sizeof(membership));
220 a = atoi(argv[1]);
221 printf("bit[%d] = %d\n", a, is_bit_set(membership, a, MEMB_MASK_LEN));
222 set_bit(membership, a, MEMB_MASK_LEN);
223 printf("bit[%d] = %d\n", a, is_bit_set(membership, a, MEMB_MASK_LEN));
224 clear_bit(membership, a, MEMB_MASK_LEN);
225 printf("bit[%d] = %d\n", a, is_bit_set(membership, a, 64));
226 set_bit(membership, a, MEMB_MASK_LEN);
228 printf("Mask = %s\n", memb_mask_str(membership));
230 return 0;
234 #endif