summaryrefslogtreecommitdiff
path: root/network_io
diff options
context:
space:
mode:
authorbnicholes <bnicholes@13f79535-47bb-0310-9956-ffa450edef68>2002-01-04 17:25:49 +0000
committerbnicholes <bnicholes@13f79535-47bb-0310-9956-ffa450edef68>2002-01-04 17:25:49 +0000
commita97d8e4dabadcf76b38f54ae708596b788929e98 (patch)
tree05de8ef053bdb3162ba73350cbdbcd707552df00 /network_io
parent4d2196f325a46bdd0170a160ee1360ff297d5568 (diff)
downloadlibapr-a97d8e4dabadcf76b38f54ae708596b788929e98.tar.gz
Deferred the memory allocation for the new apr_socket_t until after the accept()
call so that we don't chew up huge chunks of memory when using non-blocking sockets and also avoiding unnecessary memory allocations on error conditions. git-svn-id: http://svn.apache.org/repos/asf/apr/apr/trunk@62700 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'network_io')
-rw-r--r--network_io/win32/sockets.c20
1 files changed, 13 insertions, 7 deletions
diff --git a/network_io/win32/sockets.c b/network_io/win32/sockets.c
index fb233ad87..b9a3fc6ce 100644
--- a/network_io/win32/sockets.c
+++ b/network_io/win32/sockets.c
@@ -226,20 +226,26 @@ APR_DECLARE(apr_status_t) apr_listen(apr_socket_t *sock, apr_int32_t backlog)
APR_DECLARE(apr_status_t) apr_accept(apr_socket_t **new, apr_socket_t *sock,
apr_pool_t *p)
{
+ SOCKET s;
+ struct sockaddr sa;
+ int salen = sizeof(sock->remote_addr->sa);
+
+ // Don't allocate the memory until after we call accept. This allows
+ // us to work with nonblocking sockets.
+ s = accept(sock->sock, (struct sockaddr *)&sa, &salen);
+ if (s == INVALID_SOCKET) {
+ return apr_get_netos_error();
+ }
+
alloc_socket(new, p);
set_socket_vars(*new, sock->local_addr->sa.sin.sin_family, SOCK_STREAM);
(*new)->timeout = -1;
(*new)->disconnected = 0;
+ (*new)->sock = s;
(*new)->remote_addr->salen = sizeof((*new)->remote_addr->sa);
- (*new)->sock = accept(sock->sock,
- (struct sockaddr *)&(*new)->remote_addr->sa,
- &(*new)->remote_addr->salen);
-
- if ((*new)->sock == INVALID_SOCKET) {
- return apr_get_netos_error();
- }
+ memcpy (&(*new)->remote_addr->sa, &sa, salen);
*(*new)->local_addr = *sock->local_addr;
/* The above assignment just overwrote the pool entry. Setting the local_addr