]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add pool recycling
authorAnthony Minessale <anthony.minessale@gmail.com>
Mon, 1 Oct 2007 14:14:15 +0000 (14:14 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Mon, 1 Oct 2007 14:14:15 +0000 (14:14 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@5769 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/switch_core_memory.c

index 9aefc01cb51062c1ce0b443ea9d39210080e0f08..426737573f34c85ffe1b0c227b2e88a0b11f05f3 100644 (file)
@@ -38,6 +38,7 @@
 static struct {
        switch_mutex_t *mem_lock;
        switch_queue_t *pool_queue; /* 8 ball break */
+       switch_queue_t *pool_recycle_queue;
        switch_memory_pool_t *memory_pool;
        int pool_thread_running;
 } memory_manager;
@@ -249,13 +250,18 @@ SWITCH_DECLARE(char *) switch_core_strdup(switch_memory_pool_t *pool, const char
 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;
+       void *pop;
 
        switch_mutex_lock(memory_manager.mem_lock);
        assert(pool != NULL);
-       
-       apr_pool_create(pool, NULL);
-       assert(*pool != NULL);
-       
+
+       if (switch_queue_trypop(memory_manager.pool_recycle_queue, &pop) == SWITCH_STATUS_SUCCESS) {
+               *pool = (switch_memory_pool_t *) pop;
+       } else {
+               apr_pool_create(pool, NULL);
+               assert(*pool != NULL);
+       }
+
 #ifdef DEBUG_ALLOC2
        printf("New Pool %s %s:%d\n", file, func, line);
 #endif
@@ -340,7 +346,8 @@ static void *SWITCH_THREAD_FUNC pool_thread(switch_thread_t * thread, void *obj)
                                }
                                
                                pool = (switch_memory_pool_t *) pop;
-                               apr_pool_destroy(pool);
+                               apr_pool_clear(pool);
+                               switch_queue_push(memory_manager.pool_recycle_queue, pool);
                                pool = NULL;
                                x--;
                        }
@@ -354,6 +361,25 @@ static void *SWITCH_THREAD_FUNC pool_thread(switch_thread_t * thread, void *obj)
        }
 
  done:
+       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Returning %d recycled memory pool(s)\n", 
+                                         switch_queue_size(memory_manager.pool_recycle_queue) + switch_queue_size(memory_manager.pool_queue));
+
+       while (switch_queue_trypop(memory_manager.pool_recycle_queue, &pop) == SWITCH_STATUS_SUCCESS) {
+               pool = (switch_memory_pool_t *) pop;
+               if (!pool) {
+                       break;
+               }
+               apr_pool_destroy(pool);
+       }
+
+       while (switch_queue_trypop(memory_manager.pool_queue, &pop) == SWITCH_STATUS_SUCCESS) {
+               pool = (switch_memory_pool_t *) pop;
+               if (!pool) {
+                       break;
+               }
+               apr_pool_destroy(pool);
+       }
+
        memory_manager.pool_thread_running = 0;
        return NULL;
 }
@@ -380,6 +406,7 @@ switch_memory_pool_t *switch_core_memory_init(void)
        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_queue_create(&memory_manager.pool_recycle_queue, 50000, memory_manager.memory_pool);
 
        switch_threadattr_create(&thd_attr, memory_manager.memory_pool);
        switch_threadattr_detach_set(thd_attr, 1);