summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--thunk.h17
1 files changed, 7 insertions, 10 deletions
diff --git a/thunk.h b/thunk.h
index f02aff8..d1f407e 100644
--- a/thunk.h
+++ b/thunk.h
@@ -706,25 +706,22 @@ struct thunk_t {
*
* After the thunked function returns, the instance is automatically freed,
* including any payload if THUNK_ALLOC() was used.
+ *
+ * The thunk may suppress automatic freeing by returning 1 (or anything > 0),
+ * giving the callee ability to influence instance life-cycle. This is
+ * important for iterators/visitor patterns where the thunk should be reused
+ * until either finding a desired entry or reaching the end.
*/
static inline int thunk_dispatch(thunk_t *thunk) {
int r;
r = thunk->dispatch(thunk);
- thunk->free(thunk);
+ if (r <= 0)
+ thunk->free(thunk);
return r;
}
-/* Same as thunk_dispatch() but without the free.
- * Callers can trivially free thunk instances w/thunk_free().
- * So when a thunk needs to be used repeatedly in a loop for instance, use
- * this, then just free the thunk yourself.
- */
-static inline int thunk_dispatch_keep(thunk_t *thunk) {
- return thunk->dispatch(thunk);
-}
-
static inline void thunk_free(thunk_t *thunk) {
return thunk->free(thunk);
}
© All Rights Reserved