drivers/android/dbitmap.h

Source file repositories/reference/linux-study-clean/drivers/android/dbitmap.h

File Facts

System
Linux kernel
Corpus path
drivers/android/dbitmap.h
Extension
.h
Size
4207 bytes
Lines
170
Domain
Driver Families
Bucket
drivers/android
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

struct dbitmap {
	unsigned int nbits;
	unsigned long *map;
};

static inline int dbitmap_enabled(struct dbitmap *dmap)
{
	return !!dmap->nbits;
}

static inline void dbitmap_free(struct dbitmap *dmap)
{
	dmap->nbits = 0;
	kfree(dmap->map);
	dmap->map = NULL;
}

/* Returns the nbits that a dbitmap can shrink to, 0 if not possible. */
static inline unsigned int dbitmap_shrink_nbits(struct dbitmap *dmap)
{
	unsigned int bit;

	if (dmap->nbits <= NBITS_MIN)
		return 0;

	/*
	 * Determine if the bitmap can shrink based on the position of
	 * its last set bit. If the bit is within the first quarter of
	 * the bitmap then shrinking is possible. In this case, the
	 * bitmap should shrink to half its current size.
	 */
	bit = find_last_bit(dmap->map, dmap->nbits);
	if (bit < (dmap->nbits >> 2))
		return dmap->nbits >> 1;

	/* find_last_bit() returns dmap->nbits when no bits are set. */
	if (bit == dmap->nbits)
		return NBITS_MIN;

	return 0;
}

/* Replace the internal bitmap with a new one of different size */
static inline void
dbitmap_replace(struct dbitmap *dmap, unsigned long *new, unsigned int nbits)
{
	bitmap_copy(new, dmap->map, min(dmap->nbits, nbits));
	kfree(dmap->map);
	dmap->map = new;
	dmap->nbits = nbits;
}

static inline void
dbitmap_shrink(struct dbitmap *dmap, unsigned long *new, unsigned int nbits)
{
	if (!new)
		return;

	/*
	 * Verify that shrinking to @nbits is still possible. The @new
	 * bitmap might have been allocated without locks, so this call
	 * could now be outdated. In this case, free @new and move on.
	 */
	if (!dbitmap_enabled(dmap) || dbitmap_shrink_nbits(dmap) != nbits) {
		kfree(new);
		return;
	}

	dbitmap_replace(dmap, new, nbits);
}

/* Returns the nbits that a dbitmap can grow to. */
static inline unsigned int dbitmap_grow_nbits(struct dbitmap *dmap)
{
	return dmap->nbits << 1;
}

static inline void
dbitmap_grow(struct dbitmap *dmap, unsigned long *new, unsigned int nbits)
{
	/*
	 * Verify that growing to @nbits is still possible. The @new
	 * bitmap might have been allocated without locks, so this call
	 * could now be outdated. In this case, free @new and move on.
	 */
	if (!dbitmap_enabled(dmap) || nbits <= dmap->nbits) {
		kfree(new);
		return;
	}

Annotation

Implementation Notes