[8/8] network: Load the getaddrinfo function pointers at startup

Message ID 1340712758-65898-8-git-send-email-martin@martin.st
State Deferred
Headers show

Commit Message

Martin Storsjö June 26, 2012, 12:12 p.m.
This avoids having to load them each time getaddrinfo is called.
avformat_network_init exists since the last major bump and should
be called for doing static library initialization before libavformat
is used by threads.
---
 libavformat/network.c    |   10 ++++++++++
 libavformat/network.h    |   10 ++++++++++
 libavformat/os_support.c |   26 ++++++--------------------
 3 files changed, 26 insertions(+), 20 deletions(-)

Patch

diff --git a/libavformat/network.c b/libavformat/network.c
index c702214..b946767 100644
--- a/libavformat/network.c
+++ b/libavformat/network.c
@@ -125,6 +125,13 @@  int ff_network_inited_globally;
 static int ff_winsock_ptrs_loaded;
 int (WSAAPI *ff_wsapoll_ptr)(struct pollfd *fds, unsigned long nfds,
                              int timeout);
+int (WSAAPI *ff_wsa_getaddrinfo_ptr)(const char *node, const char *service,
+                                     const struct addrinfo *hints,
+                                     struct addrinfo **res);
+void (WSAAPI *ff_wsa_freeaddrinfo_ptr)(struct addrinfo *res);
+int (WSAAPI *ff_wsa_getnameinfo_ptr)(const struct sockaddr *sa, socklen_t salen,
+                                     char *host, DWORD hostlen,
+                                     char *serv, DWORD servlen, int flags);
 #endif
 
 int ff_network_init(void)
@@ -146,6 +153,9 @@  int ff_network_init(void)
          * which should be done before spawning any threads. */
         HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
         ff_wsapoll_ptr = GetProcAddress(ws2mod, "WSAPoll");
+        ff_wsa_getaddrinfo_ptr = GetProcAddress(ws2mod, "getaddrinfo");
+        ff_wsa_freeaddrinfo_ptr = GetProcAddress(ws2mod, "freeaddrinfo");
+        ff_wsa_getnameinfo_ptr = GetProcAddress(ws2mod, "getnameinfo");
         ff_winsock_ptrs_loaded = 1;
     }
 #endif
diff --git a/libavformat/network.h b/libavformat/network.h
index 8fb7a82..46b968f 100644
--- a/libavformat/network.h
+++ b/libavformat/network.h
@@ -54,6 +54,16 @@  int ff_neterrno(void);
 
 extern int (WSAAPI *ff_wsapoll_ptr)(struct pollfd *fds, unsigned long nfds,
                                     int timeout);
+extern int (WSAAPI *ff_wsa_getaddrinfo_ptr)(const char *node,
+                                            const char *service,
+                                            const struct addrinfo *hints,
+                                            struct addrinfo **res);
+extern void (WSAAPI *ff_wsa_freeaddrinfo_ptr)(struct addrinfo *res);
+extern int (WSAAPI *ff_wsa_getnameinfo_ptr)(const struct sockaddr *sa,
+                                            socklen_t salen,
+                                            char *host, DWORD hostlen,
+                                            char *serv, DWORD servlen,
+                                            int flags);
 #else
 #include <sys/types.h>
 #include <sys/socket.h>
diff --git a/libavformat/os_support.c b/libavformat/os_support.c
index 9ef63eb..16949d0 100644
--- a/libavformat/os_support.c
+++ b/libavformat/os_support.c
@@ -102,13 +102,8 @@  int ff_getaddrinfo(const char *node, const char *service,
     struct sockaddr_in *sin;
 
 #if HAVE_WINSOCK2_H
-    int (WSAAPI *win_getaddrinfo)(const char *node, const char *service,
-                                  const struct addrinfo *hints,
-                                  struct addrinfo **res);
-    HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
-    win_getaddrinfo = GetProcAddress(ws2mod, "getaddrinfo");
-    if (win_getaddrinfo)
-        return win_getaddrinfo(node, service, hints, res);
+    if (ff_wsa_getaddrinfo_ptr)
+        return ff_wsa_getaddrinfo_ptr(node, service, hints, res);
 #endif
 
     *res = NULL;
@@ -169,12 +164,8 @@  int ff_getaddrinfo(const char *node, const char *service,
 void ff_freeaddrinfo(struct addrinfo *res)
 {
 #if HAVE_WINSOCK2_H
-    void (WSAAPI *win_freeaddrinfo)(struct addrinfo *res);
-    HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
-    win_freeaddrinfo = (void (WSAAPI *)(struct addrinfo *res))
-                       GetProcAddress(ws2mod, "freeaddrinfo");
-    if (win_freeaddrinfo) {
-        win_freeaddrinfo(res);
+    if (ff_wsa_freeaddrinfo_ptr) {
+        ff_wsa_freeaddrinfo_ptr(res);
         return;
     }
 #endif
@@ -191,13 +182,8 @@  int ff_getnameinfo(const struct sockaddr *sa, int salen,
     const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
 
 #if HAVE_WINSOCK2_H
-    int (WSAAPI *win_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
-                                  char *host, DWORD hostlen,
-                                  char *serv, DWORD servlen, int flags);
-    HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
-    win_getnameinfo = GetProcAddress(ws2mod, "getnameinfo");
-    if (win_getnameinfo)
-        return win_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
+    if (ff_wsa_getnameinfo_ptr)
+        return ff_wsa_getnameinfo_ptr(sa, salen, host, hostlen, serv, servlen, flags);
 #endif
 
     if (sa->sa_family != AF_INET)