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: [PATCH] Rework constructors/destructors --- degesch.c | 50 +++++++++++++++++--------------------------------- kike.c | 20 ++++++++------------ zyklonb.c | 27 +++++++++++---------------- 3 files changed, 36 insertions(+), 61 deletions(-) diff --git a/degesch.c b/degesch.c index eb25fd1..48e2c17 100644 --- a/degesch.c +++ b/degesch.c @@ -1467,15 +1467,12 @@ struct formatter size_t items_alloc; ///< Items allocated }; -static void -formatter_init (struct formatter *self, - struct app_context *ctx, struct server *s) +static struct formatter +formatter_make (struct app_context *ctx, struct server *s) { - memset (self, 0, sizeof *self); - self->ctx = ctx; - self->s = s; - self->items = xcalloc (sizeof *self->items, (self->items_alloc = 16)); - self->items_len = 0; + struct formatter self = { .ctx = ctx, .s = s }; + self.items = xcalloc (sizeof *self.items, (self.items_alloc = 16)); + return self; } static void @@ -3749,8 +3746,7 @@ buffer_update_time (struct app_context *ctx, time_t now, FILE *stream, return; } - struct formatter f; - formatter_init (&f, ctx, NULL); + struct formatter f = formatter_make (ctx, NULL); formatter_add (&f, "#a#s\n", ATTR_DATE_CHANGE, buf); formatter_flush (&f, stream, flush_opts); // Flush the trailing formatting reset item @@ -3805,8 +3801,7 @@ buffer_line_display (struct app_context *ctx, CALL (ctx->input, hide); - struct formatter f; - formatter_init (&f, ctx, NULL); + struct formatter f = formatter_make (ctx, NULL); buffer_line_write_time (&f, line, stdout, 0); // Ignore all formatting for messages coming from other buffers, that is @@ -3827,8 +3822,7 @@ static void buffer_line_write_to_backlog (struct app_context *ctx, struct buffer_line *line, FILE *log_file, int flush_opts) { - struct formatter f; - formatter_init (&f, ctx, NULL); + struct formatter f = formatter_make (ctx, NULL); buffer_line_write_time (&f, line, log_file, flush_opts); buffer_line_flush (line, &f, log_file, flush_opts); } @@ -3840,9 +3834,7 @@ buffer_line_write_to_log (struct app_context *ctx, if (line->flags & BUFFER_LINE_SKIP_FILE) return; - struct formatter f; - formatter_init (&f, ctx, NULL); - + struct formatter f = formatter_make (ctx, NULL); struct tm current; char buf[20]; if (!gmtime_r (&line->when, ¤t)) @@ -3922,8 +3914,7 @@ log_full (struct app_context *ctx, struct server *s, struct buffer *buffer, va_list ap; va_start (ap, format); - struct formatter f; - formatter_init (&f, ctx, s); + struct formatter f = formatter_make (ctx, s); formatter_addv (&f, format, &ap); log_formatter (ctx, buffer, flags, &f); @@ -4105,8 +4096,7 @@ buffer_remove (struct app_context *ctx, struct buffer *buffer) static void buffer_print_read_marker (struct app_context *ctx, FILE *stream, int flush_opts) { - struct formatter f; - formatter_init (&f, ctx, NULL); + struct formatter f = formatter_make (ctx, NULL); formatter_add (&f, "#a-- -- -- ---\n", ATTR_READ_MARKER); formatter_flush (&f, stream, flush_opts); // Flush the trailing formatting reset item @@ -6020,8 +6010,7 @@ irc_is_highlight (struct server *s, const char *message) // Strip formatting from the message so that it doesn't interfere // with nickname detection (color sequences in particular) - struct formatter f; - formatter_init (&f, s->ctx, NULL); + struct formatter f = formatter_make (s->ctx, NULL); formatter_parse_mirc (&f, message); struct str stripped = str_make (); @@ -6576,8 +6565,7 @@ irc_handle_kick (struct server *s, const struct irc_message *msg) if (buffer) { - struct formatter f; - formatter_init (&f, s->ctx, s); + struct formatter f = formatter_make (s->ctx, s); formatter_add (&f, "#a<--#r #N #a#s#r #n", ATTR_PART, msg->prefix, ATTR_PART, "has kicked", target); if (message) @@ -6844,8 +6832,7 @@ irc_handle_part (struct server *s, const struct irc_message *msg) if (buffer) { - struct formatter f; - formatter_init (&f, s->ctx, s); + struct formatter f = formatter_make (s->ctx, s); formatter_add (&f, "#a<--#r #N #a#s#r #S", ATTR_PART, msg->prefix, ATTR_PART, "has left", channel_name); if (message) @@ -6907,8 +6894,7 @@ irc_handle_ctcp_request (struct server *s, return; } - struct formatter f; - formatter_init (&f, s->ctx, s); + struct formatter f = formatter_make (s->ctx, s); formatter_add (&f, "CTCP requested by #n", msg->prefix); if (irc_is_channel (s, irc_skip_statusmsg (s, target))) formatter_add (&f, " (to #S)", target); @@ -7001,8 +6987,7 @@ static void log_quit (struct server *s, struct buffer *buffer, const char *prefix, const char *reason) { - struct formatter f; - formatter_init (&f, s->ctx, s); + struct formatter f = formatter_make (s->ctx, s); formatter_add (&f, "#a<--#r #N #a#s#r", ATTR_PART, prefix, ATTR_PART, "has quit"); if (reason) @@ -13868,8 +13853,7 @@ format_input_and_die (struct app_context *ctx) char buf[513]; while (fgets (buf, sizeof buf, stdin)) { - struct formatter f; - formatter_init (&f, ctx, NULL); + struct formatter f = formatter_make (ctx, NULL); formatter_add (&f, "#m", buf); formatter_flush (&f, stdout, FLUSH_OPT_NOWRAP); formatter_free (&f); diff --git a/kike.c b/kike.c index 166b541..2c79d57 100644 --- a/kike.c +++ b/kike.c @@ -1739,16 +1739,14 @@ struct mode_processor struct strv *output_params; ///< Similarly for "*_params" }; -static void -mode_processor_init (struct mode_processor *self) +static struct mode_processor +mode_processor_make (void) { - memset (self, 0, sizeof *self); - - self->added = str_make (); - self->removed = str_make (); - - self->added_params = strv_make (); - self->removed_params = strv_make (); + return (struct mode_processor) + { + .added = str_make (), .added_params = strv_make (), + .removed = str_make (), .removed_params = strv_make (), + }; } static void @@ -1990,9 +1988,7 @@ static void irc_handle_chan_mode_change (struct client *c, struct channel *chan, char *params[]) { - struct mode_processor p; - mode_processor_init (&p); - + struct mode_processor p = mode_processor_make (); p.params = params; p.channel = chan; p.c = 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);