lib/packing.c
Source file repositories/reference/linux-study-clean/lib/packing.c
File Facts
- System
- Linux kernel
- Corpus path
lib/packing.c- Extension
.c- Size
- 16865 bytes
- Lines
- 479
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/packing.hlinux/module.hlinux/bitops.hlinux/bits.hlinux/errno.hlinux/types.hlinux/bitrev.h
Detected Declarations
function bufferfunction __packfunction packfunction __unpackfunction unpackfunction copiedfunction ustruct_field_to_u64function u64_to_ustruct_fieldfunction expectedfunction expectedfunction expectedfunction expectedexport packexport unpackexport packingexport pack_fields_u8export pack_fields_u16export unpack_fields_u8export unpack_fields_u16
Annotated Snippet
if (quirks & QUIRK_MSB_ON_THE_RIGHT) {
pval = bitrev8(pval);
box_mask = bitrev8(box_mask);
}
((u8 *)pbuf)[box_addr] &= ~box_mask;
((u8 *)pbuf)[box_addr] |= pval;
}
}
/**
* pack - Pack u64 number into bitfield of buffer.
*
* @pbuf: Pointer to a buffer holding the packed value.
* @uval: CPU-readable unpacked value to pack.
* @startbit: The index (in logical notation, compensated for quirks) where
* the packed value starts within pbuf. Must be larger than, or
* equal to, endbit.
* @endbit: The index (in logical notation, compensated for quirks) where
* the packed value ends within pbuf. Must be smaller than, or equal
* to, startbit.
* @pbuflen: The length in bytes of the packed buffer pointed to by @pbuf.
* @quirks: A bit mask of QUIRK_LITTLE_ENDIAN, QUIRK_LSW32_IS_FIRST and
* QUIRK_MSB_ON_THE_RIGHT.
*
* Return: 0 on success, EINVAL or ERANGE if called incorrectly. Assuming
* correct usage, return code may be discarded. The @pbuf memory will
* be modified on success.
*/
int pack(void *pbuf, u64 uval, size_t startbit, size_t endbit, size_t pbuflen,
u8 quirks)
{
/* startbit is expected to be larger than endbit, and both are
* expected to be within the logically addressable range of the buffer.
*/
if (unlikely(startbit < endbit || startbit >= BITS_PER_BYTE * pbuflen))
/* Invalid function call */
return -EINVAL;
if (unlikely(startbit - endbit >= 64))
return -ERANGE;
__pack(pbuf, uval, startbit, endbit, pbuflen, quirks);
return 0;
}
EXPORT_SYMBOL(pack);
static void __unpack(const void *pbuf, u64 *uval, size_t startbit, size_t endbit,
size_t pbuflen, u8 quirks)
{
/* Logical byte indices corresponding to the
* start and end of the field.
*/
int plogical_first_u8 = startbit / BITS_PER_BYTE;
int plogical_last_u8 = endbit / BITS_PER_BYTE;
int box;
/* Initialize parameter */
*uval = 0;
/* Iterate through an idealistic view of the pbuf as an u64 with
* no quirks, u8 by u8 (aligned at u8 boundaries), from high to low
* logical bit significance. "box" denotes the current logical u8.
*/
for (box = plogical_first_u8; box >= plogical_last_u8; box--) {
/* Bit indices into the currently accessed 8-bit box */
size_t box_start_bit, box_end_bit, box_addr;
u8 box_mask;
/* Corresponding bits from the unpacked u64 parameter */
size_t proj_start_bit, proj_end_bit;
u64 proj_mask;
u64 pval;
/* This u8 may need to be accessed in its entirety
* (from bit 7 to bit 0), or not, depending on the
* input arguments startbit and endbit.
*/
if (box == plogical_first_u8)
box_start_bit = startbit % BITS_PER_BYTE;
else
box_start_bit = 7;
if (box == plogical_last_u8)
box_end_bit = endbit % BITS_PER_BYTE;
else
box_end_bit = 0;
/* We have determined the box bit start and end.
* Now we calculate where this (masked) u8 box would fit
* in the unpacked (CPU-readable) u64 - the u8 box's
Annotation
- Immediate include surface: `linux/packing.h`, `linux/module.h`, `linux/bitops.h`, `linux/bits.h`, `linux/errno.h`, `linux/types.h`, `linux/bitrev.h`.
- Detected declarations: `function buffer`, `function __pack`, `function pack`, `function __unpack`, `function unpack`, `function copied`, `function ustruct_field_to_u64`, `function u64_to_ustruct_field`, `function expected`, `function expected`.
- 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.