commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1100451 - /commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c
Date Sat, 07 May 2011 05:26:08 GMT
Author: mturk
Date: Sat May  7 05:26:07 2011
New Revision: 1100451

URL: http://svn.apache.org/viewvc?rev=1100451&view=rev
Log:
Allocate entire sockaddr at once

Modified:
    commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c?rev=1100451&r1=1100450&r2=1100451&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c Sat May  7 05:26:07 2011
@@ -474,13 +474,15 @@ call_resolver(acr_sockaddr_t **sa, const
               int family, int port, int flags)
 {
     struct addrinfo hints, *ai, *ai_list;
-    acr_sockaddr_t *prev_sa;
+    acr_sockaddr_t *prev_sa, *new_sa;
     int error;
+    int ai_len = ISIZEOF(acr_sockaddr_t);
+    int ai_cnt = 0;
     char *servname = 0;
-    char buf[16];
+    char  buf[16];
 
     memset(&hints, 0, sizeof(hints));
-    hints.ai_family = family;
+    hints.ai_family   = family;
     hints.ai_socktype = SOCK_STREAM;
 #if defined(AI_ADDRCONFIG)
     if (family == AF_UNSPEC) {
@@ -531,12 +533,21 @@ call_resolver(acr_sockaddr_t **sa, const
 #endif /* WIN32 */
     }
 
-    prev_sa = NULL;
+    prev_sa = 0;
     ai = ai_list;
-
-    while (ai) { /* while more addresses to report */
-        acr_sockaddr_t *new_sa;
-
+    while (ai != 0) {
+        if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) {
+            ai = ai->ai_next;
+            continue;
+        }
+        ai_cnt++;
+        ai = ai->ai_next;
+    }
+    new_sa = calloc(1, ai_len * ai_cnt);
+    if (sa == 0)
+        return ACR_ENOMEM;
+    ai = ai_list;
+    while (ai) {
         /* Ignore anything bogus: getaddrinfo in some old versions of
          * glibc will return AF_LOCAL entries for APR_UNSPEC+AI_PASSIVE
          * lookups. */
@@ -544,14 +555,11 @@ call_resolver(acr_sockaddr_t **sa, const
             ai = ai->ai_next;
             continue;
         }
-
-        new_sa = calloc(1, sizeof(acr_sockaddr_t));
-        if (new_sa == 0)
-            return ACR_ENOMEM;
         memcpy(&new_sa->sa, ai->ai_addr, ai->ai_addrlen);
         sockaddr_vars_set(new_sa, ai->ai_family, port);
 
-        if (!prev_sa) { /* first element in new list */
+        if (!prev_sa) {
+            /* first element in new list */
             if (hostname != 0)
                 strlcpy(new_sa->hostname, hostname, NI_MAXHOST);
             *sa = new_sa;
@@ -561,7 +569,7 @@ call_resolver(acr_sockaddr_t **sa, const
             prev_sa->next = new_sa;
         }
 
-        prev_sa = new_sa;
+        prev_sa = new_sa++;
         ai = ai->ai_next;
     }
     freeaddrinfo(ai_list);
@@ -727,7 +735,6 @@ sockaddr_to_byte_array(JNI_STDENV, acr_s
     acr_sockaddr_t *ai;
     int ai_len = ISIZEOF(acr_sockaddr_t);
     int ai_cnt = 0;
-    int ai_pos = 0;
 
     ai = sa;
     while (ai != 0) {
@@ -737,21 +744,9 @@ sockaddr_to_byte_array(JNI_STDENV, acr_s
     if (ai_cnt == 0)
         return 0;
     ba = (*env)->NewByteArray(env, ai_len * ai_cnt);
-    if (ba == 0)
-        goto cleanup;
-    ai = sa;
-    while (ai != 0) {
-        (*env)->SetByteArrayRegion(env, ba, ai_pos, ai_len, (jbyte *)ai);
-        ai = ai->next;
-        ai_pos += ai_len;
-    }
-cleanup:
-    ai = sa;
-    while (sa != 0) {
-        ai = sa->next;
-        AcrFree(sa);
-        sa = ai;
-    }
+    if (ba != 0)
+        (*env)->SetByteArrayRegion(env, ba, 0, ai_len * ai_cnt, (jbyte *)sa);
+    AcrFree(sa);
     return ba;
 }
 



Mime
View raw message