include/linux/util_macros.h

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

File Facts

System
Linux kernel
Corpus path
include/linux/util_macros.h
Extension
.h
Size
5335 bytes
Lines
164
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

if (__fc_x <= __fc_mid_x) {				\
			__fc_left = __fc_x - __fc_a[__fc_i];		\
			__fc_right = __fc_a[__fc_i + 1] - __fc_x;	\
			if (__fc_right < __fc_left)			\
				__fc_i++;				\
			break;						\
		}							\
	}								\
	(__fc_i);							\
})

/**
 * find_closest_descending - locate the closest element in a sorted array
 * @x: The reference value.
 * @a: The array in which to look for the closest element. Must be sorted
 *  in descending order.
 * @as: Size of 'a'.
 *
 * Similar to find_closest() but 'a' is expected to be sorted in descending
 * order. The iteration is done in reverse order, so that the comparison
 * of '__fc_right' & '__fc_left' also works for unsigned numbers.
 */
#define find_closest_descending(x, a, as)				\
({									\
	typeof(as) __fc_i, __fc_as = (as) - 1;				\
	long __fc_mid_x, __fc_x = (x);					\
	long __fc_left, __fc_right;					\
	typeof(*a) const *__fc_a = (a);					\
	for (__fc_i = __fc_as; __fc_i >= 1; __fc_i--) {			\
		__fc_mid_x = (__fc_a[__fc_i] + __fc_a[__fc_i - 1]) / 2;	\
		if (__fc_x <= __fc_mid_x) {				\
			__fc_left = __fc_x - __fc_a[__fc_i];		\
			__fc_right = __fc_a[__fc_i - 1] - __fc_x;	\
			if (__fc_right < __fc_left)			\
				__fc_i--;				\
			break;						\
		}							\
	}								\
	(__fc_i);							\
})

/**
 * PTR_IF - evaluate to @ptr if @cond is true, or to NULL otherwise.
 * @cond: A conditional, usually in a form of IS_ENABLED(CONFIG_FOO)
 * @ptr: A pointer to assign if @cond is true.
 *
 * PTR_IF(IS_ENABLED(CONFIG_FOO), ptr) evaluates to @ptr if CONFIG_FOO is set
 * to 'y' or 'm', or to NULL otherwise. The @ptr argument must be a pointer.
 *
 * The macro can be very useful to help compiler dropping dead code.
 *
 * For instance, consider the following::
 *
 *     #ifdef CONFIG_FOO_SUSPEND
 *     static int foo_suspend(struct device *dev)
 *     {
 *        ...
 *     }
 *     #endif
 *
 *     static struct pm_ops foo_ops = {
 *     #ifdef CONFIG_FOO_SUSPEND
 *         .suspend = foo_suspend,
 *     #endif
 *     };
 *
 * While this works, the foo_suspend() macro is compiled conditionally,
 * only when CONFIG_FOO_SUSPEND is set. This is problematic, as there could
 * be a build bug in this function, we wouldn't have a way to know unless
 * the configuration option is set.
 *
 * An alternative is to declare foo_suspend() always, but mark it
 * as __maybe_unused. This works, but the __maybe_unused attribute
 * is required to instruct the compiler that the function may not
 * be referenced anywhere, and is safe to remove without making
 * a fuss about it. This makes the programmer responsible for tagging
 * the functions that can be garbage-collected.
 *
 * With the macro it is possible to write the following::
 *
 *     static int foo_suspend(struct device *dev)
 *     {
 *        ...
 *     }
 *
 *     static struct pm_ops foo_ops = {
 *         .suspend = PTR_IF(IS_ENABLED(CONFIG_FOO_SUSPEND), foo_suspend),
 *     };
 *
 * The foo_suspend() function will now be automatically dropped by the

Annotation

Implementation Notes