From 5d55d7f6deccfaa80fb2b33e6ac65d06cfe7148e Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Wed, 18 Nov 2015 23:49:09 +0100
Subject: degesch: refcountify "struct {buffer,server}"
---
degesch.c | 36 +++++++++++++++++++-----------------
1 file changed, 19 insertions(+), 17 deletions(-)
diff --git a/degesch.c b/degesch.c
index ce7563b..f0700f2 100644
--- a/degesch.c
+++ b/degesch.c
@@ -1045,6 +1045,7 @@ enum buffer_type
struct buffer
{
LIST_HEADER (struct buffer)
+ REF_COUNTABLE_HEADER
enum buffer_type type; ///< Type of the buffer
char *name; ///< The name of the buffer
@@ -1074,6 +1075,7 @@ static struct buffer *
buffer_new (void)
{
struct buffer *self = xcalloc (1, sizeof *self);
+ self->ref_count = 1;
self->input_data = input_buffer_new ();
return self;
}
@@ -1095,6 +1097,9 @@ buffer_destroy (struct buffer *self)
free (self);
}
+REF_COUNTABLE_METHODS (buffer)
+#define buffer_ref do_not_use_dangerous
+
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
enum transport_io_result
@@ -1143,6 +1148,7 @@ typedef size_t (*irc_strxfrm_fn) (char *, const char *, size_t);
struct server
{
+ REF_COUNTABLE_HEADER
struct app_context *ctx; ///< Application context
char *name; ///< Server identifier
@@ -1254,10 +1260,11 @@ server_free_specifics (struct server *self)
free (self->irc_chanuser_modes);
}
-static void
-server_init (struct server *self, struct poller *poller)
+static struct server *
+server_new (struct poller *poller)
{
- memset (self, 0, sizeof *self);
+ struct server *self = xcalloc (1, sizeof *self);
+ self->ref_count = 1;
self->socket = -1;
str_init (&self->read_buffer);
@@ -1289,12 +1296,12 @@ server_init (struct server *self, struct poller *poller)
str_init (&self->irc_user_mode);
- server_free_specifics (self);
server_init_specifics (self);
+ return self;
}
static void
-server_free (struct server *self)
+server_destroy (struct server *self)
{
free (self->name);
@@ -1337,15 +1344,12 @@ server_free (struct server *self)
free (self->irc_user_host);
server_free_specifics (self);
-}
-
-static void
-server_destroy (void *self)
-{
- server_free (self);
free (self);
}
+REF_COUNTABLE_METHODS (server)
+#define server_ref do_not_use_dangerous
+
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
struct app_context
@@ -1441,7 +1445,7 @@ app_context_init (struct app_context *self)
poller_init (&self->poller);
str_map_init (&self->servers);
- self->servers.free = server_destroy;
+ self->servers.free = (str_map_free_fn) server_unref;
self->servers.key_xfrm = tolower_ascii_strxfrm;
str_map_init (&self->buffers_by_name);
@@ -1491,7 +1495,7 @@ app_context_free (struct app_context *self)
input_destroy_buffer (&self->input, iter->input_data);
iter->input_data = NULL;
#endif // HAVE_READLINE
- buffer_destroy (iter);
+ buffer_unref (iter);
}
str_map_free (&self->buffers_by_name);
@@ -3174,7 +3178,7 @@ buffer_remove (struct app_context *ctx, struct buffer *buffer)
str_map_set (&ctx->buffers_by_name, buffer->name, NULL);
LIST_UNLINK_WITH_TAIL (ctx->buffers, ctx->buffers_tail, buffer);
- buffer_destroy (buffer);
+ buffer_unref (buffer);
refresh_prompt (ctx);
}
@@ -6980,9 +6984,7 @@ server_add (struct app_context *ctx,
{
hard_assert (!str_map_find (&ctx->servers, name));
- struct server *s = xmalloc (sizeof *s);
- server_init (s, &ctx->poller);
-
+ struct server *s = server_new (&ctx->poller);
s->ctx = ctx;
s->name = xstrdup (name);
str_map_set (&ctx->servers, s->name, s);
--
cgit v1.2.3-70-g09d2