update cfg_obj_attach/destroy

now that cfg_obj_destroy() has been simplified, we can use the
ISC_REFCOUNT macros to declare cfg_obj_attach() and _detach().
This commit is contained in:
Evan Hunt 2025-10-21 16:41:11 -07:00
parent ea03d743f7
commit 6de1d0dbc4
22 changed files with 66 additions and 93 deletions

View file

@ -745,7 +745,7 @@ main(int argc, char **argv) {
cleanup:
if (config != NULL) {
cfg_obj_destroy(&config);
cfg_obj_detach(&config);
}
if (parser != NULL) {

View file

@ -862,7 +862,7 @@ setup_dnsseckeys(dns_client_t *client, dns_view_t *toview) {
cleanup:
if (bindkeys != NULL) {
cfg_obj_destroy(&bindkeys);
cfg_obj_detach(&bindkeys);
}
if (parser != NULL) {
cfg_parser_destroy(&parser);

View file

@ -1119,7 +1119,7 @@ read_confkey(void) {
cleanup:
if (pctx != NULL) {
if (file != NULL) {
cfg_obj_destroy(&file);
cfg_obj_detach(&file);
}
cfg_parser_destroy(&pctx);
}

View file

@ -1199,7 +1199,7 @@ main(int argc, char **argv) {
}
dns_kasp_detach(&kasp);
cfg_obj_destroy(&config);
cfg_obj_detach(&config);
cfg_parser_destroy(&parser);
}
} else {

View file

@ -187,7 +187,7 @@ getkasp(ksr_ctx_t *ksr, dns_kasp_t **kasp) {
if (ISC_LIST_EMPTY(dns_kasp_keys(*kasp))) {
fatal("dnssec-policy '%s' has no keys configured", ksr->policy);
}
cfg_obj_destroy(&config);
cfg_obj_detach(&config);
cfg_parser_destroy(&parser);
}

View file

@ -467,7 +467,7 @@ named_config_parsefile(cfg_parser_t *parser, cfg_obj_t **conf) {
cleanup:
if (*conf) {
cfg_obj_destroy(conf);
cfg_obj_detach(conf);
}
out:

View file

@ -861,7 +861,7 @@ cleanup:
free_controlkey(keyid, mctx);
}
if (config != NULL) {
cfg_obj_destroy(&config);
cfg_obj_detach(&config);
}
if (pctx != NULL) {
cfg_parser_destroy(&pctx);

View file

@ -616,7 +616,7 @@ printversion(bool verbose) {
if (cfg_obj_isstring(obj)) {
printf(" geoip-directory: %s\n", cfg_obj_asstring(obj));
}
cfg_obj_destroy(&config);
cfg_obj_detach(&config);
isc_mem_detach(&geoip_mctx);
#endif /* HAVE_GEOIP2 */
}

View file

@ -2544,7 +2544,7 @@ cleanup:
dns_zone_detach(&zone);
}
if (zoneconf != NULL) {
cfg_obj_destroy(&zoneconf);
cfg_obj_detach(&zoneconf);
}
if (dnsforwarders != NULL) {
dns_forwarders_detach(&dnsforwarders);
@ -2785,7 +2785,7 @@ catz_reconfigure(dns_catz_entry_t *entry, void *arg1, void *arg2) {
cleanup:
if (zoneconf != NULL) {
cfg_obj_destroy(&zoneconf);
cfg_obj_detach(&zoneconf);
}
dns_zone_detach(&zone);
@ -7615,7 +7615,7 @@ data_to_cfg(dns_view_t *view, MDB_val *key, MDB_val *data, isc_buffer_t **text,
cleanup:
if (zoneconf != NULL) {
cfg_obj_destroy(&zoneconf);
cfg_obj_detach(&zoneconf);
}
return result;
@ -7696,14 +7696,14 @@ for_all_newzone_cfgs(newzone_cfg_cb_t callback, cfg_obj_t *config,
/*
* Destroy the configuration object created in this iteration.
*/
cfg_obj_destroy(&zconfigobj);
cfg_obj_detach(&zconfigobj);
}
if (text != NULL) {
isc_buffer_free(&text);
}
if (zconfigobj != NULL) {
cfg_obj_destroy(&zconfigobj);
cfg_obj_detach(&zconfigobj);
}
mdb_cursor_close(cursor);
@ -7841,7 +7841,7 @@ cleanup:
UNLOCK(&view->new_zone_lock);
if (zoneconf != NULL) {
cfg_obj_destroy(&zoneconf);
cfg_obj_detach(&zoneconf);
}
if (text != NULL) {
isc_buffer_free(&text);
@ -9342,10 +9342,10 @@ cleanup:
cfg_parser_destroy(&parser);
}
if (bindkeys != NULL) {
cfg_obj_destroy(&bindkeys);
cfg_obj_detach(&bindkeys);
}
if (config != NULL) {
cfg_obj_destroy(&config);
cfg_obj_detach(&config);
}
return result;
@ -9579,7 +9579,7 @@ shutdown_server(void *arg) {
cfg_aclconfctx_detach(&server->aclctx);
}
cfg_obj_destroy(&named_g_defaultconfig);
cfg_obj_detach(&named_g_defaultconfig);
(void)named_server_saventa(server);
@ -13014,7 +13014,7 @@ cleanup:
}
if (nzf_config != NULL) {
cfg_obj_destroy(&nzf_config);
cfg_obj_detach(&nzf_config);
}
return result;
@ -13145,7 +13145,7 @@ cleanup:
}
if (zoneconf != NULL) {
cfg_obj_destroy(&zoneconf);
cfg_obj_detach(&zoneconf);
}
if (view != NULL) {
dns_view_detach(&view);
@ -13189,7 +13189,7 @@ delete_zoneconf(dns_view_t *view, cfg_parser_t *pctx, const cfg_obj_t *config,
e = UNCONST(elt);
ISC_LIST_UNLINK(*list, e, link);
cfg_obj_destroy(&e->obj);
cfg_obj_detach(&e->obj);
isc_mem_put(pctx->mctx, e, sizeof(*e));
result = ISC_R_SUCCESS;
break;
@ -13715,7 +13715,7 @@ cleanup:
(void)putnull(text);
}
if (zoneconf != NULL) {
cfg_obj_destroy(&zoneconf);
cfg_obj_detach(&zoneconf);
}
if (view != NULL) {
dns_view_detach(&view);
@ -14214,7 +14214,7 @@ named_server_showzone(named_server_t *server, isc_lex_t *lex,
cleanup:
#ifdef HAVE_LMDB
if (nzconfig != NULL) {
cfg_obj_destroy(&nzconfig);
cfg_obj_detach(&nzconfig);
}
#endif /* HAVE_LMDB */
if (isc_buffer_usedlength(*text) > 0) {
@ -14233,13 +14233,13 @@ newzone_cfgctx_destroy(void **cfgp) {
cfg = *cfgp;
if (cfg->config != NULL) {
cfg_obj_destroy(&cfg->config);
cfg_obj_detach(&cfg->config);
}
if (cfg->vconfig != NULL) {
cfg_obj_destroy(&cfg->vconfig);
cfg_obj_detach(&cfg->vconfig);
}
if (cfg->nzf_config != NULL) {
cfg_obj_destroy(&cfg->nzf_config);
cfg_obj_detach(&cfg->nzf_config);
}
if (cfg->aclctx != NULL) {

View file

@ -604,7 +604,7 @@ read_sessionkey(isc_mem_t *mctx) {
cleanup:
if (pctx != NULL) {
if (sessionkey != NULL) {
cfg_obj_destroy(&sessionkey);
cfg_obj_detach(&sessionkey);
}
cfg_parser_destroy(&pctx);
}

View file

@ -300,7 +300,7 @@ parse_parameters(filter_instance_t *inst, const char *parameters,
cleanup:
if (param_obj != NULL) {
cfg_obj_destroy(&param_obj);
cfg_obj_detach(&param_obj);
}
if (parser != NULL) {
cfg_parser_destroy(&parser);
@ -383,7 +383,7 @@ plugin_check(const char *parameters, const void *cfg, const char *cfg_file,
cleanup:
if (param_obj != NULL) {
cfg_obj_destroy(&param_obj);
cfg_obj_detach(&param_obj);
}
if (parser != NULL) {
cfg_parser_destroy(&parser);

View file

@ -303,7 +303,7 @@ parse_parameters(filter_instance_t *inst, const char *parameters,
cleanup:
if (param_obj != NULL) {
cfg_obj_destroy(&param_obj);
cfg_obj_detach(&param_obj);
}
if (parser != NULL) {
cfg_parser_destroy(&parser);
@ -387,7 +387,7 @@ plugin_check(const char *parameters, const void *cfg, const char *cfg_file,
cleanup:
if (param_obj != NULL) {
cfg_obj_destroy(&param_obj);
cfg_obj_detach(&param_obj);
}
if (parser != NULL) {
cfg_parser_destroy(&parser);

View file

@ -593,7 +593,7 @@ synthrecord_parseconfig(synthrecord_t *inst, const char *parameters,
cleanup:
if (synthrecordcfg != NULL) {
cfg_obj_destroy(&synthrecordcfg);
cfg_obj_detach(&synthrecordcfg);
}
if (parser != NULL) {

View file

@ -999,7 +999,7 @@ main(int argc, char **argv) {
isccc_ccmsg_invalidate(&rndc_ccmsg);
cfg_obj_destroy(&config);
cfg_obj_detach(&config);
cfg_parser_destroy(&pctx);
isc_mem_put(isc_g_mctx, args, argslen);

View file

@ -217,7 +217,7 @@ cleanup:
}
if (syncplugincfg != NULL) {
cfg_obj_destroy(&syncplugincfg);
cfg_obj_detach(&syncplugincfg);
}
if (parser != NULL) {

View file

@ -146,7 +146,7 @@ main(int argc, char **argv) {
cfg_print(cfg, output, NULL);
cfg_obj_destroy(&cfg);
cfg_obj_detach(&cfg);
cfg_parser_destroy(&pctx);
}

View file

@ -550,23 +550,6 @@ cfg_obj_istype(const cfg_obj_t *obj, const cfg_type_t *type);
* Return true iff 'obj' is of type 'type'.
*/
void
cfg_obj_attach(cfg_obj_t *src, cfg_obj_t **dest);
/*%<
* Reference a configuration object.
*/
void
cfg_obj_destroy(cfg_obj_t **obj);
/*%<
* Delete a reference to a configuration object; destroy the object if
* there are no more references.
*
* Require:
* \li '*obj' is a valid cfg_obj_t.
* \li 'mctx' is a valid isc_mem_t.
*/
void
cfg_obj_log(const cfg_obj_t *obj, int level, const char *fmt, ...)
ISC_FORMAT_PRINTF(3, 4);
@ -633,3 +616,5 @@ cfg_pluginlist_foreach(const cfg_obj_t *config, const cfg_obj_t *list,
* 'list'
* \li first 'callback' return value which was not #ISC_R_SUCCESS otherwise
*/
ISC_REFCOUNT_DECL(cfg_obj);

View file

@ -42,10 +42,10 @@
} while (0)
/*% Clean up a configuration object if non-NULL. */
#define CLEANUP_OBJ(obj) \
do { \
if ((obj) != NULL) \
cfg_obj_destroy(&(obj)); \
#define CLEANUP_OBJ(obj) \
do { \
if ((obj) != NULL) \
cfg_obj_detach(&(obj)); \
} while (0)
/*%

View file

@ -88,10 +88,10 @@
} while (0)
/* Clean up a configuration object if non-NULL. */
#define CLEANUP_OBJ(obj) \
do { \
if ((obj) != NULL) \
cfg_obj_destroy(&(obj)); \
#define CLEANUP_OBJ(obj) \
do { \
if ((obj) != NULL) \
cfg_obj_detach(&(obj)); \
} while (0)
/* cfg_obj_t magic number */
@ -1973,7 +1973,7 @@ create_listelt(cfg_parser_t *pctx, cfg_listelt_t **eltp) {
static void
free_listelt(isc_mem_t *mctx, cfg_listelt_t *elt) {
if (elt->obj != NULL) {
cfg_obj_destroy(&elt->obj);
cfg_obj_detach(&elt->obj);
}
isc_mem_put(mctx, elt, sizeof(*elt));
}
@ -2305,7 +2305,7 @@ cfg_parse_mapbody(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
CHECK(parser_openfile(pctx, g.gl_pathv[i]));
}
cfg_obj_destroy(&includename);
cfg_obj_detach(&includename);
globfree(&g);
goto redo;
@ -2333,7 +2333,7 @@ cfg_parse_mapbody(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
*/
CHECK(cfg_parse_obj(pctx, &cfg_type_unsupported,
&eltobj));
cfg_obj_destroy(&eltobj);
cfg_obj_detach(&eltobj);
CHECK(parse_semicolon(pctx));
continue;
}
@ -3769,7 +3769,7 @@ map_symtabitem_destroy(char *key, unsigned int type, isc_symvalue_t symval,
UNUSED(type);
UNUSED(userarg);
cfg_obj_destroy(&obj);
cfg_obj_detach(&obj);
}
static isc_result_t
@ -3812,36 +3812,24 @@ cfg_obj_istype(const cfg_obj_t *obj, const cfg_type_t *type) {
/*
* Destroy 'obj', a configuration object created in 'pctx'.
*/
void
cfg_obj_destroy(cfg_obj_t **objp) {
REQUIRE(objp != NULL && *objp != NULL);
REQUIRE((*objp)->magic == CFGOBJ_MAGIC);
static void
cfg__obj_destroy(cfg_obj_t *obj) {
REQUIRE(obj != NULL);
REQUIRE(obj->magic == CFGOBJ_MAGIC);
cfg_obj_t *obj = *objp;
*objp = NULL;
obj->magic = 0;
if (isc_refcount_decrement(&obj->references) == 1) {
obj->magic = 0;
if (obj->file != NULL) {
cfg_obj_destroy(&obj->file);
}
obj->type->rep->free(obj);
isc_refcount_destroy(&obj->references);
isc_mem_putanddetach(&obj->mctx, obj, sizeof(cfg_obj_t));
if (obj->file != NULL) {
cfg_obj_detach(&obj->file);
}
obj->type->rep->free(obj);
isc_refcount_destroy(&obj->references);
isc_mem_putanddetach(&obj->mctx, obj, sizeof(cfg_obj_t));
}
void
cfg_obj_attach(cfg_obj_t *src, cfg_obj_t **dest) {
REQUIRE(src != NULL);
REQUIRE(dest != NULL && *dest == NULL);
isc_refcount_increment(&src->references);
*dest = src;
}
ISC_REFCOUNT_IMPL(cfg_obj, cfg__obj_destroy);
static void
free_noop(cfg_obj_t *obj) {

View file

@ -192,7 +192,7 @@ ISC_RUN_TEST_IMPL(duration) {
assert_int_equal(cmp, 0);
}
cfg_obj_destroy(&c1);
cfg_obj_detach(&c1);
cfg_parser_destroy(&p1);
}
}

View file

@ -144,7 +144,7 @@ test__query_source_print(const char *config, const char *expected) {
output_conf->type->print(&pctx, output_conf);
assert_text(expected);
cfg_obj_destroy(parser, &output_conf);
cfg_obj_detach(parser, &output_conf);
cfg_parser_reset(parser);
cfg_parser_destroy(&parser);
}

View file

@ -102,7 +102,7 @@ ISC_RUN_TEST_IMPL(addzoneconf) {
strlcat(buf, ";", sizeof(buf));
assert_string_equal(tests[i], buf);
cfg_obj_destroy(&conf);
cfg_obj_detach(&conf);
cfg_parser_reset(p);
}
@ -141,8 +141,8 @@ ISC_RUN_TEST_IMPL(parse_buffer) {
assert_int_equal(result, ISC_R_SUCCESS);
assert_int_equal(p2->line, 104);
cfg_obj_destroy(&c1);
cfg_obj_destroy(&c2);
cfg_obj_detach(&c1);
cfg_obj_detach(&c2);
cfg_parser_destroy(&p1);
cfg_parser_destroy(&p2);