aboutsummaryrefslogtreecommitdiff
path: root/cbtree.h
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2026-03-20 08:07:28 +0100
committerJunio C Hamano <gitster@pobox.com>2026-03-20 13:16:22 -0700
commitfe446b01aeaab307adcbfb39d4aaa72c37afbcda (patch)
tree5f28e0581528e50f32f44120a8890a328b2e136f /cbtree.h
parent1382e54a9c9e5f98271a943af9c10299c6ba934b (diff)
downloadgit-fe446b01aeaab307adcbfb39d4aaa72c37afbcda.tar.xz
oidtree: extend iteration to allow for arbitrary return codes
The interface `cb_each()` iterates through a crit-bit tree and calls a specific callback function for each of the contained items. The callback function is expected to return either: - `CB_CONTINUE` in case iteration shall continue. - `CB_BREAK` to abort iteration. This is needlessly restrictive though, as callers may want to return arbitrary values and have them be bubbled up to the `cb_each()` call site. In fact, this is a rather common pattern we have: whenever such a callback function returns a non-zero error code, we abort iteration and bubble up the code as-is. Refactor both the crit-bit tree and oidtree subsystems to behave accordingly. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'cbtree.h')
-rw-r--r--cbtree.h17
1 files changed, 9 insertions, 8 deletions
diff --git a/cbtree.h b/cbtree.h
index 43193abdda..c374b1b3db 100644
--- a/cbtree.h
+++ b/cbtree.h
@@ -30,11 +30,6 @@ struct cb_tree {
struct cb_node *root;
};
-enum cb_next {
- CB_CONTINUE = 0,
- CB_BREAK = 1
-};
-
#define CBTREE_INIT { 0 }
static inline void cb_init(struct cb_tree *t)
@@ -46,9 +41,15 @@ static inline void cb_init(struct cb_tree *t)
struct cb_node *cb_lookup(struct cb_tree *, const uint8_t *k, size_t klen);
struct cb_node *cb_insert(struct cb_tree *, struct cb_node *, size_t klen);
-typedef enum cb_next (*cb_iter)(struct cb_node *, void *arg);
+/*
+ * Callback invoked by `cb_each()` for each node in the critbit tree. A return
+ * value of 0 will cause the iteration to continue, a non-zero return code will
+ * cause iteration to abort. The error code will be relayed back from
+ * `cb_each()` in that case.
+ */
+typedef int (*cb_iter)(struct cb_node *, void *arg);
-void cb_each(struct cb_tree *, const uint8_t *kpfx, size_t klen,
- cb_iter, void *arg);
+int cb_each(struct cb_tree *, const uint8_t *kpfx, size_t klen,
+ cb_iter, void *arg);
#endif /* CBTREE_H */