From 8eaf8844173c3c01d9273dfe858d1bdf4a972f50 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 14 Mar 2016 09:18:01 +0100 Subject: multi: introduce sh_getentry() for looking up sockets in the sockhash Simplify the code by using a single entry that looks for a socket in the socket hash. As indicated in #712, the code looked for CURL_SOCKET_BAD at some point and that is ineffective/wrong and this makes it easier to avoid that. --- lib/multi.c | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) (limited to 'lib/multi.c') diff --git a/lib/multi.c b/lib/multi.c index 068ac48a1..f7624dd26 100644 --- a/lib/multi.c +++ b/lib/multi.c @@ -172,13 +172,22 @@ struct Curl_sh_entry { #define SH_READ 1 #define SH_WRITE 2 +/* look up a given socket in the socket hash, skip invalid sockets */ +static struct Curl_sh_entry *sh_getentry(struct curl_hash *sh, + curl_socket_t s) +{ + if(s != CURL_SOCKET_BAD) + /* only look for proper sockets */ + return Curl_hash_pick(sh, (char *)&s, sizeof(curl_socket_t)); + return NULL; +} + /* make sure this socket is present in the hash for this handle */ static struct Curl_sh_entry *sh_addentry(struct curl_hash *sh, curl_socket_t s, struct SessionHandle *data) { - struct Curl_sh_entry *there = - Curl_hash_pick(sh, (char *)&s, sizeof(curl_socket_t)); + struct Curl_sh_entry *there = sh_getentry(sh, s); struct Curl_sh_entry *check; if(there) @@ -206,15 +215,9 @@ static struct Curl_sh_entry *sh_addentry(struct curl_hash *sh, /* delete the given socket + handle from the hash */ static void sh_delentry(struct curl_hash *sh, curl_socket_t s) { - struct Curl_sh_entry *there = - Curl_hash_pick(sh, (char *)&s, sizeof(curl_socket_t)); - - if(there) { - /* this socket is in the hash */ - /* We remove the hash entry. (This'll end up in a call to - sh_freeentry().) */ - Curl_hash_delete(sh, (char *)&s, sizeof(curl_socket_t)); - } + /* We remove the hash entry. This will end up in a call to + sh_freeentry(). */ + Curl_hash_delete(sh, (char *)&s, sizeof(curl_socket_t)); } /* @@ -2019,7 +2022,7 @@ static void singlesocket(struct Curl_multi *multi, s = socks[i]; /* get it from the hash */ - entry = Curl_hash_pick(&multi->sockhash, (char *)&s, sizeof(s)); + entry = sh_getentry(&multi->sockhash, s); if(curraction & GETSOCK_READSOCK(i)) action |= CURL_POLL_IN; @@ -2070,7 +2073,7 @@ static void singlesocket(struct Curl_multi *multi, /* this socket has been removed. Tell the app to remove it */ remove_sock_from_hash = TRUE; - entry = Curl_hash_pick(&multi->sockhash, (char *)&s, sizeof(s)); + entry = sh_getentry(&multi->sockhash, s); if(entry) { /* check if the socket to be removed serves a connection which has other easy-s in a pipeline. In this case the socket should not be @@ -2151,8 +2154,7 @@ void Curl_multi_closed(struct connectdata *conn, curl_socket_t s) if(multi) { /* this is set if this connection is part of a handle that is added to a multi handle, and only then this is necessary */ - struct Curl_sh_entry *entry = - Curl_hash_pick(&multi->sockhash, (char *)&s, sizeof(s)); + struct Curl_sh_entry *entry = sh_getentry(&multi->sockhash, s); if(entry) { if(multi->socket_cb) @@ -2253,8 +2255,7 @@ static CURLMcode multi_socket(struct Curl_multi *multi, } else if(s != CURL_SOCKET_TIMEOUT) { - struct Curl_sh_entry *entry = - Curl_hash_pick(&multi->sockhash, (char *)&s, sizeof(s)); + struct Curl_sh_entry *entry = sh_getentry(&multi->sockhash, s); if(!entry) /* Unmatched socket, we can't act on it but we ignore this fact. In @@ -2741,9 +2742,7 @@ CURLMcode curl_multi_assign(CURLM *multi_handle, struct Curl_sh_entry *there = NULL; struct Curl_multi *multi = (struct Curl_multi *)multi_handle; - if(s != CURL_SOCKET_BAD) - there = Curl_hash_pick(&multi->sockhash, (char *)&s, - sizeof(curl_socket_t)); + there = sh_getentry(&multi->sockhash, s); if(!there) return CURLM_BAD_SOCKET; @@ -2821,8 +2820,7 @@ void Curl_multi_dump(const struct Curl_multi *multi_handle) statename[data->mstate], data->numsocks); for(i=0; i < data->numsocks; i++) { curl_socket_t s = data->sockets[i]; - struct Curl_sh_entry *entry = - Curl_hash_pick(&multi->sockhash, (char *)&s, sizeof(s)); + struct Curl_sh_entry *entry = sh_getentry(&multi->sockhash, s); fprintf(stderr, "%d ", (int)s); if(!entry) { -- cgit v1.2.3