[PATCH 1/2] eal: RTE_PTR_ADD/SUB char* for compiler optimizations

Stephen Hemminger stephen at networkplumber.org
Sun Jan 11 16:59:19 CET 2026


On Sun, 11 Jan 2026 10:00:32 -0500
scott.k.mitch1 at gmail.com wrote:

> +#define RTE_PTR_ADD(ptr, x) \
> +	(__extension__ ({ \
> +		/* Diagnostics suppressed for internal macro operations only. \
> +		 * Compiler type-checks all _Generic branches even when unselected, \
> +		 * triggering warnings with no external impact. */ \
> +		__rte_diagnostic_push \
> +		__rte_diagnostic_ignored_wcast_qual \
> +		_Pragma("GCC diagnostic ignored \"-Wconditional-type-mismatch\"") \
> +		/* Uses uintptr_t arithmetic for integer types (API compatibility), \
> +		 * and char* arithmetic for pointer types (enables optimization). */ \
> +		__auto_type _ptr_result = _Generic((ptr), \
> +			unsigned long long: ((void *)((uintptr_t)(ptr) + (x))), \
> +			long long:          ((void *)((uintptr_t)(ptr) + (x))), \
> +			unsigned long:      ((void *)((uintptr_t)(ptr) + (x))), \
> +			long:               ((void *)((uintptr_t)(ptr) + (x))), \
> +			unsigned int:       ((void *)((uintptr_t)(ptr) + (x))), \
> +			int:                ((void *)((uintptr_t)(ptr) + (x))), \
> +			unsigned short:     ((void *)((uintptr_t)(ptr) + (x))), \
> +			short:              ((void *)((uintptr_t)(ptr) + (x))), \
> +			unsigned char:      ((void *)((uintptr_t)(ptr) + (x))), \
> +			signed char:        ((void *)((uintptr_t)(ptr) + (x))), \
> +			char:               ((void *)((uintptr_t)(ptr) + (x))), \
> +			_Bool:              ((void *)((uintptr_t)(ptr) + (x))), \
> +			/* Ternary with null pointer constant: per C11, if one operand \
> +			 * is a null pointer constant and the other is a pointer, the \
> +			 * result type is qualified per the pointer operand, normalizing \
> +			 * const T* to const void* and T* to void*. */ \
> +			default: _Generic((1 ? (ptr) : (void *)0), \
> +				const void *: ((void *)((const char *)(ptr) + (x))), \
> +				default:      ((void *)((char *)(ptr) + (x))) \
> +			) \
> +		); \
> +		__rte_diagnostic_pop \
> +		_ptr_result; \
> +	}))

Good idea in general but the macro is way to big and therefore hard to read.
The comments could be outside the macro.

Any code that adds dependency on a pragma to work is brittle and likely
to allow bugs through. Please figure out how to do it without.


More information about the dev mailing list