From a2611cdc3c6d7c615ac7057087b3060cb46f9400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Thu, 22 Jun 2017 22:45:25 +0200 Subject: Rework constructors/destructors --- zyklonb.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'zyklonb.c') diff --git a/zyklonb.c b/zyklonb.c index b1a9282..9a51d85 100644 --- a/zyklonb.c +++ b/zyklonb.c @@ -83,11 +83,10 @@ struct plugin struct str write_buffer; ///< Output yet to be sent out }; -static void -plugin_init (struct plugin *self) +static struct plugin * +plugin_new (void) { - memset (self, 0, sizeof *self); - + struct plugin *self = xcalloc (1, sizeof *self); self->pid = -1; self->queued_output = str_make (); @@ -95,10 +94,11 @@ plugin_init (struct plugin *self) self->read_buffer = str_make (); self->write_fd = -1; self->write_buffer = str_make (); + return self; } static void -plugin_free (struct plugin *self) +plugin_destroy (struct plugin *self) { soft_assert (self->pid == -1); free (self->name); @@ -113,6 +113,8 @@ plugin_free (struct plugin *self) if (!self->initialized) str_free (&self->queued_output); + + free (self); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -192,13 +194,8 @@ bot_context_free (struct bot_context *self) str_free (&self->read_buffer); // TODO: terminate the plugins properly before this is called - struct plugin *link, *tmp; - for (link = self->plugins; link; link = tmp) - { - tmp = link->next; - plugin_free (link); - free (link); - } + LIST_FOR_EACH (struct plugin, link, self->plugins) + plugin_destroy (link); if (self->irc_fd != -1) { @@ -1099,8 +1096,7 @@ plugin_launch (struct bot_context *ctx, const char *name, struct error **e) xclose (stdin_pipe[0]); xclose (stdout_pipe[1]); - struct plugin *plugin = xmalloc (sizeof *plugin); - plugin_init (plugin); + struct plugin *plugin = plugin_new (); plugin->ctx = ctx; plugin->pid = pid; plugin->name = xstrdup (name); @@ -1876,8 +1872,7 @@ on_plugin_death (struct plugin *plugin, int status) plugin->read_fd = -1; LIST_UNLINK (ctx->plugins, plugin); - plugin_free (plugin); - free (plugin); + plugin_destroy (plugin); // Living child processes block us from quitting try_finish_quit (ctx); -- cgit v1.2.3