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
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
21 * Bitmap and membership mask handling routines.
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.
39 clear_bit(uint32_t *mask
, uint32_t bitidx
, uint32_t masklen
)
44 /* Index into array */
46 bit
= 1 << (bitidx
& 0x1F);
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.
67 set_bit(uint32_t *mask
, uint32_t bitidx
, uint32_t masklen
)
72 /* Index into array */
74 bit
= 1 << (bitidx
& 0x1F);
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.
95 is_bit_set(uint32_t *mask
, uint32_t bitidx
, uint32_t masklen
)
100 /* Index into array */
102 bit
= 1 << (bitidx
& 0x1F);
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.
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.
134 memb_low_id(memb_mask_t mask
)
138 for (x
= 0; x
< MAX_NODES
; x
++)
139 if (is_bit_set((uint32_t *)mask
, x
, MEMB_MASK_LEN
) == 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.
153 memb_high_id(memb_mask_t mask
)
157 for (x
= MAX_NODES
-1; x
>= 0; x
--)
158 if (is_bit_set((uint32_t *)mask
, x
, MEMB_MASK_LEN
) == 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.
172 memb_count(memb_mask_t mask
)
176 for (x
= 0; x
< MAX_NODES
; x
++)
177 if (is_bit_set((uint32_t *)mask
, x
, MEMB_MASK_LEN
) == 1)
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
192 memb_mask_str(memb_mask_t mask
)
194 static char bufferstr
[MEMB_MASK_LEN
* 8 + 3];
198 if (!(uint32_t *)mask
)
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
);
213 main(int argc
, char **argv
)
216 memb_mask_t membership
;
218 memset(membership
, 0, sizeof(membership
));
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
));