From aa69d8fa1c40b6f1c6a41bda6565f7a4de2c8083 Mon Sep 17 00:00:00 2001 From: Christopher Faulet Date: Tue, 12 Apr 2022 18:09:48 +0200 Subject: [PATCH] MINOR: conn-stream: Use a dedicated function to conditionally remove a CS cs_free_cond() must now be used to remove a CS. cs_free() may be used on error path to release a freshly allocated but unused CS. But in all other cases cs_free_cond() must be used. This function takes care to release the CS if it is possible (no app and detached from any endpoint). In fact, this function is only used internally. From the outside, cs_detach_* functions are used. --- src/conn_stream.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/conn_stream.c b/src/conn_stream.c index 0c6f1936a3..572c13e4f3 100644 --- a/src/conn_stream.c +++ b/src/conn_stream.c @@ -244,6 +244,15 @@ void cs_free(struct conn_stream *cs) pool_free(pool_head_connstream, cs); } +/* Conditionally removes a conn-stream if it is detached and it there is no app + * layer defined. Except on error path, this one must be used. + */ +static void cs_free_cond(struct conn_stream *cs) +{ + if (!cs->app && (!cs->endp || (cs->endp->flags & CS_EP_DETACHED))) + cs_free(cs); +} + /* Attaches a conn_stream to a mux endpoint and sets the endpoint ctx. Returns * -1 on error and 0 on sucess. CS_EP_DETACHED flag is removed. This function is @@ -385,9 +394,7 @@ void cs_detach_endp(struct conn_stream *cs) if (cs_strm(cs)) cs->ops = &cs_app_embedded_ops; cs->data_cb = NULL; - - if (cs->app == NULL) - cs_free(cs); + cs_free_cond(cs); } /* Detaches the conn_stream from the app layer. If there is no endpoint attached @@ -404,9 +411,7 @@ void cs_detach_app(struct conn_stream *cs) tasklet_free(cs->wait_event.tasklet); cs->wait_event.tasklet = NULL; cs->wait_event.events = 0; - - if (!cs->endp || (cs->endp->flags & CS_EP_DETACHED)) - cs_free(cs); + cs_free_cond(cs); } /* Resets the conn-stream endpoint. It happens when the app layer want to renew