static switch_hash_t *CUSTOM_HASH = NULL;
static int THREAD_RUNNING = 0;
static int EVENT_QUEUE_HAVEMORE = 0;
-
+static switch_queue_t *EVENT_RECYCLE_QUEUE = NULL;
+static switch_queue_t *EVENT_HEADER_RECYCLE_QUEUE = NULL;
#if 0
static void *locked_alloc(switch_size_t len)
{
#define DUP(str) my_dup(str)
#endif
#ifndef FREE
-#define FREE(ptr) if (ptr) free(ptr)
+#define FREE(ptr) switch_safe_free(ptr)
#endif
/* make sure this is synced with the switch_event_types_t enum in switch_types.h
SWITCH_DECLARE(switch_status_t) switch_event_shutdown(void)
{
int x = 0, last = 0;
+ void *pop;
if (THREAD_RUNNING > 0) {
THREAD_RUNNING = -1;
}
switch_core_hash_destroy(&CUSTOM_HASH);
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Returning %d recycled event node(s) and %d recycled event header node(s)\n",
+ switch_queue_size(EVENT_RECYCLE_QUEUE),switch_queue_size(EVENT_HEADER_RECYCLE_QUEUE));
+
+
+ while (switch_queue_trypop(EVENT_HEADER_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS) {
+ free(pop);
+ }
+ while (switch_queue_trypop(EVENT_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS) {
+ free(pop);
+ }
+
return SWITCH_STATUS_SUCCESS;
}
switch_queue_create(&EVENT_QUEUE[0], POOL_COUNT_MAX + 10, THRUNTIME_POOL);
switch_queue_create(&EVENT_QUEUE[1], POOL_COUNT_MAX + 10, THRUNTIME_POOL);
switch_queue_create(&EVENT_QUEUE[2], POOL_COUNT_MAX + 10, THRUNTIME_POOL);
+ switch_queue_create(&EVENT_RECYCLE_QUEUE, SWITCH_CORE_QUEUE_LEN, THRUNTIME_POOL);
+ switch_queue_create(&EVENT_HEADER_RECYCLE_QUEUE, SWITCH_CORE_QUEUE_LEN, THRUNTIME_POOL);
+
+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Activate Eventing Engine.\n");
switch_mutex_init(&BLOCK, SWITCH_MUTEX_NESTED, RUNTIME_POOL);
SWITCH_DECLARE(switch_status_t) switch_event_create_subclass(switch_event_t **event, switch_event_types_t event_id, const char *subclass_name)
{
-
+ void *pop;
+
if (event_id != SWITCH_EVENT_CUSTOM && subclass_name) {
return SWITCH_STATUS_GENERR;
}
-
- if ((*event = ALLOC(sizeof(switch_event_t))) == 0) {
- return SWITCH_STATUS_MEMERR;
+
+ if (switch_queue_trypop(EVENT_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS) {
+ *event = (switch_event_t *) pop;
+ } else {
+ *event = ALLOC(sizeof(switch_event_t));
+ assert(*event);
}
+
memset(*event, 0, sizeof(switch_event_t));
(*event)->event_id = event_id;
}
FREE(hp->name);
FREE(hp->value);
- FREE(hp);
+ switch_queue_push(EVENT_HEADER_RECYCLE_QUEUE, hp);
status = SWITCH_STATUS_SUCCESS;
break;
}
return SWITCH_STATUS_MEMERR;
} else {
switch_event_header_t *header, *hp;
+ void *pop;
- if ((header = ALLOC(sizeof(*header))) == 0) {
- return SWITCH_STATUS_MEMERR;
+ if (switch_queue_trypop(EVENT_HEADER_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS) {
+ header = (switch_event_header_t *) pop;
+ } else {
+ header = ALLOC(sizeof(*header));
+ assert(header);
}
memset(header, 0, sizeof(*header));
hp = hp->next;
FREE(this->name);
FREE(this->value);
- FREE(this);
+ switch_queue_push(EVENT_HEADER_RECYCLE_QUEUE, this);
}
FREE(ep->body);
- FREE(ep);
+ switch_queue_push(EVENT_RECYCLE_QUEUE, ep);
}
*event = NULL;
}
static switch_log_binding_t *BINDINGS = NULL;
static switch_mutex_t *BINDLOCK = NULL;
static switch_queue_t *LOG_QUEUE = NULL;
+static switch_queue_t *LOG_RECYCLE_QUEUE = NULL;
static int8_t THREAD_RUNNING = 0;
static uint8_t MAX_LEVEL = 0;
}
node = (switch_log_node_t *) pop;
-
switch_mutex_lock(BINDLOCK);
for (binding = BINDINGS; binding; binding = binding->next) {
if (binding->level >= node->level) {
}
}
switch_mutex_unlock(BINDLOCK);
- if (node) {
- if (node->data) {
- free(node->data);
- }
-
- if (node->file) {
- free(node->file);
- }
-
- if (node->func) {
- free(node->func);
- }
-
- free(node);
- }
+
+ switch_safe_free(node->data);
+ switch_queue_push(LOG_RECYCLE_QUEUE, node);
}
THREAD_RUNNING = 0;
switch_time_t now = switch_time_now();
uint32_t len;
const char *extra_fmt = "%s [%s] %s:%d %s()%c%s";
+
va_start(ap, fmt);
handle = switch_core_data_channel(channel);
free(data);
} else if (level <= MAX_LEVEL) {
switch_log_node_t *node;
+ void *pop = NULL;
- if ((node = malloc(sizeof(*node)))) {
- node->data = data;
- node->file = strdup(filep);
- node->func = strdup(funcp);
- node->line = line;
- node->level = level;
- node->content = content;
- node->timestamp = now;
- switch_queue_push(LOG_QUEUE, node);
+ if (switch_queue_trypop(LOG_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS) {
+ node = (switch_log_node_t *) pop;
} else {
- free(data);
+ node = malloc(sizeof(*node));
+ assert(node);
}
+
+ node->data = data;
+ switch_set_string(node->file, filep);
+ switch_set_string(node->func, funcp);
+ node->line = line;
+ node->level = level;
+ node->content = content;
+ node->timestamp = now;
+ switch_queue_push(LOG_QUEUE, node);
}
}
}
- if (new_fmt) {
- free(new_fmt);
- }
-
+ switch_safe_free(new_fmt);
fflush(handle);
}
switch_queue_create(&LOG_QUEUE, SWITCH_CORE_QUEUE_LEN, LOG_POOL);
+ switch_queue_create(&LOG_RECYCLE_QUEUE, SWITCH_CORE_QUEUE_LEN, LOG_POOL);
switch_mutex_init(&BINDLOCK, SWITCH_MUTEX_NESTED, LOG_POOL);
switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
switch_thread_create(&thread, thd_attr, log_thread, NULL, LOG_POOL);
SWITCH_DECLARE(switch_status_t) switch_log_shutdown(void)
{
+ void *pop;
+
THREAD_RUNNING = -1;
switch_queue_push(LOG_QUEUE, NULL);
while (THREAD_RUNNING) {
switch_yield(1000);
}
+
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Returning %d recycled log node(s)\n", switch_queue_size(LOG_RECYCLE_QUEUE));
+ while (switch_queue_trypop(LOG_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS) {
+ free(pop);
+ }
+
return SWITCH_STATUS_SUCCESS;
}