summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2021-08-23 20:12:01 -0700
committerVito Caputo <vcaputo@pengaru.com>2021-08-23 20:16:30 -0700
commitcaa8f4319a8e9ef096e03857f0f85b48c0930a5e (patch)
treeb604240a362ab2aa0ccc94987852f2996e10a5ca
parent22ea32f0f945509888a3d0d5167b5d4ed001a3d9 (diff)
thunk: deprecate thunk_dispatch_keep()
Enable callees to control lifecycle via return values in thunk_dispatch() instead. The thunk_dispatch_keep() function encuraged caller-determined lifecycles which really doesn't work in practice. Now if a thunk returns <= 0 it will be freed, negative values are expected to be used for propagating out fatal errors. A positive value (conventionally will just be 1) will bypass the free, which is expected to be used in thunk reuse situations like iterators.
-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);
}
© 2021 All Rights Reserved