File Index
Files and directories
Sign in
FILE

bitmap.h

File preview with syntax highlighting.

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