mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-08-13 17:38:59 +00:00
tear the hell out of everything and rip it up into itsy bitsy pieces and put it all back together
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@5765 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
@@ -33,13 +33,19 @@
|
||||
*/
|
||||
#include <switch.h>
|
||||
#include "private/switch_core_pvt.h"
|
||||
/*#define LOCK_MORE*/
|
||||
|
||||
static struct {
|
||||
switch_mutex_t *mem_lock;
|
||||
switch_queue_t *pool_queue; /* 8 ball break */
|
||||
switch_memory_pool_t *memory_pool;
|
||||
int pool_thread_running;
|
||||
} memory_manager;
|
||||
|
||||
SWITCH_DECLARE(switch_memory_pool_t *) switch_core_session_get_pool(switch_core_session_t *session)
|
||||
{
|
||||
assert(session != NULL);
|
||||
assert(session->pool != NULL);
|
||||
return session->pool;
|
||||
}
|
||||
|
||||
@@ -51,14 +57,23 @@ SWITCH_DECLARE(void *) switch_core_session_alloc(switch_core_session_t *session,
|
||||
assert(session != NULL);
|
||||
assert(session->pool != NULL);
|
||||
|
||||
#ifdef DEBUG_ALLOC
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Allocate %d\n", memory);
|
||||
#ifdef LOCK_MORE
|
||||
switch_mutex_lock(memory_manager.mem_lock);
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG_ALLOC
|
||||
printf("Allocate %d\n", (int)memory);
|
||||
#endif
|
||||
|
||||
ptr = apr_palloc(session->pool, memory);
|
||||
assert(ptr != NULL);
|
||||
|
||||
memset(ptr, 0, memory);
|
||||
|
||||
#ifdef LOCK_MORE
|
||||
switch_mutex_unlock(memory_manager.mem_lock);
|
||||
#endif
|
||||
|
||||
if ((ptr = apr_palloc(session->pool, memory)) != 0) {
|
||||
memset(ptr, 0, memory);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
@@ -70,14 +85,23 @@ SWITCH_DECLARE(void *) switch_core_permanent_alloc(switch_size_t memory)
|
||||
void *ptr = NULL;
|
||||
assert(memory_manager.memory_pool != NULL);
|
||||
|
||||
|
||||
#ifdef DEBUG_ALLOC
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Perm Allocate %d\n", memory);
|
||||
#ifdef LOCK_MORE
|
||||
switch_mutex_lock(memory_manager.mem_lock);
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG_ALLOC
|
||||
printf("Perm Allocate %d\n", (int)memory);
|
||||
#endif
|
||||
|
||||
ptr = apr_palloc(memory_manager.memory_pool, memory);
|
||||
|
||||
assert(ptr != NULL);
|
||||
memset(ptr, 0, memory);
|
||||
|
||||
#ifdef LOCK_MORE
|
||||
switch_mutex_unlock(memory_manager.mem_lock);
|
||||
#endif
|
||||
|
||||
if ((ptr = apr_palloc(memory_manager.memory_pool, memory)) != 0) {
|
||||
memset(ptr, 0, memory);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
@@ -90,15 +114,22 @@ SWITCH_DECLARE(char *) switch_core_permanent_strdup(const char *todup)
|
||||
if (!todup)
|
||||
return NULL;
|
||||
|
||||
len = strlen(todup) + 1;
|
||||
|
||||
#ifdef DEBUG_ALLOC
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Perm Allocate %d\n", len);
|
||||
#ifdef LOCK_MORE
|
||||
switch_mutex_lock(memory_manager.mem_lock);
|
||||
#endif
|
||||
|
||||
len = strlen(todup) + 1;
|
||||
duped = apr_pstrmemdup(memory_manager.memory_pool, todup, len);
|
||||
assert(duped != NULL);
|
||||
|
||||
#ifdef DEBUG_ALLOC
|
||||
printf("Perm Allocate %d\n", (int)len);
|
||||
#endif
|
||||
|
||||
#ifdef LOCK_MORE
|
||||
switch_mutex_unlock(memory_manager.mem_lock);
|
||||
#endif
|
||||
|
||||
if (todup && (duped = apr_palloc(memory_manager.memory_pool, len)) != 0) {
|
||||
strncpy(duped, todup, len);
|
||||
}
|
||||
return duped;
|
||||
}
|
||||
|
||||
@@ -107,14 +138,23 @@ SWITCH_DECLARE(char *) switch_core_session_sprintf(switch_core_session_t *sessio
|
||||
va_list ap;
|
||||
char *result = NULL;
|
||||
|
||||
#ifdef LOCK_MORE
|
||||
switch_mutex_lock(memory_manager.mem_lock);
|
||||
#endif
|
||||
|
||||
assert(session != NULL);
|
||||
assert(session->pool != NULL);
|
||||
va_start(ap, fmt);
|
||||
|
||||
|
||||
result = apr_pvsprintf(session->pool, fmt, ap);
|
||||
|
||||
assert(result != NULL);
|
||||
va_end(ap);
|
||||
|
||||
|
||||
#ifdef LOCK_MORE
|
||||
switch_mutex_unlock(memory_manager.mem_lock);
|
||||
#endif
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -124,12 +164,21 @@ SWITCH_DECLARE(char *) switch_core_sprintf(switch_memory_pool_t *pool, const cha
|
||||
char *result = NULL;
|
||||
|
||||
assert(pool != NULL);
|
||||
|
||||
#ifdef LOCK_MORE
|
||||
switch_mutex_lock(memory_manager.mem_lock);
|
||||
#endif
|
||||
|
||||
va_start(ap, fmt);
|
||||
|
||||
result = apr_pvsprintf(pool, fmt, ap);
|
||||
|
||||
assert(result != NULL);
|
||||
va_end(ap);
|
||||
|
||||
#ifdef LOCK_MORE
|
||||
switch_mutex_unlock(memory_manager.mem_lock);
|
||||
#endif
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -144,15 +193,25 @@ SWITCH_DECLARE(char *) switch_core_session_strdup(switch_core_session_t *session
|
||||
if (!todup) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef LOCK_MORE
|
||||
switch_mutex_lock(memory_manager.mem_lock);
|
||||
#endif
|
||||
|
||||
len = strlen(todup) + 1;
|
||||
|
||||
#ifdef DEBUG_ALLOC
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Allocate %d\n", len);
|
||||
printf("Allocate %d\n", (int)len);
|
||||
#endif
|
||||
|
||||
duped = apr_pstrmemdup(session->pool, todup, len);
|
||||
assert(duped != NULL);
|
||||
|
||||
|
||||
#ifdef LOCK_MORE
|
||||
switch_mutex_unlock(memory_manager.mem_lock);
|
||||
#endif
|
||||
|
||||
if (todup && (duped = apr_palloc(session->pool, len)) != 0) {
|
||||
strncpy(duped, todup, len);
|
||||
}
|
||||
return duped;
|
||||
}
|
||||
|
||||
@@ -167,31 +226,61 @@ SWITCH_DECLARE(char *) switch_core_strdup(switch_memory_pool_t *pool, const char
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef LOCK_MORE
|
||||
switch_mutex_lock(memory_manager.mem_lock);
|
||||
#endif
|
||||
|
||||
len = strlen(todup) + 1;
|
||||
|
||||
#ifdef DEBUG_ALLOC
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Allocate %d\n", len);
|
||||
printf("Allocate %d\n", (int)len);
|
||||
#endif
|
||||
|
||||
duped = apr_pstrmemdup(pool, todup, len);
|
||||
assert(duped != NULL);
|
||||
|
||||
#ifdef LOCK_MORE
|
||||
switch_mutex_unlock(memory_manager.mem_lock);
|
||||
#endif
|
||||
|
||||
if (todup && (duped = apr_palloc(pool, len)) != 0) {
|
||||
strncpy(duped, todup, len);
|
||||
}
|
||||
return duped;
|
||||
}
|
||||
|
||||
SWITCH_DECLARE(switch_status_t) switch_core_new_memory_pool(switch_memory_pool_t **pool)
|
||||
SWITCH_DECLARE(switch_status_t) switch_core_perform_new_memory_pool(switch_memory_pool_t **pool, const char *file, const char *func, int line)
|
||||
{
|
||||
char *tmp;
|
||||
|
||||
if ((apr_pool_create(pool, NULL)) != SWITCH_STATUS_SUCCESS) {
|
||||
*pool = NULL;
|
||||
return SWITCH_STATUS_MEMERR;
|
||||
}
|
||||
switch_mutex_lock(memory_manager.mem_lock);
|
||||
assert(pool != NULL);
|
||||
|
||||
apr_pool_create(pool, NULL);
|
||||
assert(*pool != NULL);
|
||||
|
||||
#ifdef DEBUG_ALLOC2
|
||||
printf("New Pool %s %s:%d\n", file, func, line);
|
||||
#endif
|
||||
tmp = switch_core_sprintf(*pool, "%s:%d", func, line);
|
||||
apr_pool_tag(*pool, tmp);
|
||||
switch_mutex_unlock(memory_manager.mem_lock);
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
SWITCH_DECLARE(switch_status_t) switch_core_destroy_memory_pool(switch_memory_pool_t **pool)
|
||||
SWITCH_DECLARE(switch_status_t) switch_core_perform_destroy_memory_pool(switch_memory_pool_t **pool, const char *file, const char *func, int line)
|
||||
{
|
||||
apr_pool_destroy(*pool);
|
||||
//char tmp[128] = "";
|
||||
|
||||
|
||||
assert(pool != NULL);
|
||||
|
||||
#ifdef DEBUG_ALLOC2
|
||||
printf("Free Pool %s %s:%d\n", file, func, line);
|
||||
#endif
|
||||
|
||||
switch_queue_push(memory_manager.pool_queue, *pool);
|
||||
//apr_pool_destroy(*pool);
|
||||
*pool = NULL;
|
||||
|
||||
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -199,23 +288,108 @@ SWITCH_DECLARE(switch_status_t) switch_core_destroy_memory_pool(switch_memory_po
|
||||
SWITCH_DECLARE(void *) switch_core_alloc(switch_memory_pool_t *pool, switch_size_t memory)
|
||||
{
|
||||
void *ptr = NULL;
|
||||
|
||||
assert(pool != NULL);
|
||||
|
||||
#ifdef DEBUG_ALLOC
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Allocate %d\n", memory);
|
||||
/* assert(memory < 600000); */
|
||||
#ifdef LOCK_MORE
|
||||
switch_mutex_lock(memory_manager.mem_lock);
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG_ALLOC
|
||||
printf("Allocate %d\n", (int)memory);
|
||||
/*assert(memory < 20000);*/
|
||||
#endif
|
||||
|
||||
ptr = apr_palloc(pool, memory);
|
||||
assert(ptr != NULL);
|
||||
memset(ptr, 0, memory);
|
||||
|
||||
|
||||
#ifdef LOCK_MORE
|
||||
switch_mutex_unlock(memory_manager.mem_lock);
|
||||
#endif
|
||||
|
||||
if ((ptr = apr_palloc(pool, memory)) != 0) {
|
||||
memset(ptr, 0, memory);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
|
||||
static void *SWITCH_THREAD_FUNC pool_thread(switch_thread_t * thread, void *obj)
|
||||
{
|
||||
void *pop = NULL;
|
||||
switch_memory_pool_t *pool;
|
||||
|
||||
memory_manager.pool_thread_running = 1;
|
||||
|
||||
while (memory_manager.pool_thread_running == 1) {
|
||||
int len = switch_queue_size(memory_manager.pool_queue);
|
||||
|
||||
if (len) {
|
||||
int x = len, done = 0;
|
||||
|
||||
switch_yield(1000000);
|
||||
switch_mutex_lock(memory_manager.mem_lock);
|
||||
while (x > 0) {
|
||||
if (switch_queue_pop(memory_manager.pool_queue, &pop) != SWITCH_STATUS_SUCCESS) {
|
||||
done = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!pop) {
|
||||
done = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
pool = (switch_memory_pool_t *) pop;
|
||||
apr_pool_destroy(pool);
|
||||
pool = NULL;
|
||||
x--;
|
||||
}
|
||||
switch_mutex_unlock(memory_manager.mem_lock);
|
||||
if (done) {
|
||||
goto done;
|
||||
}
|
||||
} else {
|
||||
switch_yield(100000);
|
||||
}
|
||||
}
|
||||
|
||||
done:
|
||||
memory_manager.pool_thread_running = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
void switch_core_memory_stop(void)
|
||||
{
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Stopping memory pool queue.\n");
|
||||
switch_queue_push(memory_manager.pool_queue, NULL);
|
||||
while(memory_manager.pool_thread_running) {
|
||||
switch_yield(1000);
|
||||
}
|
||||
}
|
||||
|
||||
switch_memory_pool_t *switch_core_memory_init(void)
|
||||
{
|
||||
switch_thread_t *thread;
|
||||
switch_threadattr_t *thd_attr;
|
||||
|
||||
|
||||
memset(&memory_manager, 0, sizeof(memory_manager));
|
||||
|
||||
apr_pool_create(&memory_manager.memory_pool, NULL);
|
||||
assert(memory_manager.memory_pool != NULL);
|
||||
switch_mutex_init(&memory_manager.mem_lock, SWITCH_MUTEX_NESTED, memory_manager.memory_pool);
|
||||
switch_queue_create(&memory_manager.pool_queue, 50000, memory_manager.memory_pool);
|
||||
|
||||
switch_threadattr_create(&thd_attr, memory_manager.memory_pool);
|
||||
switch_threadattr_detach_set(thd_attr, 1);
|
||||
|
||||
switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
|
||||
switch_thread_create(&thread, thd_attr, pool_thread, NULL, memory_manager.memory_pool);
|
||||
|
||||
while (!memory_manager.pool_thread_running) {
|
||||
switch_yield(1000);
|
||||
}
|
||||
|
||||
return memory_manager.memory_pool;
|
||||
}
|
||||
|
Reference in New Issue
Block a user