Re: git: 3d73146baeb9 - main - pwait: Add an option to print remaining processes

From: Zhenlei Huang <zlei_at_FreeBSD.org>
Date: Tue, 28 Oct 2025 18:18:55 UTC

> On Oct 29, 2025, at 1:28 AM, Gleb Smirnoff <glebius@freebsd.org> wrote:
> 
> On Tue, Oct 28, 2025 at 11:57:43AM +0000, Dag-Erling Smørgrav wrote:
> D> +static int
> D> +pidcmp(const struct pid *a, const struct pid *b)
> D> +{
> D> +	return (a->pid > b->pid ? 1 : a->pid < b->pid ? -1 : 0);
> D> +}
> D> +
> D> +RB_HEAD(pidtree, pid);
> D> +static struct pidtree pids = RB_INITIALIZER(&pids);
> D> +RB_GENERATE_STATIC(pidtree, pid, entry, pidcmp);
> 
> We have a nice trick in our tree(3) that allows to use lighter compare
> functions.  The function can return any signed integer type, thus we
> can:
> 
> static pid_t
> pidcmp(const struct pid *a, const struct pid *b)
> {
> 	return (a->pid - b->pid);
> }

I'd prefer to return const 1 / -1 / 0, that is straight forward of a comparator. Also the compiler is smart
enough to catch this pattern and generate optimized code, from my experiment which is long long time ago.

The pid has type pid_t which is a type redefinition of  __int32_t. Although the pid will not reach
2^31 or -2^31 - 1, in principle the computing of the delta of the two signed integers may overflow
and that is bad smell.

Best regards,
Zhenlei

> 
> -- 
> Gleb Smirnoff