libisdn
bitmap.h File Reference
#include <limits.h>
#include "common.h"

Go to the source code of this file.

Defines

#define BITS_PER_LONG   64
#define BITMAP_SIZE(x)   (((x) + 63) >> 6)
#define BITMAP_SIZE_BYTES(x)   (BITMAP_SIZE(x) << 3)
#define BITMAP_BIT(x)   ((x) & 0x3f)
#define BITMAP_OFFSET(x)   ((x) >> 6)
#define DECLARE_BITMAP(_name, _nbits)   bitmap_t _name[BITMAP_SIZE(_nbits)]

Typedefs

typedef unsigned long bitmap_t

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_first_bit (const bitmap_t *bm, const int nbits)
int bitmap_next_bit (const bitmap_t *bm, const int nbits, const int last)
int bitmap_count (const bitmap_t *bm, const int nbits)

Define Documentation

#define BITMAP_BIT (   x)    ((x) & 0x3f)

Definition at line 21 of file bitmap.h.

Referenced by bitmap_next_bit().

#define BITMAP_OFFSET (   x)    ((x) >> 6)

Definition at line 22 of file bitmap.h.

Referenced by bitmap_next_bit().

#define BITMAP_SIZE (   x)    (((x) + 63) >> 6)

Definition at line 19 of file bitmap.h.

Referenced by bitmap_dump().

#define BITMAP_SIZE_BYTES (   x)    (BITMAP_SIZE(x) << 3)

Definition at line 20 of file bitmap.h.

Referenced by bitmap_clear(), bitmap_create(), bitmap_init(), and bitmap_is_empty().

#define BITS_PER_LONG   64

Definition at line 18 of file bitmap.h.

Referenced by bitmap_next_bit().

#define DECLARE_BITMAP (   _name,
  _nbits 
)    bitmap_t _name[BITMAP_SIZE(_nbits)]

Definition at line 27 of file bitmap.h.


Typedef Documentation

typedef unsigned long bitmap_t

Definition at line 25 of file bitmap.h.


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_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.