From: Stephan Bosch Date: Sun, 21 Jan 2024 16:46:07 +0000 (+0100) Subject: lmtp: lmtp-client - Add reference counting X-Git-Tag: 2.4.0~1823 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=da04de9feeff62e25a8872eb9443c46503ee71b1;p=thirdparty%2Fdovecot%2Fcore.git lmtp: lmtp-client - Add reference counting --- diff --git a/src/lmtp/lmtp-client.c b/src/lmtp/lmtp-client.c index 60fec8dbb4..3ac84737b3 100644 --- a/src/lmtp/lmtp-client.c +++ b/src/lmtp/lmtp-client.c @@ -147,6 +147,7 @@ struct client *client_create(int fd_in, int fd_out, pool = pool_alloconly_create("lmtp client", 2048); client = p_new(pool, struct client, 1); + client->refcount = 1; client->pool = pool; client->v = lmtp_client_vfuncs; client->remote_ip = conn->remote_ip; @@ -240,6 +241,27 @@ void client_state_reset(struct client *client) p_clear(client->state_pool); } +void client_ref(struct client *client) +{ + i_assert(client->refcount > 0); + client->refcount++; +} + +void client_unref(struct client **_client) +{ + struct client *client = *_client; + + if (client == NULL) + return; + *_client = NULL; + + i_assert(client->refcount > 0); + if (--client->refcount > 0) + return; + + client->v.destroy(client); +} + void client_destroy(struct client **_client, const char *enh_code, const char *reason) { @@ -383,7 +405,7 @@ static void client_connection_free(void *context) { struct client *client = context; - client->v.destroy(client); + client_unref(&client); } static bool client_connection_is_trusted(void *context) diff --git a/src/lmtp/lmtp-client.h b/src/lmtp/lmtp-client.h index 00ed96dda6..3bec871abd 100644 --- a/src/lmtp/lmtp-client.h +++ b/src/lmtp/lmtp-client.h @@ -67,6 +67,7 @@ struct lmtp_client_vfuncs { struct client { struct client *prev, *next; + int refcount; pool_t pool; struct lmtp_client_vfuncs v; @@ -112,6 +113,8 @@ extern struct lmtp_module_register lmtp_module_register; struct client *client_create(int fd_in, int fd_out, const struct master_service_connection *conn); +void client_ref(struct client *client); +void client_unref(struct client **_client); void client_destroy(struct client **client, const char *enh_code, const char *reason) ATTR_NULL(2, 3);