OSDN Git Service

nbd/client: Simplify cookie vs. index computation
authorEric Blake <eblake@redhat.com>
Thu, 8 Jun 2023 13:56:35 +0000 (08:56 -0500)
committerEric Blake <eblake@redhat.com>
Wed, 19 Jul 2023 20:26:13 +0000 (15:26 -0500)
commit8cb98a725e7397c9de25ebd77c00b1d5f2d8351e
treed0db8b66e4d7ce26c8f5cc9d4b9ae2433b92e9e6
parent22efd81104f07f7dbe571a923cc43ed625560551
nbd/client: Simplify cookie vs. index computation

Our code relies on a sentinel cookie value of zero for deciding when a
packet has been handled, as well as relying on array indices between 0
and MAX_NBD_REQUESTS-1 for dereferencing purposes.  As long as we can
symmetrically convert between two forms, there is no reason to go with
the odd choice of using XOR with a random pointer, when we can instead
simplify the mappings with a mere offset of 1.

Using ((uint64_t)-1) as the sentinel instead of NULL such that the two
macros could be entirely eliminated might also be possible, but would
require a more careful audit to find places where we currently rely on
zero-initialization to be interpreted as the sentinel value, so I did
not pursue that course.

Signed-off-by: Eric Blake <eblake@redhat.com>
Message-ID: <20230608135653.2918540-7-eblake@redhat.com>
[eblake: enhance commit message]
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
block/nbd.c