From fbcddd6465dd69c949c8cac763569a02576b2579 Mon Sep 17 00:00:00 2001 From: Andrew Thompson Date: Mon, 9 Mar 2009 20:22:42 +0000 Subject: [PATCH] Some more memory management tweaks git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@12543 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- .../mod_erlang_event/handle_msg.c | 39 ++++++++++++++++--- .../mod_erlang_event/mod_erlang_event.c | 17 ++++---- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/mod/event_handlers/mod_erlang_event/handle_msg.c b/src/mod/event_handlers/mod_erlang_event/handle_msg.c index c77470b07d..a8bf507816 100644 --- a/src/mod/event_handlers/mod_erlang_event/handle_msg.c +++ b/src/mod/event_handlers/mod_erlang_event/handle_msg.c @@ -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) { 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)) { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "badarg"); } else { - ei_x_buff *nbuf = switch_core_alloc(listener->pool, sizeof(nbuf)); - nbuf->buff = switch_core_alloc(listener->pool, buf->buffsz); + ei_x_buff *nbuf = malloc(sizeof(nbuf)); + nbuf->buff = malloc(buf->buffsz); memcpy(nbuf->buff, buf->buff, buf->buffsz); nbuf->index = buf->index; nbuf->buffsz = buf->buffsz; - - switch_core_hash_insert(listener->fetch_reply_hash, uuid_str, nbuf); - ei_x_encode_atom(rbuf, "ok"); + + 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); + 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; } @@ -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) { erlang_ref ref; - erlang_pid *pid;/* = switch_core_alloc(listener->pool, sizeof(erlang_pid));*/ + erlang_pid *pid; void *p; char hash[100]; int arity; diff --git a/src/mod/event_handlers/mod_erlang_event/mod_erlang_event.c b/src/mod/event_handlers/mod_erlang_event/mod_erlang_event.c index 350144efd5..8aad698c57 100644 --- a/src/mod/event_handlers/mod_erlang_event/mod_erlang_event.c +++ b/src/mod/event_handlers/mod_erlang_event/mod_erlang_event.c @@ -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_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); 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; ei_x_buff *rep; - /*int index = 3;*/ - while (!(rep = (ei_x_buff *) switch_core_hash_find(ptr->listener->fetch_reply_hash, uuid_str))) { + void *p = NULL; + + while (!(p = switch_core_hash_find(ptr->listener->fetch_reply_hash, uuid_str)) || p == &globals.WAITING) { 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; } i++; switch_yield(10000); /* 10ms */ } + rep = (ei_x_buff *) p; int 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"); } + /* cleanup */ switch_core_hash_delete(ptr->listener->fetch_reply_hash, uuid_str); - - /*switch_safe_free(rep->buff);*/ - /*switch_safe_free(rep);*/ + free(rep->buff); + free(rep); free(xmlstr); return xml;