Add support for deleting per-network DNS resolver caches.
This is required when the possible range of NetIds is large to
prevent netd consuming excessive amounts of memory.
This required replacing the per-cache locks in favor of a single
global lock to prevent accesses to deleted caches.

Change-Id: I99d058bafea5de743e56075dbed74031da4df63f
diff --git a/libc/dns/resolv/res_send.c b/libc/dns/resolv/res_send.c
index 9b36f55..972e143 100644
--- a/libc/dns/resolv/res_send.c
+++ b/libc/dns/resolv/res_send.c
@@ -367,7 +367,6 @@
 	int gotsomewhere, terrno, try, v_circuit, resplen, ns, n;
 	char abuf[NI_MAXHOST];
 #if USE_RESOLV_CACHE
-        struct resolv_cache*  cache;
         ResolvCacheStatus     cache_status = RESOLV_CACHE_UNSUPPORTED;
 #endif
 
@@ -389,21 +388,17 @@
 	terrno = ETIMEDOUT;
 
 #if USE_RESOLV_CACHE
-	// get the cache associated with the network
-	cache = __get_res_cache(statp->netid);
-	if (cache != NULL) {
-		int  anslen = 0;
-		cache_status = _resolv_cache_lookup(
-				cache, buf, buflen,
-				ans, anssiz, &anslen);
+	int  anslen = 0;
+	cache_status = _resolv_cache_lookup(
+			statp->netid, buf, buflen,
+			ans, anssiz, &anslen);
 
-		if (cache_status == RESOLV_CACHE_FOUND) {
-			return anslen;
-		} else {
-			// had a cache miss for a known network, so populate the thread private
-			// data so the normal resolve path can do its thing
-			_resolv_populate_res_for_net(statp);
-		}
+	if (cache_status == RESOLV_CACHE_FOUND) {
+		return anslen;
+	} else if (cache_status != RESOLV_CACHE_UNSUPPORTED) {
+		// had a cache miss for a known network, so populate the thread private
+		// data so the normal resolve path can do its thing
+		_resolv_populate_res_for_net(statp);
 	}
 
 	if (statp->nscount == 0) {
@@ -602,7 +597,7 @@
 
 #if USE_RESOLV_CACHE
                 if (cache_status == RESOLV_CACHE_NOTFOUND) {
-                    _resolv_cache_add(cache, buf, buflen,
+                    _resolv_cache_add(statp->netid, buf, buflen,
                                       ans, resplen);
                 }
 #endif
@@ -658,13 +653,13 @@
 		errno = terrno;
 
 #if USE_RESOLV_CACHE
-        _resolv_cache_query_failed(cache, buf, buflen);
+        _resolv_cache_query_failed(statp->netid, buf, buflen);
 #endif
 
 	return (-1);
  fail:
 #if USE_RESOLV_CACHE
-	_resolv_cache_query_failed(cache, buf, buflen);
+	_resolv_cache_query_failed(statp->netid, buf, buflen);
 #endif
 	res_nclose(statp);
 	return (-1);