From: Aurelien DARRAGON Date: Thu, 6 Jul 2023 14:55:55 +0000 (+0200) Subject: MINOR: sink: add helper function to deallocate sink struct X-Git-Tag: v2.9-dev5~18 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5a8755681d455b2d7d0f1310a511d61ca7a6d664;p=thirdparty%2Fhaproxy.git MINOR: sink: add helper function to deallocate sink struct In this patch we move sink freeing logic outside of sink_deinit() function in order to create the sink_free() helper function that could be used on error paths for example. --- diff --git a/src/sink.c b/src/sink.c index 3768a44bde..1e0355cb5f 100644 --- a/src/sink.c +++ b/src/sink.c @@ -771,6 +771,40 @@ void sink_rotate_file_backed_ring(const char *name) } } + +/* helper function to completely deallocate a sink struct + */ +static void sink_free(struct sink *sink) +{ + struct sink_forward_target *sft_next; + + if (!sink) + return; + if (sink->type == SINK_TYPE_BUFFER) { + if (sink->store) { + size_t size = (sink->ctx.ring->buf.size + 4095UL) & -4096UL; + void *area = (sink->ctx.ring->buf.area - sizeof(*sink->ctx.ring)); + + msync(area, size, MS_SYNC); + munmap(area, size); + ha_free(&sink->store); + } + else + ring_free(sink->ctx.ring); + } + LIST_DEL_INIT(&sink->sink_list); // remove from parent list + task_destroy(sink->forward_task); + free_proxy(sink->forward_px); + ha_free(&sink->name); + ha_free(&sink->desc); + while (sink->sft) { + sft_next = sink->sft->next; + ha_free(&sink->sft); + sink->sft = sft_next; + } + ha_free(&sink); +} + /* * Parse "ring" section and create corresponding sink buffer. * @@ -1304,33 +1338,9 @@ static void sink_init() static void sink_deinit() { struct sink *sink, *sb; - struct sink_forward_target *sft_next; - - list_for_each_entry_safe(sink, sb, &sink_list, sink_list) { - if (sink->type == SINK_TYPE_BUFFER) { - if (sink->store) { - size_t size = (sink->ctx.ring->buf.size + 4095UL) & -4096UL; - void *area = (sink->ctx.ring->buf.area - sizeof(*sink->ctx.ring)); - msync(area, size, MS_SYNC); - munmap(area, size); - ha_free(&sink->store); - } - else - ring_free(sink->ctx.ring); - } - LIST_DELETE(&sink->sink_list); - task_destroy(sink->forward_task); - free_proxy(sink->forward_px); - free(sink->name); - free(sink->desc); - while (sink->sft) { - sft_next = sink->sft->next; - free(sink->sft); - sink->sft = sft_next; - } - free(sink); - } + list_for_each_entry_safe(sink, sb, &sink_list, sink_list) + sink_free(sink); } INITCALL0(STG_REGISTER, sink_init);