]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sink: add helper function to deallocate sink struct
authorAurelien DARRAGON <adarragon@haproxy.com>
Thu, 6 Jul 2023 14:55:55 +0000 (16:55 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 6 Sep 2023 14:06:39 +0000 (16:06 +0200)
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.

src/sink.c

index 3768a44bdeeeca967ffdb1f1c1c07818da6b0ec8..1e0355cb5f76d43fcef32485ea10e434fcb6b796 100644 (file)
@@ -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);