From: Timo Sirainen Date: Fri, 2 Dec 2022 12:42:27 +0000 (+0200) Subject: lib-settings: Add refcounting with settings_parser_[un]ref() X-Git-Tag: 2.4.0~3321 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=015882d3ec702cab8f3ac9e3ae122661d64083dd;p=thirdparty%2Fdovecot%2Fcore.git lib-settings: Add refcounting with settings_parser_[un]ref() --- diff --git a/src/lib-settings/settings-parser.c b/src/lib-settings/settings-parser.c index e8f4c275af..b2e199b16c 100644 --- a/src/lib-settings/settings-parser.c +++ b/src/lib-settings/settings-parser.c @@ -42,6 +42,7 @@ struct setting_link { struct setting_parser_context { pool_t set_pool, parser_pool; + int refcount; enum settings_parser_flags flags; bool str_vars_are_expanded; @@ -204,6 +205,7 @@ settings_parser_init_list(pool_t set_pool, parser_pool = pool_alloconly_create(MEMPOOL_GROWING"settings parser", 1024); ctx = p_new(parser_pool, struct setting_parser_context, 1); + ctx->refcount = 1; ctx->set_pool = set_pool; ctx->parser_pool = parser_pool; ctx->flags = flags; @@ -235,16 +237,31 @@ settings_parser_init_list(pool_t set_pool, return ctx; } -void settings_parser_deinit(struct setting_parser_context **_ctx) +void settings_parser_ref(struct setting_parser_context *ctx) +{ + i_assert(ctx->refcount > 0); + ctx->refcount++; +} + +void settings_parser_unref(struct setting_parser_context **_ctx) { struct setting_parser_context *ctx = *_ctx; *_ctx = NULL; + + i_assert(ctx->refcount > 0); + if (--ctx->refcount > 0) + return; hash_table_destroy(&ctx->links); pool_unref(&ctx->set_pool); pool_unref(&ctx->parser_pool); } +void settings_parser_deinit(struct setting_parser_context **ctx) +{ + settings_parser_unref(ctx); +} + void *settings_parser_get(struct setting_parser_context *ctx) { i_assert(ctx->root_count == 1); @@ -1511,6 +1528,7 @@ settings_parser_dup(const struct setting_parser_context *old_ctx, parser_pool = pool_alloconly_create(MEMPOOL_GROWING"dup settings parser", 1024); new_ctx = p_new(parser_pool, struct setting_parser_context, 1); + new_ctx->refcount = 1; new_ctx->set_pool = new_pool; new_ctx->parser_pool = parser_pool; new_ctx->flags = old_ctx->flags; diff --git a/src/lib-settings/settings-parser.h b/src/lib-settings/settings-parser.h index 5317448f58..4f0895ee75 100644 --- a/src/lib-settings/settings-parser.h +++ b/src/lib-settings/settings-parser.h @@ -132,6 +132,8 @@ struct setting_parser_context * settings_parser_init_list(pool_t set_pool, const struct setting_parser_info *const *roots, unsigned int count, enum settings_parser_flags flags); +void settings_parser_ref(struct setting_parser_context *ctx); +void settings_parser_unref(struct setting_parser_context **ctx); void settings_parser_deinit(struct setting_parser_context **ctx); /* Return pointer to root setting structure. */