degesch: refcountify "struct {buffer,server}"

This commit is contained in:
Přemysl Eric Janouch 2015-11-18 23:49:09 +01:00
parent b952fc1f6d
commit 5d55d7f6de
1 changed files with 19 additions and 17 deletions

View File

@ -1045,6 +1045,7 @@ enum buffer_type
struct buffer struct buffer
{ {
LIST_HEADER (struct buffer) LIST_HEADER (struct buffer)
REF_COUNTABLE_HEADER
enum buffer_type type; ///< Type of the buffer enum buffer_type type; ///< Type of the buffer
char *name; ///< The name of the buffer char *name; ///< The name of the buffer
@ -1074,6 +1075,7 @@ static struct buffer *
buffer_new (void) buffer_new (void)
{ {
struct buffer *self = xcalloc (1, sizeof *self); struct buffer *self = xcalloc (1, sizeof *self);
self->ref_count = 1;
self->input_data = input_buffer_new (); self->input_data = input_buffer_new ();
return self; return self;
} }
@ -1095,6 +1097,9 @@ buffer_destroy (struct buffer *self)
free (self); free (self);
} }
REF_COUNTABLE_METHODS (buffer)
#define buffer_ref do_not_use_dangerous
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
enum transport_io_result enum transport_io_result
@ -1143,6 +1148,7 @@ typedef size_t (*irc_strxfrm_fn) (char *, const char *, size_t);
struct server struct server
{ {
REF_COUNTABLE_HEADER
struct app_context *ctx; ///< Application context struct app_context *ctx; ///< Application context
char *name; ///< Server identifier char *name; ///< Server identifier
@ -1254,10 +1260,11 @@ server_free_specifics (struct server *self)
free (self->irc_chanuser_modes); free (self->irc_chanuser_modes);
} }
static void static struct server *
server_init (struct server *self, struct poller *poller) server_new (struct poller *poller)
{ {
memset (self, 0, sizeof *self); struct server *self = xcalloc (1, sizeof *self);
self->ref_count = 1;
self->socket = -1; self->socket = -1;
str_init (&self->read_buffer); str_init (&self->read_buffer);
@ -1289,12 +1296,12 @@ server_init (struct server *self, struct poller *poller)
str_init (&self->irc_user_mode); str_init (&self->irc_user_mode);
server_free_specifics (self);
server_init_specifics (self); server_init_specifics (self);
return self;
} }
static void static void
server_free (struct server *self) server_destroy (struct server *self)
{ {
free (self->name); free (self->name);
@ -1337,15 +1344,12 @@ server_free (struct server *self)
free (self->irc_user_host); free (self->irc_user_host);
server_free_specifics (self); server_free_specifics (self);
}
static void
server_destroy (void *self)
{
server_free (self);
free (self); free (self);
} }
REF_COUNTABLE_METHODS (server)
#define server_ref do_not_use_dangerous
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
struct app_context struct app_context
@ -1441,7 +1445,7 @@ app_context_init (struct app_context *self)
poller_init (&self->poller); poller_init (&self->poller);
str_map_init (&self->servers); 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; self->servers.key_xfrm = tolower_ascii_strxfrm;
str_map_init (&self->buffers_by_name); 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); input_destroy_buffer (&self->input, iter->input_data);
iter->input_data = NULL; iter->input_data = NULL;
#endif // HAVE_READLINE #endif // HAVE_READLINE
buffer_destroy (iter); buffer_unref (iter);
} }
str_map_free (&self->buffers_by_name); 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); str_map_set (&ctx->buffers_by_name, buffer->name, NULL);
LIST_UNLINK_WITH_TAIL (ctx->buffers, ctx->buffers_tail, buffer); LIST_UNLINK_WITH_TAIL (ctx->buffers, ctx->buffers_tail, buffer);
buffer_destroy (buffer); buffer_unref (buffer);
refresh_prompt (ctx); refresh_prompt (ctx);
} }
@ -6980,9 +6984,7 @@ server_add (struct app_context *ctx,
{ {
hard_assert (!str_map_find (&ctx->servers, name)); hard_assert (!str_map_find (&ctx->servers, name));
struct server *s = xmalloc (sizeof *s); struct server *s = server_new (&ctx->poller);
server_init (s, &ctx->poller);
s->ctx = ctx; s->ctx = ctx;
s->name = xstrdup (name); s->name = xstrdup (name);
str_map_set (&ctx->servers, s->name, s); str_map_set (&ctx->servers, s->name, s);