Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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