util: Use unsigned indices for bits in bitmaps

A negative bit index in a bitmap doesn't make sense.  Avoid this by
construction by using unsigned indices.  While we're there adjust
bitmap_isset() to return a bool instead of an int.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2024-06-06 20:09:46 +10:00 committed by Stefano Brivio
parent 0e36fe1a43
commit bda80ef53f
2 changed files with 7 additions and 7 deletions

8
util.c
View file

@ -232,7 +232,7 @@ int timespec_diff_ms(const struct timespec *a, const struct timespec *b)
* @map: Pointer to bitmap * @map: Pointer to bitmap
* @bit: Bit number to set * @bit: Bit number to set
*/ */
void bitmap_set(uint8_t *map, int bit) void bitmap_set(uint8_t *map, unsigned bit)
{ {
unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit); unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit);
@ -244,7 +244,7 @@ void bitmap_set(uint8_t *map, int bit)
* @map: Pointer to bitmap * @map: Pointer to bitmap
* @bit: Bit number to clear * @bit: Bit number to clear
*/ */
void bitmap_clear(uint8_t *map, int bit) void bitmap_clear(uint8_t *map, unsigned bit)
{ {
unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit); unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit);
@ -256,9 +256,9 @@ void bitmap_clear(uint8_t *map, int bit)
* @map: Pointer to bitmap * @map: Pointer to bitmap
* @bit: Bit number to check * @bit: Bit number to check
* *
* Return: one if given bit is set, zero if it's not * Return: true if given bit is set, false if it's not
*/ */
int bitmap_isset(const uint8_t *map, int bit) bool bitmap_isset(const uint8_t *map, unsigned bit)
{ {
const unsigned long *word const unsigned long *word
= (const unsigned long *)map + BITMAP_WORD(bit); = (const unsigned long *)map + BITMAP_WORD(bit);

6
util.h
View file

@ -148,9 +148,9 @@ int sock_l4(const struct ctx *c, sa_family_t af, uint8_t proto,
uint32_t data); uint32_t data);
void sock_probe_mem(struct ctx *c); void sock_probe_mem(struct ctx *c);
int timespec_diff_ms(const struct timespec *a, const struct timespec *b); int timespec_diff_ms(const struct timespec *a, const struct timespec *b);
void bitmap_set(uint8_t *map, int bit); void bitmap_set(uint8_t *map, unsigned bit);
void bitmap_clear(uint8_t *map, int bit); void bitmap_clear(uint8_t *map, unsigned bit);
int bitmap_isset(const uint8_t *map, int bit); bool bitmap_isset(const uint8_t *map, unsigned bit);
void bitmap_or(uint8_t *dst, size_t size, const uint8_t *a, const uint8_t *b); void bitmap_or(uint8_t *dst, size_t size, const uint8_t *a, const uint8_t *b);
char *line_read(char *buf, size_t len, int fd); char *line_read(char *buf, size_t len, int fd);
void ns_enter(const struct ctx *c); void ns_enter(const struct ctx *c);