Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _TOOLS_LINUX_BITMAP_H
3#define _TOOLS_LINUX_BITMAP_H
4
5#include <string.h>
6#include <linux/align.h>
7#include <linux/bitops.h>
8#include <linux/find.h>
9#include <stdlib.h>
10#include <linux/kernel.h>
11
12#define DECLARE_BITMAP(name,bits) \
13 unsigned long name[BITS_TO_LONGS(bits)]
14
15unsigned int __bitmap_weight(const unsigned long *bitmap, int bits);
16void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
17 const unsigned long *bitmap2, int bits);
18bool __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
19 const unsigned long *bitmap2, unsigned int bits);
20bool __bitmap_equal(const unsigned long *bitmap1,
21 const unsigned long *bitmap2, unsigned int bits);
22void __bitmap_set(unsigned long *map, unsigned int start, int len);
23void __bitmap_clear(unsigned long *map, unsigned int start, int len);
24bool __bitmap_intersects(const unsigned long *bitmap1,
25 const unsigned long *bitmap2, unsigned int bits);
26
27#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
28#define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
29
30#define bitmap_size(nbits) (ALIGN(nbits, BITS_PER_LONG) / BITS_PER_BYTE)
31
32static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
33{
34 if (small_const_nbits(nbits))
35 *dst = 0UL;
36 else {
37 memset(dst, 0, bitmap_size(nbits));
38 }
39}
40
41static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
42{
43 unsigned int nlongs = BITS_TO_LONGS(nbits);
44 if (!small_const_nbits(nbits)) {
45 unsigned int len = (nlongs - 1) * sizeof(unsigned long);
46 memset(dst, 0xff, len);
47 }
48 dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
49}
50
51static inline bool bitmap_empty(const unsigned long *src, unsigned int nbits)
52{
53 if (small_const_nbits(nbits))
54 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
55
56 return find_first_bit(src, nbits) == nbits;
57}
58
59static inline bool bitmap_full(const unsigned long *src, unsigned int nbits)
60{
61 if (small_const_nbits(nbits))
62 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
63
64 return find_first_zero_bit(src, nbits) == nbits;
65}
66
67static inline unsigned int bitmap_weight(const unsigned long *src, unsigned int nbits)
68{
69 if (small_const_nbits(nbits))
70 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
71 return __bitmap_weight(src, nbits);
72}
73
74static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
75 const unsigned long *src2, unsigned int nbits)
76{
77 if (small_const_nbits(nbits))
78 *dst = *src1 | *src2;
79 else
80 __bitmap_or(dst, src1, src2, nbits);
81}
82
83static inline unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags __maybe_unused)
84{
85 return malloc(bitmap_size(nbits));
86}
87
88/**
89 * bitmap_zalloc - Allocate bitmap
90 * @nbits: Number of bits
91 */
92static inline unsigned long *bitmap_zalloc(int nbits)
93{
94 return calloc(1, bitmap_size(nbits));
95}
96
97/*
98 * bitmap_free - Free bitmap
99 * @bitmap: pointer to bitmap
100 */
101static inline void bitmap_free(unsigned long *bitmap)
102{
103 free(bitmap);
104}
105
106/*
107 * bitmap_scnprintf - print bitmap list into buffer
108 * @bitmap: bitmap
109 * @nbits: size of bitmap
110 * @buf: buffer to store output
111 * @size: size of @buf
112 */
113size_t bitmap_scnprintf(unsigned long *bitmap, unsigned int nbits,
114 char *buf, size_t size);
115
116/**
117 * bitmap_and - Do logical and on bitmaps
118 * @dst: resulting bitmap
119 * @src1: operand 1
120 * @src2: operand 2
121 * @nbits: size of bitmap
122 */
123static inline bool bitmap_and(unsigned long *dst, const unsigned long *src1,
124 const unsigned long *src2, unsigned int nbits)
125{
126 if (small_const_nbits(nbits))
127 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
128 return __bitmap_and(dst, src1, src2, nbits);
129}
130
131#ifdef __LITTLE_ENDIAN
132#define BITMAP_MEM_ALIGNMENT 8
133#else
134#define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long))
135#endif
136#define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
137
138static inline bool bitmap_equal(const unsigned long *src1,
139 const unsigned long *src2, unsigned int nbits)
140{
141 if (small_const_nbits(nbits))
142 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
143 if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
144 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
145 return !memcmp(src1, src2, nbits / 8);
146 return __bitmap_equal(src1, src2, nbits);
147}
148
149static inline bool bitmap_intersects(const unsigned long *src1,
150 const unsigned long *src2,
151 unsigned int nbits)
152{
153 if (small_const_nbits(nbits))
154 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
155 else
156 return __bitmap_intersects(src1, src2, nbits);
157}
158
159static inline void bitmap_set(unsigned long *map, unsigned int start, unsigned int nbits)
160{
161 if (__builtin_constant_p(nbits) && nbits == 1)
162 __set_bit(start, map);
163 else if (small_const_nbits(start + nbits))
164 *map |= GENMASK(start + nbits - 1, start);
165 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
166 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
167 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
168 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
169 memset((char *)map + start / 8, 0xff, nbits / 8);
170 else
171 __bitmap_set(map, start, nbits);
172}
173
174static inline void bitmap_clear(unsigned long *map, unsigned int start,
175 unsigned int nbits)
176{
177 if (__builtin_constant_p(nbits) && nbits == 1)
178 __clear_bit(start, map);
179 else if (small_const_nbits(start + nbits))
180 *map &= ~GENMASK(start + nbits - 1, start);
181 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
182 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
183 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
184 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
185 memset((char *)map + start / 8, 0, nbits / 8);
186 else
187 __bitmap_clear(map, start, nbits);
188}
189#endif /* _TOOLS_LINUX_BITMAP_H */