1
0
mirror of https://github.com/moparisthebest/curl synced 2024-11-12 04:25:08 -05:00

multi: avoid a malloc() when a transfer is complete

The struct used for storing the message for a completed transfer is now
no longer allocated separatly but is kept within the main struct kept
for each easy handle so that we avoid one malloc (and the subsequent
free).
This commit is contained in:
Daniel Stenberg 2010-08-08 01:06:29 +02:00
parent c49e9683b8
commit 1267719735

View File

@ -61,7 +61,6 @@
struct Curl_message { struct Curl_message {
/* the 'CURLMsg' is the part that is visible to the external user */ /* the 'CURLMsg' is the part that is visible to the external user */
struct CURLMsg extmsg; struct CURLMsg extmsg;
struct Curl_message *next;
}; };
/* NOTE: if you add a state here, add the name to the statename[] array as /* NOTE: if you add a state here, add the name to the statename[] array as
@ -110,12 +109,8 @@ struct Curl_one_easy {
CURLMstate state; /* the handle's state */ CURLMstate state; /* the handle's state */
CURLcode result; /* previous result */ CURLcode result; /* previous result */
struct Curl_message *msg; /* A pointer to one single posted message. struct Curl_message msg; /* A single posted message. */
Cleanup should be done on this pointer NOT on int msg_stored; /* a message is stored in 'msg' to return */
the linked list in Curl_multi. This message
will be deleted when this handle is removed
from the multi-handle */
int msg_num; /* number of messages left in 'msg' to return */
/* Array with the plain socket numbers this handle takes care of, in no /* Array with the plain socket numbers this handle takes care of, in no
particular order. Note that all sockets are added to the sockhash, where particular order. Note that all sockets are added to the sockhash, where
@ -697,8 +692,6 @@ CURLMcode curl_multi_remove_handle(CURLM *multi_handle,
/* NOTE NOTE NOTE /* NOTE NOTE NOTE
We do not touch the easy handle here! */ We do not touch the easy handle here! */
if(easy->msg)
free(easy->msg);
free(easy); free(easy);
multi->num_easy--; /* one less to care about now */ multi->num_easy--; /* one less to care about now */
@ -1536,26 +1529,21 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi,
easy->result = CURLE_ABORTED_BY_CALLBACK; easy->result = CURLE_ABORTED_BY_CALLBACK;
} }
} while(0); } while(0);
if((CURLM_STATE_COMPLETED == easy->state) && !easy->msg) { if((CURLM_STATE_COMPLETED == easy->state) && !easy->msg_stored) {
if(easy->easy_handle->dns.hostcachetype == HCACHE_MULTI) { if(easy->easy_handle->dns.hostcachetype == HCACHE_MULTI) {
/* clear out the usage of the shared DNS cache */ /* clear out the usage of the shared DNS cache */
easy->easy_handle->dns.hostcache = NULL; easy->easy_handle->dns.hostcache = NULL;
easy->easy_handle->dns.hostcachetype = HCACHE_NONE; easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
} }
/* now add a node to the Curl_message linked list with this info */ /* now fill in the Curl_message with this info */
msg = malloc(sizeof(struct Curl_message)); msg = &easy->msg;
if(!msg)
return CURLM_OUT_OF_MEMORY;
msg->extmsg.msg = CURLMSG_DONE; msg->extmsg.msg = CURLMSG_DONE;
msg->extmsg.easy_handle = easy->easy_handle; msg->extmsg.easy_handle = easy->easy_handle;
msg->extmsg.data.result = easy->result; msg->extmsg.data.result = easy->result;
msg->next = NULL;
easy->msg = msg; easy->msg_stored = 1; /* there is an unread message here */
easy->msg_num = 1; /* there is one unread message here */
multi->num_msgs++; /* increase message counter */ multi->num_msgs++; /* increase message counter */
} }
@ -1699,8 +1687,6 @@ CURLMcode curl_multi_cleanup(CURLM *multi_handle)
Curl_easy_addmulti(easy->easy_handle, NULL); /* clear the association */ Curl_easy_addmulti(easy->easy_handle, NULL); /* clear the association */
if(easy->msg)
free(easy->msg);
free(easy); free(easy);
easy = nexteasy; easy = nexteasy;
} }
@ -1727,8 +1713,8 @@ CURLMsg *curl_multi_info_read(CURLM *multi_handle, int *msgs_in_queue)
easy=multi->easy.next; easy=multi->easy.next;
while(easy != &multi->easy) { while(easy != &multi->easy) {
if(easy->msg_num) { if(easy->msg_stored) {
easy->msg_num--; easy->msg_stored = 0;;
break; break;
} }
easy = easy->next; easy = easy->next;
@ -1739,7 +1725,7 @@ CURLMsg *curl_multi_info_read(CURLM *multi_handle, int *msgs_in_queue)
multi->num_msgs--; multi->num_msgs--;
*msgs_in_queue = multi->num_msgs; *msgs_in_queue = multi->num_msgs;
return &easy->msg->extmsg; return &easy->msg.extmsg;
} }
else else
return NULL; return NULL;