Some more memory management tweaks

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@12543 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Andrew Thompson 2009-03-09 20:22:42 +00:00
parent ca2fcf9b9f
commit fbcddd6465
2 changed files with 43 additions and 13 deletions

View File

@ -165,20 +165,47 @@ static void *SWITCH_THREAD_FUNC api_exec(switch_thread_t *thread, void *obj)
static switch_status_t handle_msg_fetch_reply(listener_t *listener, ei_x_buff *buf, ei_x_buff *rbuf) static switch_status_t handle_msg_fetch_reply(listener_t *listener, ei_x_buff *buf, ei_x_buff *rbuf)
{ {
char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1]; char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
void *p;
if (ei_decode_string_or_binary(buf->buff, &buf->index, SWITCH_UUID_FORMATTED_LENGTH, uuid_str)) { if (ei_decode_string_or_binary(buf->buff, &buf->index, SWITCH_UUID_FORMATTED_LENGTH, uuid_str)) {
ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_tuple_header(rbuf, 2);
ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "error");
ei_x_encode_atom(rbuf, "badarg"); ei_x_encode_atom(rbuf, "badarg");
} else { } else {
ei_x_buff *nbuf = switch_core_alloc(listener->pool, sizeof(nbuf)); ei_x_buff *nbuf = malloc(sizeof(nbuf));
nbuf->buff = switch_core_alloc(listener->pool, buf->buffsz); nbuf->buff = malloc(buf->buffsz);
memcpy(nbuf->buff, buf->buff, buf->buffsz); memcpy(nbuf->buff, buf->buff, buf->buffsz);
nbuf->index = buf->index; nbuf->index = buf->index;
nbuf->buffsz = buf->buffsz; nbuf->buffsz = buf->buffsz;
if ((p = switch_core_hash_find(listener->fetch_reply_hash, uuid_str))) {
if (p == &globals.TIMEOUT) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Handler for %s timed out\n", uuid_str);
switch_core_hash_delete(listener->fetch_reply_hash, uuid_str);
ei_x_encode_tuple_header(rbuf, 2);
ei_x_encode_atom(rbuf, "error");
ei_x_encode_atom(rbuf, "timeout");
} else if (p == &globals.WAITING) {
/* update the key to point at a pid */
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Found waiting slot for %s\n", uuid_str);
switch_core_hash_delete(listener->fetch_reply_hash, uuid_str);
switch_core_hash_insert(listener->fetch_reply_hash, uuid_str, nbuf); switch_core_hash_insert(listener->fetch_reply_hash, uuid_str, nbuf);
ei_x_encode_atom(rbuf, "ok"); ei_x_encode_atom(rbuf, "ok");
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Found filled slot for %s\n", uuid_str);
ei_x_encode_tuple_header(rbuf, 2);
ei_x_encode_atom(rbuf, "error");
ei_x_encode_atom(rbuf, "duplicate_response");
}
} else {
/* nothin in the hash */
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Empty slot for %s\n", uuid_str);
ei_x_encode_tuple_header(rbuf, 2);
ei_x_encode_atom(rbuf, "error");
ei_x_encode_atom(rbuf, "invalid_uuid");
}
/*switch_core_hash_insert(listener->fetch_reply_hash, uuid_str, nbuf);*/
} }
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
@ -700,7 +727,7 @@ static switch_status_t handle_msg_atom(listener_t *listener, erlang_msg *msg, e
static switch_status_t handle_ref_tuple(listener_t *listener, erlang_msg *msg, ei_x_buff *buf, ei_x_buff *rbuf) static switch_status_t handle_ref_tuple(listener_t *listener, erlang_msg *msg, ei_x_buff *buf, ei_x_buff *rbuf)
{ {
erlang_ref ref; erlang_ref ref;
erlang_pid *pid;/* = switch_core_alloc(listener->pool, sizeof(erlang_pid));*/ erlang_pid *pid;
void *p; void *p;
char hash[100]; char hash[100];
int arity; int arity;

View File

@ -388,7 +388,7 @@ static switch_xml_t erlang_fetch(const char *sectionstr, const char *tag_name, c
_ei_x_encode_string(&buf, uuid_str); _ei_x_encode_string(&buf, uuid_str);
ei_encode_switch_event_headers(&buf, params); ei_encode_switch_event_headers(&buf, params);
/*switch_core_hash_insert(ptr->reply_hash, uuid_str, );*/ switch_core_hash_insert(ptr->listener->fetch_reply_hash, uuid_str, &globals.WAITING);
switch_mutex_lock(ptr->listener->sock_mutex); switch_mutex_lock(ptr->listener->sock_mutex);
ei_sendto(ptr->listener->ec, ptr->listener->sockfd, &ptr->process, &buf); ei_sendto(ptr->listener->ec, ptr->listener->sockfd, &ptr->process, &buf);
@ -396,16 +396,19 @@ static switch_xml_t erlang_fetch(const char *sectionstr, const char *tag_name, c
int i = 0; int i = 0;
ei_x_buff *rep; ei_x_buff *rep;
/*int index = 3;*/ void *p = NULL;
while (!(rep = (ei_x_buff *) switch_core_hash_find(ptr->listener->fetch_reply_hash, uuid_str))) {
while (!(p = switch_core_hash_find(ptr->listener->fetch_reply_hash, uuid_str)) || p == &globals.WAITING) {
if (i > 50) { /* half a second timeout */ if (i > 50) { /* half a second timeout */
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Timed out when waiting for XML fetch response!\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Timed out when waiting for XML fetch response\n");
switch_core_hash_insert(ptr->listener->fetch_reply_hash, uuid_str, &globals.TIMEOUT); /* TODO lock this? */
return NULL; return NULL;
} }
i++; i++;
switch_yield(10000); /* 10ms */ switch_yield(10000); /* 10ms */
} }
rep = (ei_x_buff *) p;
int type, size; int type, size;
ei_get_type(rep->buff, &rep->index, &type, &size); ei_get_type(rep->buff, &rep->index, &type, &size);
@ -432,10 +435,10 @@ static switch_xml_t erlang_fetch(const char *sectionstr, const char *tag_name, c
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "XML parsed OK!\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "XML parsed OK!\n");
} }
/* cleanup */
switch_core_hash_delete(ptr->listener->fetch_reply_hash, uuid_str); switch_core_hash_delete(ptr->listener->fetch_reply_hash, uuid_str);
free(rep->buff);
/*switch_safe_free(rep->buff);*/ free(rep);
/*switch_safe_free(rep);*/
free(xmlstr); free(xmlstr);
return xml; return xml;