include/linux/packing.h

Source file repositories/reference/linux-study-clean/include/linux/packing.h

File Facts

System
Linux kernel
Corpus path
include/linux/packing.h
Extension
.h
Size
16495 bytes
Lines
459
Domain
Core OS
Bucket
Core Kernel Interface
Inferred role
Core OS: implementation source
Status
source implementation candidate

Why This File Exists

Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.

Dependency Surface

Detected Declarations

Annotated Snippet

#ifndef _LINUX_PACKING_H
#define _LINUX_PACKING_H

#include <linux/array_size.h>
#include <linux/bitops.h>
#include <linux/build_bug.h>
#include <linux/minmax.h>
#include <linux/stddef.h>
#include <linux/types.h>

#define GEN_PACKED_FIELD_STRUCT(__type) \
	struct packed_field_ ## __type { \
		__type startbit; \
		__type endbit; \
		__type offset; \
		__type size; \
	}

/* struct packed_field_u8. Use with bit offsets < 256, buffers < 32B and
 * unpacked structures < 256B.
 */
GEN_PACKED_FIELD_STRUCT(u8);

/* struct packed_field_u16. Use with bit offsets < 65536, buffers < 8KB and
 * unpacked structures < 64KB.
 */
GEN_PACKED_FIELD_STRUCT(u16);

#define PACKED_FIELD(start, end, struct_name, struct_field) \
{ \
	(start), \
	(end), \
	offsetof(struct_name, struct_field), \
	sizeof_field(struct_name, struct_field), \
}

#define CHECK_PACKED_FIELD_OVERLAP(fields, index1, index2) ({ \
	typeof(&(fields)[0]) __f = (fields); \
	typeof(__f[0]) _f1 = __f[index1]; typeof(__f[0]) _f2 = __f[index2]; \
	const bool _ascending = __f[0].startbit < __f[1].startbit; \
	BUILD_BUG_ON_MSG(_ascending && _f1.startbit >= _f2.startbit, \
			 __stringify(fields) " field " __stringify(index2) \
			 " breaks ascending order"); \
	BUILD_BUG_ON_MSG(!_ascending && _f1.startbit <= _f2.startbit, \
			 __stringify(fields) " field " __stringify(index2) \
			 " breaks descending order"); \
	BUILD_BUG_ON_MSG(max(_f1.endbit, _f2.endbit) <= \
			 min(_f1.startbit, _f2.startbit), \
			 __stringify(fields) " field " __stringify(index2) \
			 " overlaps with previous field"); \
})

#define CHECK_PACKED_FIELD(fields, index) ({ \
	typeof(&(fields)[0]) _f = (fields); \
	typeof(_f[0]) __f = _f[index]; \
	BUILD_BUG_ON_MSG(__f.startbit < __f.endbit, \
			 __stringify(fields) " field " __stringify(index) \
			 " start bit must not be smaller than end bit"); \
	BUILD_BUG_ON_MSG(__f.size != 1 && __f.size != 2 && \
			 __f.size != 4 && __f.size != 8, \
			 __stringify(fields) " field " __stringify(index) \
			" has unsupported unpacked storage size"); \
	BUILD_BUG_ON_MSG(__f.startbit - __f.endbit >= BITS_PER_BYTE * __f.size, \
			 __stringify(fields) " field " __stringify(index) \
			 " exceeds unpacked storage size"); \
	__builtin_choose_expr(index != 0, \
			      CHECK_PACKED_FIELD_OVERLAP(fields, index - 1, index), \
			      1); \
})

/* Note that the packed fields may be either in ascending or descending order.
 * Thus, we must check that both the first and last field wit within the
 * packed buffer size.
 */
#define CHECK_PACKED_FIELDS_SIZE(fields, pbuflen) ({ \
	typeof(&(fields)[0]) _f = (fields); \
	typeof(pbuflen) _len = (pbuflen); \
	const size_t num_fields = ARRAY_SIZE(fields); \
	BUILD_BUG_ON_MSG(!__builtin_constant_p(_len), \
			 __stringify(fields) " pbuflen " __stringify(pbuflen) \
			 " must be a compile time constant"); \
	BUILD_BUG_ON_MSG(_f[0].startbit >= BITS_PER_BYTE * _len, \
			 __stringify(fields) " first field exceeds packed buffer size"); \
	BUILD_BUG_ON_MSG(_f[num_fields - 1].startbit >= BITS_PER_BYTE * _len, \
			 __stringify(fields) " last field exceeds packed buffer size"); \
})

#define QUIRK_MSB_ON_THE_RIGHT	BIT(0)
#define QUIRK_LITTLE_ENDIAN	BIT(1)
#define QUIRK_LSW32_IS_FIRST	BIT(2)

Annotation

Implementation Notes