Page MenuHomeFreeBSD

tree(3): allow the compare function to return any signed type
ClosedPublic

Authored by glebius on Jul 6 2022, 9:20 AM.
Tags
None
Referenced Files
F166510173: D35722.id107871.diff
Thu, Aug 13, 9:09 PM
F166510155: D35722.id107809.diff
Thu, Aug 13, 9:09 PM
F166509907: D35722.diff
Thu, Aug 13, 9:09 PM
F166323374: D35722.id.diff
Wed, Aug 12, 10:05 PM
Unknown Object (File)
Tue, Aug 11, 2:45 PM
Unknown Object (File)
Mon, Aug 10, 7:22 AM
Unknown Object (File)
Mon, Aug 10, 5:39 AM
Unknown Object (File)
Sun, Aug 9, 3:40 AM
Subscribers

Details

Summary

This allows to write very short comparison function when we are
comparing just pointer values:

return ((intptr_t)((uintptr_t)a->ptr - (uintptr_t)b->ptr));

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

How portable is __typeof? Is it in a standard now, or sometime soon?

How portable is __typeof? Is it in a standard now, or sometime soon?

AFAIK, it is not part of standard, yet. The point is that tree.h already requires it for RB_PARENT :)

Point taken on __typeof.

In your example of use

return ((intptr_t)((uintptr_t)a->ptr - (uintptr_t)b->ptr));

can overflow, but (assuming that ptr points to something bigger than char)

return ((intptr_t)((uintptr_t)a->ptr/2 - (uintptr_t)b->ptr/2));

does not. Otherwise, it's okay by me.

This revision is now accepted and ready to land.Jul 6 2022, 6:36 PM

Good point. Maybe /2 of the result of the difference.

Good point. Maybe /2 of the result of the difference.

No, it needs to be done before the subtraction, as Doug wrote it.

Yep, now I also see it. Thanks!