cedric pushed a commit to branch master. http://git.enlightenment.org/core/efl.git/commit/?id=ce56f32270c2f75adab022b701d3bdcbbbc3fb44
commit ce56f32270c2f75adab022b701d3bdcbbbc3fb44 Author: Cedric BAIL <[email protected]> Date: Wed Dec 19 16:25:38 2018 -0800 eina: add a function to free Eina_Promise attached data when the promise is destroyed. Reviewed-by: Lauro Neto <Lauro Moura <[email protected]>> Differential Revision: https://phab.enlightenment.org/D7491 --- src/lib/eina/eina_promise.c | 21 ++++++++++++++++++--- src/lib/eina/eina_promise.h | 8 ++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/lib/eina/eina_promise.c b/src/lib/eina/eina_promise.c index 222f7d5613..c239ba6261 100644 --- a/src/lib/eina/eina_promise.c +++ b/src/lib/eina/eina_promise.c @@ -103,6 +103,7 @@ struct _Eina_Promise { Eina_Future *future; Eina_Future_Scheduler *scheduler; Eina_Promise_Cancel_Cb cancel; + Eina_Free_Cb free_cb; const void *data; }; @@ -305,13 +306,20 @@ _eina_promise_link(Eina_Promise *p, Eina_Future *f) DBG("Linking future %p with promise %p", f, p); } +static void +_eina_promise_free(Eina_Promise *p) +{ + if (p->free_cb) p->free_cb((void*) p->data); + eina_mempool_free(_promise_mp, p); +} + static void _eina_promise_cancel(Eina_Promise *p) { DBG("Cancelling promise: %p, data: %p, future: %p", p, p->data, p->future); _eina_promise_unlink(p); p->cancel((void *)p->data, p); - eina_mempool_free(_promise_mp, p); + _eina_promise_free(p); } static void @@ -516,7 +524,7 @@ _eina_promise_deliver(Eina_Promise *p, DBG("Promise %p has no future", p); eina_value_flush(&value); } - eina_mempool_free(_promise_mp, p); + _eina_promise_free(p); } Eina_Bool @@ -635,7 +643,7 @@ _eina_promise_clean_dispatch(Eina_Promise *p, Eina_Value v) // This function is called on a promise created with a scheduler, not a continue one. _eina_future_dispatch(p->scheduler, f, v); } - eina_mempool_free(_promise_mp, p); + _eina_promise_free(p); } static Eina_Value @@ -1111,6 +1119,13 @@ eina_promise_data_set(Eina_Promise *p, p->data = data; } +EAPI void +eina_promise_data_free_cb_set(Eina_Promise *p, + Eina_Free_Cb free_cb) +{ + EINA_SAFETY_ON_NULL_RETURN(p); + p->free_cb = free_cb; +} static Eina_Value _eina_future_cb_easy(void *data, const Eina_Value value, diff --git a/src/lib/eina/eina_promise.h b/src/lib/eina/eina_promise.h index 14e550d866..1896260dba 100644 --- a/src/lib/eina/eina_promise.h +++ b/src/lib/eina/eina_promise.h @@ -628,6 +628,14 @@ EAPI void *eina_promise_data_get(const Eina_Promise *p) EINA_ARG_NONNULL(1); */ EAPI void eina_promise_data_set(Eina_Promise *p, void *data) EINA_ARG_NONNULL(1); +/** + * Sets the free callback used when the data attached on the promise is freed just before the destruction of the promise itself. + * + * @param[in] p The promise to set the free callback on. + * @param[in] free_cb The free callback. + */ +EAPI void eina_promise_data_free_cb_set(Eina_Promise *p, Eina_Free_Cb free_cb); + /** * Resolves a promise. * --
