libisdn
bitmap.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "bitmap.h"

Go to the source code of this file.

Defines

#define BITMAP_GET(m, b)   (!!((m)[BITMAP_OFFSET(b)] & (1 << BITMAP_BIT(b))))
#define BITMAP_SET(m, b)   ((m)[BITMAP_OFFSET(b)] |= (1 << BITMAP_BIT(b)))
#define BITMAP_UNSET(m, b)   ((m)[BITMAP_OFFSET(b)] &= ~(1 << BITMAP_BIT(b)))
#define bitmap_debug(x,...)

Functions

int bitmap_create (bitmap_t **bm, const int nbits)
int bitmap_destroy (bitmap_t *bm)
int bitmap_init (bitmap_t *bm, const int nbits)
int bitmap_is_set (const bitmap_t *bm, const int nbits, const int pos)
int bitmap_set (bitmap_t *bm, const int nbits, const int pos)
int bitmap_unset (bitmap_t *bm, const int nbits, const int pos)
int bitmap_clear (bitmap_t *bm, const int nbits)
int bitmap_is_empty (const bitmap_t *bm, const int nbits)
int bitmap_next_bit (const bitmap_t *bm, const int nbits, const int last)
int bitmap_first_bit (const bitmap_t *bm, const int nbits)
int bitmap_count (const bitmap_t *bm, const int nbits)
int bitmap_dump (const bitmap_t *bm, const int nbits)

Define Documentation

#define bitmap_debug (   x,
  ... 
)

Definition at line 22 of file bitmap.c.

Referenced by bitmap_next_bit().

#define BITMAP_GET (   m,
 
)    (!!((m)[BITMAP_OFFSET(b)] & (1 << BITMAP_BIT(b))))

Definition at line 16 of file bitmap.c.

Referenced by bitmap_is_set().

#define BITMAP_SET (   m,
 
)    ((m)[BITMAP_OFFSET(b)] |= (1 << BITMAP_BIT(b)))

Definition at line 17 of file bitmap.c.

Referenced by bitmap_set().

#define BITMAP_UNSET (   m,
 
)    ((m)[BITMAP_OFFSET(b)] &= ~(1 << BITMAP_BIT(b)))

Definition at line 18 of file bitmap.c.

Referenced by bitmap_unset().


Function Documentation

int bitmap_clear ( bitmap_t bm,
const int  nbits 
)

Clear all bits

Parameters:
[in]bmBitmap
[in]nbitsSize of bitmap in bits 0 on success, -1 on error

Definition at line 74 of file bitmap.c.

References BITMAP_SIZE_BYTES.

int bitmap_count ( const bitmap_t bm,
const int  nbits 
)

Count set bits in bitmap

Parameters:
[in]bmBitmap
[in]nbitsSize of bitmap (number of bits)
Returns:
Number of '1' bits in bitmap

Definition at line 131 of file bitmap.c.

References bitmap_first_bit(), and bitmap_next_bit().

int bitmap_create ( bitmap_t **  bm,
const int  nbits 
)

Allocate new bitmap with size of <nbits> bits ([0:nbits-1])

Parameters:
bmBitmap
[in]nbitsSize in bits
Returns:
0 on success, -1 on error

Definition at line 24 of file bitmap.c.

References BITMAP_SIZE_BYTES.

int bitmap_destroy ( bitmap_t bm)

Destroy dynamically allocated bitmap

Parameters:
bmBitmap
Returns:
0 on success, -1 on error

Definition at line 35 of file bitmap.c.

int bitmap_dump ( const bitmap_t bm,
const int  nbits 
)

Definition at line 140 of file bitmap.c.

References BITMAP_SIZE.

int bitmap_first_bit ( const bitmap_t bm,
const int  nbits 
)

Return first bit set in bitmap

Parameters:
[in]bmBitmap
[in]nbitsSize of bitmap in bits
Returns:
-1 if none found or error, >= 0 pos of first bit

Definition at line 126 of file bitmap.c.

References bitmap_next_bit().

Referenced by bitmap_count().

int bitmap_init ( bitmap_t bm,
const int  nbits 
)

Initialize a static bitmap with size <nbits> bits ([0:nbits-1])

Parameters:
[in]bmBitmap
[in]nbitsSize of bitmap in bits
Returns:
0 on success, -1 on error

Definition at line 43 of file bitmap.c.

References BITMAP_SIZE_BYTES.

int bitmap_is_empty ( const bitmap_t bm,
const int  nbits 
)

Check if map has no entries

Parameters:
[in]bmBitmap
[in]nbitsSize of bitmap in bits
Returns:
0 if map has entries, 1 otherwise

Definition at line 82 of file bitmap.c.

References BITMAP_SIZE_BYTES.

int bitmap_is_set ( const bitmap_t bm,
const int  nbits,
const int  pos 
)

Check whether a bit in the map is set

Parameters:
[in]bmBitmap
[in]nbitsSize of bitmap in bits
[in]posBit to check
Returns:
1 if the bit is set, 0 if it's not, -1 on error

Definition at line 51 of file bitmap.c.

References BITMAP_GET.

int bitmap_next_bit ( const bitmap_t bm,
const int  nbits,
const int  last 
)

Return next bit set in bitmap

Parameters:
[in]bmBitmap
[in]nbitsSize of bitmap in bits
[in]lastPosition of last set bit (output of bitmap_next_bit())
Returns:
-1 if none found or error, >= 0 pos of first bit

Definition at line 91 of file bitmap.c.

References BITMAP_BIT, bitmap_debug, BITMAP_OFFSET, and BITS_PER_LONG.

Referenced by bitmap_count(), and bitmap_first_bit().

int bitmap_set ( bitmap_t bm,
const int  nbits,
const int  pos 
)

Set a bit in the map

Parameters:
[in]bmBitmap
[in]posBit to set
Returns:
0 on success, -1 on error

Definition at line 58 of file bitmap.c.

References BITMAP_SET.

int bitmap_unset ( bitmap_t bm,
const int  nbits,
const int  pos 
)

Unset a bit

Parameters:
[in]bmBitmap
[in]nbitsSize of bitmap in bits
[in]posBit to clear
Returns:
0 on success, -1 on error

Definition at line 66 of file bitmap.c.

References BITMAP_UNSET.