+void *adns__alloc_final(adns_query qu, size_t sz) {
+ /* When we're in the _final stage, we _subtract_ from interim_alloc'd
+ * each allocation, and use final_allocspace to point to the next free
+ * bit.
+ */
+ void *rp;
+
+ sz= MEM_ROUND(sz);
+ rp= qu->final_allocspace;
+ assert(rp);
+ qu->interim_allocd -= sz;
+ assert(qu->interim_allocd>=0);
+ qu->final_allocspace= (byte*)rp + sz;
+ return rp;
+}
+
+void adns__cancel_children(adns_query qu) {
+ adns_query cqu, ncqu;
+
+ for (cqu= qu->children.head; cqu; cqu= ncqu) {
+ ncqu= cqu->siblings.next;
+ adns__cancel(cqu);
+ }
+}
+
+void adns__reset_preserved(adns_query qu) {
+ assert(!qu->final_allocspace);
+ adns__cancel_children(qu);
+ qu->answer->nrrs= 0;
+ qu->answer->rrs.untyped= 0;
+ qu->interim_allocd= qu->preserved_allocd;
+}
+
+static void free_query_allocs(adns_query qu) {
+ allocnode *an, *ann;
+
+ adns__cancel_children(qu);
+ for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
+ LIST_INIT(qu->allocations);
+ adns__vbuf_free(&qu->vb);
+ adns__vbuf_free(&qu->search_vb);
+ free(qu->query_dgram);
+ qu->query_dgram= 0;
+}
+
+void adns__intdone_process(adns_state ads) {
+ while (ads->intdone.head) {
+ adns_query iq= ads->intdone.head;
+ adns_query parent= iq->parent;
+ LIST_UNLINK_PART(parent->children,iq,siblings.);
+ LIST_UNLINK(iq->ads->childw,parent);
+ LIST_UNLINK(ads->intdone,iq);
+ iq->ctx.callback(parent,iq);
+ free_query_allocs(iq);
+ free(iq->answer);
+ free(iq);
+ }
+}
+
+void adns__returning(adns_state ads, adns_query qu_for_caller) {
+ adns__intdone_process(ads);
+ adns__consistency(ads,qu_for_caller,cc_exit);
+}
+
+void adns__cancel(adns_query qu) {
+ adns_state ads;
+
+ ads= qu->ads;
+ adns__consistency(ads,qu,cc_freq);
+ if (qu->parent) LIST_UNLINK_PART(qu->parent->children,qu,siblings.);
+ switch (qu->state) {
+ case query_tosend:
+ LIST_UNLINK(ads->udpw,qu);
+ break;
+ case query_tcpw:
+ LIST_UNLINK(ads->tcpw,qu);
+ break;
+ case query_childw:
+ LIST_UNLINK(ads->childw,qu);
+ break;
+ case query_done:
+ if (qu->parent)
+ LIST_UNLINK(ads->intdone,qu);
+ else
+ LIST_UNLINK(ads->output,qu);
+ break;
+ default:
+ abort();
+ }
+ free_query_allocs(qu);
+ free(qu->answer);
+ free(qu);
+}
+
+void adns_cancel(adns_query qu) {
+ adns_state ads;
+
+ assert(!qu->parent);
+ ads= qu->ads;
+ adns__consistency(ads,qu,cc_enter);
+ adns__cancel(qu);
+ adns__returning(ads,0);
+}
+
+void adns__update_expires(adns_query qu, unsigned long ttl,
+ struct timeval now) {
+ time_t max;
+
+ assert(ttl <= MAXTTLBELIEVE);
+ max= now.tv_sec + ttl;
+ if (qu->expires < max) return;
+ qu->expires= max;
+}
+
+static void makefinal_query(adns_query qu) {