+void *adns__alloc_preserved(adns_query qu, size_t sz) {
+ void *rv;
+
+ sz= MEM_ROUND(sz);
+ rv= adns__alloc_interim(qu,sz);
+ if (!rv) return 0;
+ qu->preserved_allocd += sz;
+ return rv;
+}
+
+static allocnode *alloc_info(adns_query qu, void *p, size_t *sz_r) {
+ allocnode *an;
+
+ if (!p || p == qu) { *sz_r= 0; return 0; }
+ an= (allocnode *)((byte *)p - MEM_ROUND(sizeof(allocnode)));
+ *sz_r= MEM_ROUND(an->sz);
+ return an;
+}
+
+void adns__free_interim(adns_query qu, void *p) {
+ size_t sz;
+ allocnode *an= alloc_info(qu, p, &sz);
+
+ if (!an) return;
+ assert(!qu->final_allocspace);
+ LIST_UNLINK(qu->allocations, an);
+ free(an);
+ qu->interim_allocd -= sz;
+}
+
+void *adns__alloc_mine(adns_query qu, size_t sz) {
+ return alloc_common(qu,MEM_ROUND(sz));
+}
+
+void adns__transfer_interim(adns_query from, adns_query to, void *block) {
+ size_t sz;
+ allocnode *an= alloc_info(from, block, &sz);
+
+ if (!an) return;
+
+ assert(!to->final_allocspace);
+ assert(!from->final_allocspace);
+
+ LIST_UNLINK(from->allocations,an);
+ LIST_LINK_TAIL(to->allocations,an);
+
+ from->interim_allocd -= sz;
+ to->interim_allocd += sz;
+
+ if (to->expires > from->expires) to->expires= from->expires;
+}
+
+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__returning(adns_state ads, adns_query qu_for_caller) {
+ 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);
+ }
+ adns__consistency(ads,qu_for_caller,cc_entex);
+}
+
+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);