diff options
Diffstat (limited to 'src/afb-session.c')
-rw-r--r-- | src/afb-session.c | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/afb-session.c b/src/afb-session.c index db7a99a3..61bce09a 100644 --- a/src/afb-session.c +++ b/src/afb-session.c @@ -433,7 +433,7 @@ static struct cookie *cookie_add(struct afb_session *session, int idx, const voi return cookie; } -void *afb_session_cookie(struct afb_session *session, const void *key, void *(*makecb)(void), void (*freecb)(void*)) +void *afb_session_cookie(struct afb_session *session, const void *key, void *(*makecb)(void *closure), void (*freecb)(void *item), void *closure, int replace) { int idx; void *value; @@ -441,11 +441,19 @@ void *afb_session_cookie(struct afb_session *session, const void *key, void *(*m lock(session); cookie = cookie_search(session, key, &idx); - if (cookie) - value = cookie->value; - else { - value = makecb ? makecb() : NULL; - if (makecb || freecb) { + if (cookie) { + if (!replace) + value = cookie->value; + else { + value = makecb ? makecb(closure) : closure; + if (cookie->value != value && cookie->freecb) + cookie->freecb(cookie->value); + cookie->value = value; + cookie->freecb = freecb; + } + } else { + value = makecb ? makecb(closure) : closure; + if (replace || makecb || freecb) { cookie = cookie_add(session, idx, key, value, freecb); if (!cookie) { if (makecb && freecb) |