File: bitmap.h

package info (click to toggle)
rdma-core 61.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,124 kB
  • sloc: ansic: 176,798; python: 15,496; sh: 2,742; perl: 1,465; makefile: 73
file content (120 lines) | stat: -rw-r--r-- 2,609 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* GPLv2 or OpenIB.org BSD (MIT) See COPYING file */

#ifndef UTIL_BITMAP_H
#define UTIL_BITMAP_H

#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
#include <string.h>
#include <assert.h>

#include "util.h"

#define BMP_DECLARE(name, nbits) \
	unsigned long (name)[BITS_TO_LONGS((nbits))]

unsigned long bitmap_find_first_bit(const unsigned long *bmp,
				    unsigned long start, unsigned long end);

void bitmap_zero_region(unsigned long *bmp, unsigned long start,
			unsigned long end);

void bitmap_fill_region(unsigned long *bmp, unsigned long start,
			unsigned long end);

unsigned long bitmap_find_free_region(unsigned long *bmp,
				      unsigned long nbits,
				      unsigned long region_size);

static inline void bitmap_fill(unsigned long *bmp, unsigned long nbits)
{
	unsigned long size = BITS_TO_LONGS(nbits) * sizeof(unsigned long);

	memset(bmp, 0xff, size);
}

static inline void bitmap_zero(unsigned long *bmp, unsigned long nbits)
{
	unsigned long size = BITS_TO_LONGS(nbits) * sizeof(unsigned long);

	memset(bmp, 0, size);
}

static inline bool bitmap_empty(const unsigned long *bmp, unsigned long nbits)
{
	unsigned long i;
	unsigned long mask = ULONG_MAX;

	assert(nbits);

	for (i = 0; i < BITS_TO_LONGS(nbits) - 1; i++) {
		if (bmp[i] != 0)
			return false;
	}

	if (nbits % BITS_PER_LONG)
		mask = (1UL << (nbits % BITS_PER_LONG)) - 1;

	return (bmp[i] & mask) ? false : true;
}

static inline bool bitmap_full(const unsigned long *bmp, unsigned long nbits)
{
	unsigned long i;
	unsigned long mask = ULONG_MAX;

	assert(nbits);

	for (i = 0; i < BITS_TO_LONGS(nbits) - 1; i++) {
		if (bmp[i] != -1UL)
			return false;
	}

	if (nbits % BITS_PER_LONG)
		mask = (1UL << (nbits % BITS_PER_LONG)) - 1;

	return ((bmp[i] & mask) ^ (mask)) ? false : true;
}

static inline void bitmap_set_bit(unsigned long *bmp, unsigned long idx)
{
	bmp[(idx / BITS_PER_LONG)] |= (1UL << (idx % BITS_PER_LONG));
}

static inline void bitmap_clear_bit(unsigned long *bmp, unsigned long idx)
{
	bmp[(idx / BITS_PER_LONG)] &= ~(1UL << (idx % BITS_PER_LONG));
}

static inline bool bitmap_test_bit(const unsigned long *bmp, unsigned long idx)
{
	return !!(bmp[(idx / BITS_PER_LONG)] &
		 (1UL << (idx % BITS_PER_LONG)));
}

static inline unsigned long *bitmap_alloc0(unsigned long size)
{
	unsigned long *bmp;

	bmp = calloc(BITS_TO_LONGS(size), sizeof(long));
	if (!bmp)
		return NULL;

	return bmp;
}

static inline unsigned long *bitmap_alloc1(unsigned long size)
{
	unsigned long *bmp;

	bmp = bitmap_alloc0(size);
	if (!bmp)
		return NULL;

	bitmap_fill(bmp, size);

	return bmp;
}

#endif