lib/bitmap.c
Source file repositories/reference/linux-study-clean/lib/bitmap.c
File Facts
- System
- Linux kernel
- Corpus path
lib/bitmap.c- Extension
.c- Size
- 27827 bytes
- Lines
- 898
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bitmap.hlinux/bitops.hlinux/ctype.hlinux/device.hlinux/export.hlinux/slab.h
Detected Declarations
function Booleanfunction __bitmap_or_equalfunction __bitmap_complementfunction rightfunction leftfunction bitmap_cutfunction __bitmap_andfunction __bitmap_orfunction __bitmap_xorfunction __bitmap_andnotfunction __bitmap_replacefunction __bitmap_intersectsfunction __bitmap_subsetfunction __bitmap_weightfunction __bitmap_weight_andfunction __bitmap_weight_andnotfunction __bitmap_weighted_orfunction __bitmap_weighted_xorfunction __bitmap_setfunction __bitmap_clearfunction bitmap_find_next_zero_area_offfunction tofunction bitmap_remapfunction bitmap_bitremapfunction bitmap_foldfunction bitmap_foldfunction bitmap_freefunction devm_bitmap_freefunction bitmap_from_arr32function bitmap_to_arr32function bitmap_from_arr64function bitmap_to_arr64export __bitmap_equalexport __bitmap_or_equalexport __bitmap_complementexport __bitmap_shift_rightexport __bitmap_shift_leftexport bitmap_cutexport __bitmap_andexport __bitmap_orexport __bitmap_xorexport __bitmap_andnotexport __bitmap_replaceexport __bitmap_intersectsexport __bitmap_subsetexport __bitmap_weightexport __bitmap_weight_andexport __bitmap_weight_andnot
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0-only
/*
* lib/bitmap.c
* Helper functions for bitmap.h.
*/
#include <linux/bitmap.h>
#include <linux/bitops.h>
#include <linux/ctype.h>
#include <linux/device.h>
#include <linux/export.h>
#include <linux/slab.h>
/**
* DOC: bitmap introduction
*
* bitmaps provide an array of bits, implemented using an
* array of unsigned longs. The number of valid bits in a
* given bitmap does _not_ need to be an exact multiple of
* BITS_PER_LONG.
*
* The possible unused bits in the last, partially used word
* of a bitmap are 'don't care'. The implementation makes
* no particular effort to keep them zero. It ensures that
* their value will not affect the results of any operation.
* The bitmap operations that return Boolean (bitmap_empty,
* for example) or scalar (bitmap_weight, for example) results
* carefully filter out these unused bits from impacting their
* results.
*
* The byte ordering of bitmaps is more natural on little
* endian architectures. See the big-endian headers
* include/asm-ppc64/bitops.h and include/asm-s390/bitops.h
* for the best explanations of this ordering.
*/
bool __bitmap_equal(const unsigned long *bitmap1,
const unsigned long *bitmap2, unsigned int bits)
{
unsigned int k, lim = bits/BITS_PER_LONG;
for (k = 0; k < lim; ++k)
if (bitmap1[k] != bitmap2[k])
return false;
if (bits % BITS_PER_LONG)
if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
return false;
return true;
}
EXPORT_SYMBOL(__bitmap_equal);
bool __bitmap_or_equal(const unsigned long *bitmap1,
const unsigned long *bitmap2,
const unsigned long *bitmap3,
unsigned int bits)
{
unsigned int k, lim = bits / BITS_PER_LONG;
unsigned long tmp;
for (k = 0; k < lim; ++k) {
if ((bitmap1[k] | bitmap2[k]) != bitmap3[k])
return false;
}
if (!(bits % BITS_PER_LONG))
return true;
tmp = (bitmap1[k] | bitmap2[k]) ^ bitmap3[k];
return (tmp & BITMAP_LAST_WORD_MASK(bits)) == 0;
}
EXPORT_SYMBOL(__bitmap_or_equal);
void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits)
{
unsigned int k, lim = BITS_TO_LONGS(bits);
for (k = 0; k < lim; ++k)
dst[k] = ~src[k];
}
EXPORT_SYMBOL(__bitmap_complement);
/**
* __bitmap_shift_right - logical right shift of the bits in a bitmap
* @dst : destination bitmap
* @src : source bitmap
* @shift : shift by this many bits
* @nbits : bitmap size, in bits
*
* Shifting right (dividing) means moving bits in the MS -> LS bit
* direction. Zeros are fed into the vacated MS positions and the
Annotation
- Immediate include surface: `linux/bitmap.h`, `linux/bitops.h`, `linux/ctype.h`, `linux/device.h`, `linux/export.h`, `linux/slab.h`.
- Detected declarations: `function Boolean`, `function __bitmap_or_equal`, `function __bitmap_complement`, `function right`, `function left`, `function bitmap_cut`, `function __bitmap_and`, `function __bitmap_or`, `function __bitmap_xor`, `function __bitmap_andnot`.
- Atlas domain: Kernel Services / lib.
- Implementation status: integration implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.