Use non-copying list_append_this_dict

This commit is contained in:
Willem Toorop 2016-03-21 14:56:09 +01:00
parent ba2da45819
commit 90beaaff1d
4 changed files with 43 additions and 38 deletions

View File

@ -320,7 +320,8 @@ local_host_cmp(const void *id1, const void *id2)
return canonical_dname_compare(id1, id2); return canonical_dname_compare(id1, id2);
} }
static void /** return 0 on success */
static int
add_local_host(getdns_context *context, getdns_dict *address, const char *str) add_local_host(getdns_context *context, getdns_dict *address, const char *str)
{ {
uint8_t host_name[256]; uint8_t host_name[256];
@ -331,7 +332,7 @@ add_local_host(getdns_context *context, getdns_dict *address, const char *str)
getdns_list **addrs; getdns_list **addrs;
if (gldns_str2wire_dname_buf(str, host_name, &host_name_len)) if (gldns_str2wire_dname_buf(str, host_name, &host_name_len))
return; return -1;
canonicalize_dname(host_name); canonicalize_dname(host_name);
@ -340,7 +341,7 @@ add_local_host(getdns_context *context, getdns_dict *address, const char *str)
if (!(hnas = (host_name_addrs *)GETDNS_XMALLOC(context->mf, if (!(hnas = (host_name_addrs *)GETDNS_XMALLOC(context->mf,
uint8_t, sizeof(host_name_addrs) + host_name_len))) uint8_t, sizeof(host_name_addrs) + host_name_len)))
return; return -1;
hnas->ipv4addrs = NULL; hnas->ipv4addrs = NULL;
hnas->ipv6addrs = NULL; hnas->ipv6addrs = NULL;
@ -358,18 +359,23 @@ add_local_host(getdns_context *context, getdns_dict *address, const char *str)
: address_type->data[3] == '6'? &hnas->ipv4addrs : NULL)) { : address_type->data[3] == '6'? &hnas->ipv4addrs : NULL)) {
if (!hnas_found) GETDNS_FREE(context->mf, hnas); if (!hnas_found) GETDNS_FREE(context->mf, hnas);
return; return -1;
} }
if (!*addrs && !(*addrs = getdns_list_create_with_context(context))) { if (!*addrs && !(*addrs = getdns_list_create_with_context(context))) {
if (!hnas_found) GETDNS_FREE(context->mf, hnas); if (!hnas_found) GETDNS_FREE(context->mf, hnas);
return; return -1;
} }
if (_getdns_list_append_dict(*addrs, address) && !hnas_found) { if (_getdns_list_append_this_dict(*addrs, address)) {
if (!hnas_found) {
getdns_list_destroy(*addrs); getdns_list_destroy(*addrs);
GETDNS_FREE(context->mf, hnas); GETDNS_FREE(context->mf, hnas);
}
return -1;
} else if (!hnas_found) } else if (!hnas_found)
(void)_getdns_rbtree_insert(&context->local_hosts, &hnas->node); (void)_getdns_rbtree_insert(&context->local_hosts, &hnas->node);
return 0;
} }
static getdns_dict * static getdns_dict *
@ -515,8 +521,8 @@ create_local_hosts(getdns_context *context)
str_addr_dict(context, start_of_word))) str_addr_dict(context, start_of_word)))
/* Unparseable address */ /* Unparseable address */
break; /* skip to next line */ break; /* skip to next line */
} else } else if (!add_local_host(context, address, start_of_word))
add_local_host(context, address, start_of_word); address = NULL;
start_of_word = NULL; start_of_word = NULL;
*pos = prev_c; *pos = prev_c;
@ -540,7 +546,8 @@ read_more: ;
if (address) { if (address) {
/* One last name for this address? */ /* One last name for this address? */
if (start_of_word && !start_of_line) if (start_of_word && !start_of_line)
add_local_host(context, address, start_of_word); if (!add_local_host(context, address, start_of_word))
address = NULL;
getdns_dict_destroy(address); getdns_dict_destroy(address);
} }
} }
@ -3634,7 +3641,8 @@ getdns_context_get_upstream_recursive_servers(getdns_context *context,
} }
} }
if (!r) if (!r)
r = _getdns_list_append_dict(upstreams, d); if (!(r = _getdns_list_append_this_dict(upstreams, d)))
d = NULL;
getdns_dict_destroy(d); getdns_dict_destroy(d);
} }
if (r) if (r)

View File

@ -567,7 +567,7 @@ _getdns_fp2rr_list(struct mem_funcs *mf,
continue; continue;
if ((r = _getdns_wire2rr_dict(mf, rr, len, &rr_dict))) if ((r = _getdns_wire2rr_dict(mf, rr, len, &rr_dict)))
break; break;
r = _getdns_list_append_dict(rrs, rr_dict); if ((r = _getdns_list_append_this_dict(rrs, rr_dict)))
getdns_dict_destroy(rr_dict); getdns_dict_destroy(rr_dict);
} }
if (rr) if (rr)
@ -659,10 +659,9 @@ _getdns_wire2msg_dict_scan(struct mem_funcs *mf,
goto error; goto error;
break; break;
default: default:
if ((r = _getdns_list_append_dict( if ((r = _getdns_list_append_this_dict(
sections[section], rr_dict))) sections[section], rr_dict)))
goto error; goto error;
getdns_dict_destroy(rr_dict);
break; break;
} }
rr_dict = NULL; rr_dict = NULL;

View File

@ -3079,7 +3079,7 @@ static void append_rrs2val_chain_list(getdns_context *ctxt,
&ctxt->mf, &rr->rr_i))) &ctxt->mf, &rr->rr_i)))
continue; continue;
(void)_getdns_list_append_dict(val_chain_list, rr_dict); if (_getdns_list_append_this_dict(val_chain_list, rr_dict))
getdns_dict_destroy(rr_dict); getdns_dict_destroy(rr_dict);
} }
for ( rrsig = rrsig_iter_init(&rrsig_spc, rrset) for ( rrsig = rrsig_iter_init(&rrsig_spc, rrset)
@ -3098,7 +3098,7 @@ static void append_rrs2val_chain_list(getdns_context *ctxt,
&ctxt->mf, &rrsig->rr_i))) &ctxt->mf, &rrsig->rr_i)))
continue; continue;
(void)_getdns_list_append_dict(val_chain_list, rr_dict); if (_getdns_list_append_this_dict(val_chain_list, rr_dict))
getdns_dict_destroy(rr_dict); getdns_dict_destroy(rr_dict);
} }
} }
@ -3130,7 +3130,7 @@ static void append_empty_ds2val_chain_list(
(void) getdns_dict_set_bindata(rdata_dict, "rdata_raw", &bindata); (void) getdns_dict_set_bindata(rdata_dict, "rdata_raw", &bindata);
getdns_dict_destroy(rdata_dict); getdns_dict_destroy(rdata_dict);
(void)_getdns_list_append_dict(val_chain_list, rr_dict); if (_getdns_list_append_this_dict(val_chain_list, rr_dict))
getdns_dict_destroy(rr_dict); getdns_dict_destroy(rr_dict);
} }

View File

@ -329,11 +329,10 @@ _getdns_rr_iter2rr_dict(struct mem_funcs *mf, _getdns_rr_iter *i)
_getdns_list_create_with_mf(mf))) _getdns_list_create_with_mf(mf)))
goto rdata_error; goto rdata_error;
if (_getdns_list_append_dict( if (_getdns_list_append_this_dict(
repeat_list, repeat_dict)) repeat_list, repeat_dict))
goto rdata_error; goto rdata_error;
getdns_dict_destroy(repeat_dict);
repeat_dict = NULL; repeat_dict = NULL;
} }
if (!(repeat_dict = if (!(repeat_dict =
@ -364,9 +363,8 @@ _getdns_rr_iter2rr_dict(struct mem_funcs *mf, _getdns_rr_iter *i)
if (!repeat_list && !(repeat_list = if (!repeat_list && !(repeat_list =
_getdns_list_create_with_mf(mf))) _getdns_list_create_with_mf(mf)))
goto rdata_error; goto rdata_error;
if (_getdns_list_append_dict(repeat_list, repeat_dict)) if (_getdns_list_append_this_dict(repeat_list, repeat_dict))
goto rdata_error; goto rdata_error;
getdns_dict_destroy(repeat_dict);
repeat_dict = NULL; repeat_dict = NULL;
} }
if (repeat_list) { if (repeat_list) {
@ -549,13 +547,12 @@ _getdns_create_reply_dict(getdns_context *context, getdns_network_req *req,
if (_getdns_dict_set_this_dict(result, "question", rr_dict)) if (_getdns_dict_set_this_dict(result, "question", rr_dict))
goto error; goto error;
else rr_dict = NULL;
rr_dict = NULL;
continue; continue;
} }
if (_getdns_list_append_dict(sections[section], rr_dict)) if (_getdns_list_append_this_dict(sections[section], rr_dict))
goto error; goto error;
else rr_dict = NULL;
rr_type = gldns_read_uint16(rr_iter->rr_type); rr_type = gldns_read_uint16(rr_iter->rr_type);
if (section > GLDNS_SECTION_QUESTION && if (section > GLDNS_SECTION_QUESTION &&
@ -589,6 +586,9 @@ _getdns_create_reply_dict(getdns_context *context, getdns_network_req *req,
&rdf_iter_storage, rr_iter))) &rdf_iter_storage, rr_iter)))
continue; continue;
if (!just_addrs)
continue;
bin_size = rdf_iter->nxt - rdf_iter->pos; bin_size = rdf_iter->nxt - rdf_iter->pos;
bin_data = rdf_iter->pos; bin_data = rdf_iter->pos;
if (!set_dict(&rr_dict, getdns_dict_create_with_context(context)) || if (!set_dict(&rr_dict, getdns_dict_create_with_context(context)) ||
@ -599,10 +599,11 @@ _getdns_create_reply_dict(getdns_context *context, getdns_network_req *req,
_getdns_dict_set_const_bindata( _getdns_dict_set_const_bindata(
rr_dict, "address_data", bin_size, bin_data) || rr_dict, "address_data", bin_size, bin_data) ||
(just_addrs && _getdns_list_append_dict(just_addrs, rr_dict))) { _getdns_list_append_this_dict(just_addrs, rr_dict)) {
goto error; goto error;
} }
rr_dict = NULL;
} }
if (!_getdns_dict_set_this_list(result, "answer", if (!_getdns_dict_set_this_list(result, "answer",
sections[GLDNS_SECTION_ANSWER])) sections[GLDNS_SECTION_ANSWER]))
@ -951,7 +952,7 @@ _getdns_create_getdns_response(getdns_dns_req *completed_request)
netreq->tsig_status)) netreq->tsig_status))
goto error; goto error;
} }
if (_getdns_list_append_dict(replies_tree, reply)) { if (_getdns_list_append_this_dict(replies_tree, reply)) {
getdns_dict_destroy(reply); getdns_dict_destroy(reply);
goto error; goto error;
} }
@ -961,17 +962,14 @@ _getdns_create_getdns_response(getdns_dns_req *completed_request)
_getdns_create_call_reporting_dict(context,netreq))) _getdns_create_call_reporting_dict(context,netreq)))
goto error; goto error;
if (_getdns_list_append_dict( if (_getdns_list_append_this_dict(
call_reporting, netreq_debug)) { call_reporting, netreq_debug)) {
getdns_dict_destroy(netreq_debug); getdns_dict_destroy(netreq_debug);
goto error; goto error;
} }
getdns_dict_destroy(netreq_debug);
} }
getdns_dict_destroy(reply);
if (_getdns_list_append_const_bindata(replies_full, if (_getdns_list_append_const_bindata(replies_full,
netreq->response_len, netreq->response)) netreq->response_len, netreq->response))
goto error; goto error;
@ -1293,7 +1291,7 @@ void _getdns_wire2list(uint8_t *pkt, size_t pkt_len, getdns_list *l)
if (!(rr_dict = _getdns_rr_iter2rr_dict(&l->mf, rr))) if (!(rr_dict = _getdns_rr_iter2rr_dict(&l->mf, rr)))
continue; continue;
(void)_getdns_list_append_dict(l, rr_dict); if (_getdns_list_append_this_dict(l, rr_dict))
getdns_dict_destroy(rr_dict); getdns_dict_destroy(rr_dict);
} }
} }