Date: Wed, 29 Oct 2025 02:18:55 +0800 From: Zhenlei Huang <zlei@FreeBSD.org> To: Gleb Smirnoff <glebius@freebsd.org> Cc: =?utf-8?Q?Dag-Erling_Sm=C3=B8rgrav?= <des@freebsd.org>, src-committers@freebsd.org, dev-commits-src-all@freebsd.org, dev-commits-src-main@freebsd.org Subject: Re: git: 3d73146baeb9 - main - pwait: Add an option to print remaining processes Message-ID: <DBABFDB7-184E-4C7F-9A9C-B12A2C07C08C@FreeBSD.org> In-Reply-To: <aQD9M9K-Zk0M-213@cell.glebi.us> References: <202510281157.59SBvhNB002870@gitrepo.freebsd.org> <aQD9M9K-Zk0M-213@cell.glebi.us>
index | next in thread | previous in thread | raw e-mail
> 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
home |
help
Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?DBABFDB7-184E-4C7F-9A9C-B12A2C07C08C>
