Skip to content

Commit 9c43240

Browse files
author
Kent Overstreet
committed
bcachefs: fix eytzinger0_find_gt()
- fix return types: promoting from unsigned to ssize_t does not do what we want here, and was pointless since the rest of the eytzinger code is u32 - nr, not size Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
1 parent b897b14 commit 9c43240

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

fs/bcachefs/eytzinger.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ static inline unsigned inorder_to_eytzinger0(unsigned i, unsigned size)
242242
(_i) = eytzinger0_next((_i), (_size)))
243243

244244
/* return greatest node <= @search, or -1 if not found */
245-
static inline ssize_t eytzinger0_find_le(void *base, size_t nr, size_t size,
246-
cmp_func_t cmp, const void *search)
245+
static inline int eytzinger0_find_le(void *base, size_t nr, size_t size,
246+
cmp_func_t cmp, const void *search)
247247
{
248248
unsigned i, n = 0;
249249

@@ -256,18 +256,32 @@ static inline ssize_t eytzinger0_find_le(void *base, size_t nr, size_t size,
256256
} while (n < nr);
257257

258258
if (n & 1) {
259-
/* @i was greater than @search, return previous node: */
259+
/*
260+
* @i was greater than @search, return previous node:
261+
*
262+
* if @i was leftmost/smallest element,
263+
* eytzinger0_prev(eytzinger0_first())) returns -1, as expected
264+
*/
260265
return eytzinger0_prev(i, nr);
261266
} else {
262267
return i;
263268
}
264269
}
265270

266-
static inline ssize_t eytzinger0_find_gt(void *base, size_t nr, size_t size,
267-
cmp_func_t cmp, const void *search)
271+
static inline int eytzinger0_find_gt(void *base, size_t nr, size_t size,
272+
cmp_func_t cmp, const void *search)
268273
{
269274
ssize_t idx = eytzinger0_find_le(base, nr, size, cmp, search);
270-
return eytzinger0_next(idx, size);
275+
276+
/*
277+
* if eytitzinger0_find_le() returned -1 - no element was <= search - we
278+
* want to return the first element; next/prev identities mean this work
279+
* as expected
280+
*
281+
* similarly if find_le() returns last element, we should return -1;
282+
* identities mean this all works out:
283+
*/
284+
return eytzinger0_next(idx, nr);
271285
}
272286

273287
#define eytzinger0_find(base, nr, size, _cmp, search) \

0 commit comments

Comments
 (0)