2011-02-04 09:06:04 +01:00
|
|
|
/*
|
|
|
|
* Bitmap Module
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
|
|
|
|
*
|
|
|
|
* Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BITMAP_H
|
|
|
|
#define BITMAP_H
|
|
|
|
|
2014-09-26 21:46:04 +02:00
|
|
|
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/bitops.h"
|
2011-02-04 09:06:04 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The available bitmap operations and their rough meaning in the
|
|
|
|
* case that the bitmap is a single unsigned long are thus:
|
|
|
|
*
|
|
|
|
* Note that nbits should be always a compile time evaluable constant.
|
|
|
|
* Otherwise many inlines will generate horrible code.
|
|
|
|
*
|
|
|
|
* bitmap_zero(dst, nbits) *dst = 0UL
|
|
|
|
* bitmap_fill(dst, nbits) *dst = ~0UL
|
|
|
|
* bitmap_copy(dst, src, nbits) *dst = *src
|
|
|
|
* bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2
|
|
|
|
* bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2
|
|
|
|
* bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2
|
|
|
|
* bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2)
|
|
|
|
* bitmap_complement(dst, src, nbits) *dst = ~(*src)
|
|
|
|
* bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal?
|
2013-12-17 12:12:24 +01:00
|
|
|
* bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap?
|
2011-02-04 09:06:04 +01:00
|
|
|
* bitmap_empty(src, nbits) Are all bits zero in *src?
|
|
|
|
* bitmap_full(src, nbits) Are all bits set in *src?
|
|
|
|
* bitmap_set(dst, pos, nbits) Set specified bit area
|
2014-12-02 12:23:14 +01:00
|
|
|
* bitmap_set_atomic(dst, pos, nbits) Set specified bit area with atomic ops
|
2011-02-04 09:06:04 +01:00
|
|
|
* bitmap_clear(dst, pos, nbits) Clear specified bit area
|
2014-12-02 12:23:15 +01:00
|
|
|
* bitmap_test_and_clear_atomic(dst, pos, nbits) Test and clear area
|
2011-02-04 09:06:04 +01:00
|
|
|
* bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area
|
2017-08-30 10:32:00 +02:00
|
|
|
* bitmap_to_le(dst, src, nbits) Convert bitmap to little endian
|
|
|
|
* bitmap_from_le(dst, src, nbits) Convert bitmap from little endian
|
2011-02-04 09:06:04 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Also the following operations apply to bitmaps.
|
|
|
|
*
|
|
|
|
* set_bit(bit, addr) *addr |= bit
|
|
|
|
* clear_bit(bit, addr) *addr &= ~bit
|
|
|
|
* change_bit(bit, addr) *addr ^= bit
|
|
|
|
* test_bit(bit, addr) Is bit set in *addr?
|
|
|
|
* test_and_set_bit(bit, addr) Set bit and return old value
|
|
|
|
* test_and_clear_bit(bit, addr) Clear bit and return old value
|
|
|
|
* test_and_change_bit(bit, addr) Change bit and return old value
|
|
|
|
* find_first_zero_bit(addr, nbits) Position first zero bit in *addr
|
|
|
|
* find_first_bit(addr, nbits) Position first set bit in *addr
|
|
|
|
* find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit
|
|
|
|
* find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
|
|
|
|
*/
|
|
|
|
|
2016-03-05 14:47:08 +01:00
|
|
|
#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
|
|
|
|
#define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
|
2011-02-04 09:06:04 +01:00
|
|
|
|
|
|
|
#define DECLARE_BITMAP(name,bits) \
|
2013-12-17 12:12:24 +01:00
|
|
|
unsigned long name[BITS_TO_LONGS(bits)]
|
2011-02-04 09:06:04 +01:00
|
|
|
|
|
|
|
#define small_nbits(nbits) \
|
2013-12-17 12:12:24 +01:00
|
|
|
((nbits) <= BITS_PER_LONG)
|
2011-02-04 09:06:04 +01:00
|
|
|
|
2013-12-17 12:12:24 +01:00
|
|
|
int slow_bitmap_empty(const unsigned long *bitmap, long bits);
|
|
|
|
int slow_bitmap_full(const unsigned long *bitmap, long bits);
|
2011-02-04 09:06:04 +01:00
|
|
|
int slow_bitmap_equal(const unsigned long *bitmap1,
|
2013-12-17 12:12:24 +01:00
|
|
|
const unsigned long *bitmap2, long bits);
|
2011-02-04 09:06:04 +01:00
|
|
|
void slow_bitmap_complement(unsigned long *dst, const unsigned long *src,
|
2013-12-17 12:12:24 +01:00
|
|
|
long bits);
|
2011-02-04 09:06:04 +01:00
|
|
|
int slow_bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
|
2013-12-17 12:12:24 +01:00
|
|
|
const unsigned long *bitmap2, long bits);
|
2011-02-04 09:06:04 +01:00
|
|
|
void slow_bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
|
2013-12-17 12:12:24 +01:00
|
|
|
const unsigned long *bitmap2, long bits);
|
2011-02-04 09:06:04 +01:00
|
|
|
void slow_bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
|
2013-12-17 12:12:24 +01:00
|
|
|
const unsigned long *bitmap2, long bits);
|
2011-02-04 09:06:04 +01:00
|
|
|
int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
|
2013-12-17 12:12:24 +01:00
|
|
|
const unsigned long *bitmap2, long bits);
|
2011-02-04 09:06:04 +01:00
|
|
|
int slow_bitmap_intersects(const unsigned long *bitmap1,
|
2013-12-17 12:12:24 +01:00
|
|
|
const unsigned long *bitmap2, long bits);
|
2017-08-30 10:31:59 +02:00
|
|
|
long slow_bitmap_count_one(const unsigned long *bitmap, long nbits);
|
2011-02-04 09:06:04 +01:00
|
|
|
|
2014-09-30 09:09:11 +02:00
|
|
|
static inline unsigned long *bitmap_try_new(long nbits)
|
2011-02-04 09:06:04 +01:00
|
|
|
{
|
2013-12-17 12:12:24 +01:00
|
|
|
long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
2014-09-30 09:09:11 +02:00
|
|
|
return g_try_malloc0(len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned long *bitmap_new(long nbits)
|
|
|
|
{
|
|
|
|
unsigned long *ptr = bitmap_try_new(nbits);
|
|
|
|
if (ptr == NULL) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return ptr;
|
2011-02-04 09:06:04 +01:00
|
|
|
}
|
|
|
|
|
2013-12-17 12:12:24 +01:00
|
|
|
static inline void bitmap_zero(unsigned long *dst, long nbits)
|
2011-02-04 09:06:04 +01:00
|
|
|
{
|
|
|
|
if (small_nbits(nbits)) {
|
|
|
|
*dst = 0UL;
|
|
|
|
} else {
|
2013-12-17 12:12:24 +01:00
|
|
|
long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
2011-02-04 09:06:04 +01:00
|
|
|
memset(dst, 0, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-17 12:12:24 +01:00
|
|
|
static inline void bitmap_fill(unsigned long *dst, long nbits)
|
2011-02-04 09:06:04 +01:00
|
|
|
{
|
|
|
|
size_t nlongs = BITS_TO_LONGS(nbits);
|
|
|
|
if (!small_nbits(nbits)) {
|
2013-12-17 12:12:24 +01:00
|
|
|
long len = (nlongs - 1) * sizeof(unsigned long);
|
2011-02-04 09:06:04 +01:00
|
|
|
memset(dst, 0xff, len);
|
|
|
|
}
|
|
|
|
dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
|
2013-12-17 12:12:24 +01:00
|
|
|
long nbits)
|
2011-02-04 09:06:04 +01:00
|
|
|
{
|
|
|
|
if (small_nbits(nbits)) {
|
|
|
|
*dst = *src;
|
|
|
|
} else {
|
2013-12-17 12:12:24 +01:00
|
|
|
long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
2011-02-04 09:06:04 +01:00
|
|
|
memcpy(dst, src, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
|
2013-12-17 12:12:24 +01:00
|
|
|
const unsigned long *src2, long nbits)
|
2011-02-04 09:06:04 +01:00
|
|
|
{
|
|
|
|
if (small_nbits(nbits)) {
|
|
|
|
return (*dst = *src1 & *src2) != 0;
|
|
|
|
}
|
|
|
|
return slow_bitmap_and(dst, src1, src2, nbits);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
|
2013-12-17 12:12:24 +01:00
|
|
|
const unsigned long *src2, long nbits)
|
2011-02-04 09:06:04 +01:00
|
|
|
{
|
|
|
|
if (small_nbits(nbits)) {
|
|
|
|
*dst = *src1 | *src2;
|
|
|
|
} else {
|
|
|
|
slow_bitmap_or(dst, src1, src2, nbits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
|
2013-12-17 12:12:24 +01:00
|
|
|
const unsigned long *src2, long nbits)
|
2011-02-04 09:06:04 +01:00
|
|
|
{
|
|
|
|
if (small_nbits(nbits)) {
|
|
|
|
*dst = *src1 ^ *src2;
|
|
|
|
} else {
|
|
|
|
slow_bitmap_xor(dst, src1, src2, nbits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
|
2013-12-17 12:12:24 +01:00
|
|
|
const unsigned long *src2, long nbits)
|
2011-02-04 09:06:04 +01:00
|
|
|
{
|
|
|
|
if (small_nbits(nbits)) {
|
|
|
|
return (*dst = *src1 & ~(*src2)) != 0;
|
|
|
|
}
|
|
|
|
return slow_bitmap_andnot(dst, src1, src2, nbits);
|
|
|
|
}
|
|
|
|
|
2013-12-17 12:12:24 +01:00
|
|
|
static inline void bitmap_complement(unsigned long *dst,
|
|
|
|
const unsigned long *src,
|
|
|
|
long nbits)
|
2011-02-04 09:06:04 +01:00
|
|
|
{
|
|
|
|
if (small_nbits(nbits)) {
|
|
|
|
*dst = ~(*src) & BITMAP_LAST_WORD_MASK(nbits);
|
|
|
|
} else {
|
|
|
|
slow_bitmap_complement(dst, src, nbits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int bitmap_equal(const unsigned long *src1,
|
2013-12-17 12:12:24 +01:00
|
|
|
const unsigned long *src2, long nbits)
|
2011-02-04 09:06:04 +01:00
|
|
|
{
|
|
|
|
if (small_nbits(nbits)) {
|
|
|
|
return ! ((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
|
|
|
|
} else {
|
|
|
|
return slow_bitmap_equal(src1, src2, nbits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-17 12:12:24 +01:00
|
|
|
static inline int bitmap_empty(const unsigned long *src, long nbits)
|
2011-02-04 09:06:04 +01:00
|
|
|
{
|
|
|
|
if (small_nbits(nbits)) {
|
|
|
|
return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
|
|
|
|
} else {
|
|
|
|
return slow_bitmap_empty(src, nbits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-17 12:12:24 +01:00
|
|
|
static inline int bitmap_full(const unsigned long *src, long nbits)
|
2011-02-04 09:06:04 +01:00
|
|
|
{
|
|
|
|
if (small_nbits(nbits)) {
|
|
|
|
return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
|
|
|
|
} else {
|
|
|
|
return slow_bitmap_full(src, nbits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int bitmap_intersects(const unsigned long *src1,
|
2013-12-17 12:12:24 +01:00
|
|
|
const unsigned long *src2, long nbits)
|
2011-02-04 09:06:04 +01:00
|
|
|
{
|
|
|
|
if (small_nbits(nbits)) {
|
|
|
|
return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
|
|
|
|
} else {
|
|
|
|
return slow_bitmap_intersects(src1, src2, nbits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-30 10:31:59 +02:00
|
|
|
static inline long bitmap_count_one(const unsigned long *bitmap, long nbits)
|
|
|
|
{
|
|
|
|
if (small_nbits(nbits)) {
|
|
|
|
return ctpopl(*bitmap & BITMAP_LAST_WORD_MASK(nbits));
|
|
|
|
} else {
|
|
|
|
return slow_bitmap_count_one(bitmap, nbits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-17 12:12:24 +01:00
|
|
|
void bitmap_set(unsigned long *map, long i, long len);
|
2014-12-02 12:23:14 +01:00
|
|
|
void bitmap_set_atomic(unsigned long *map, long i, long len);
|
2013-12-17 12:12:24 +01:00
|
|
|
void bitmap_clear(unsigned long *map, long start, long nr);
|
2014-12-02 12:23:15 +01:00
|
|
|
bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr);
|
2017-04-21 11:16:24 +02:00
|
|
|
void bitmap_copy_and_clear_atomic(unsigned long *dst, unsigned long *src,
|
|
|
|
long nr);
|
2011-02-04 09:06:04 +01:00
|
|
|
unsigned long bitmap_find_next_zero_area(unsigned long *map,
|
2013-12-17 12:12:24 +01:00
|
|
|
unsigned long size,
|
|
|
|
unsigned long start,
|
|
|
|
unsigned long nr,
|
|
|
|
unsigned long align_mask);
|
2011-02-04 09:06:04 +01:00
|
|
|
|
2013-10-08 16:12:17 +02:00
|
|
|
static inline unsigned long *bitmap_zero_extend(unsigned long *old,
|
|
|
|
long old_nbits, long new_nbits)
|
|
|
|
{
|
|
|
|
long new_len = BITS_TO_LONGS(new_nbits) * sizeof(unsigned long);
|
|
|
|
unsigned long *new = g_realloc(old, new_len);
|
|
|
|
bitmap_clear(new, old_nbits, new_nbits - old_nbits);
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2017-08-30 10:32:00 +02:00
|
|
|
void bitmap_to_le(unsigned long *dst, const unsigned long *src,
|
|
|
|
long nbits);
|
|
|
|
void bitmap_from_le(unsigned long *dst, const unsigned long *src,
|
|
|
|
long nbits);
|
|
|
|
|
2011-02-04 09:06:04 +01:00
|
|
|
#endif /* BITMAP_H */
|