Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

switch_core_memory.c File Reference


Functions

switch_memory_pool_tswitch_core_session_get_pool (switch_core_session_t *session)
void * switch_core_perform_session_alloc (switch_core_session_t *session, switch_size_t memory, const char *file, const char *func, int line)
void * switch_core_perform_permanent_alloc (switch_size_t memory, const char *file, const char *func, int line)
char * switch_core_perform_permanent_strdup (const char *todup, const char *file, const char *func, int line)
char * switch_core_session_sprintf (switch_core_session_t *session, const char *fmt,...)
char * switch_core_sprintf (switch_memory_pool_t *pool, const char *fmt,...)
char * switch_core_perform_session_strdup (switch_core_session_t *session, const char *todup, const char *file, const char *func, int line)
char * switch_core_perform_strdup (switch_memory_pool_t *pool, const char *todup, const char *file, const char *func, int line)
void switch_core_memory_pool_tag (switch_memory_pool_t *pool, const char *tag)
switch_status_t switch_core_perform_new_memory_pool (switch_memory_pool_t **pool, const char *file, const char *func, int line)
switch_status_t switch_core_perform_destroy_memory_pool (switch_memory_pool_t **pool, const char *file, const char *func, int line)
void * switch_core_perform_alloc (switch_memory_pool_t *pool, switch_size_t memory, const char *file, const char *func, int line)
void switch_core_memory_reclaim (void)
void switch_core_memory_stop (void)
switch_memory_pool_tswitch_core_memory_init (void)


Function Documentation

switch_memory_pool_t* switch_core_memory_init void   ) 
 

00437 {
00438         switch_thread_t *thread;
00439     switch_threadattr_t *thd_attr;
00440 #ifdef PER_POOL_LOCK
00441         apr_allocator_t *my_allocator = NULL;
00442         apr_thread_mutex_t *my_mutex;
00443 #endif
00444 
00445         memset(&memory_manager, 0, sizeof(memory_manager));
00446 
00447 #ifdef PER_POOL_LOCK
00448                 if ((apr_allocator_create(&my_allocator)) != APR_SUCCESS) {
00449                         abort();
00450                 }
00451 
00452                 if ((apr_pool_create_ex(&memory_manager.memory_pool, NULL, NULL, my_allocator)) != APR_SUCCESS) {
00453                         apr_allocator_destroy(my_allocator);
00454                         my_allocator = NULL;
00455                         abort();
00456                 }
00457 
00458                 if ((apr_thread_mutex_create(&my_mutex, APR_THREAD_MUTEX_DEFAULT, memory_manager.memory_pool)) != APR_SUCCESS) {
00459                         abort();
00460                 }
00461 
00462                 apr_allocator_mutex_set(my_allocator, my_mutex);
00463                 apr_allocator_owner_set(my_allocator, memory_manager.memory_pool);
00464 #else
00465         apr_pool_create(&memory_manager.memory_pool, NULL);
00466         switch_assert(memory_manager.memory_pool != NULL);
00467 #endif
00468 
00469         switch_mutex_init(&memory_manager.mem_lock, SWITCH_MUTEX_NESTED, memory_manager.memory_pool);
00470         switch_queue_create(&memory_manager.pool_queue, 50000, memory_manager.memory_pool);
00471         switch_queue_create(&memory_manager.pool_recycle_queue, 50000, memory_manager.memory_pool);
00472 
00473         switch_threadattr_create(&thd_attr, memory_manager.memory_pool);
00474         switch_threadattr_detach_set(thd_attr, 1);
00475 
00476         switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
00477         switch_thread_create(&thread, thd_attr, pool_thread, NULL, memory_manager.memory_pool);
00478         
00479         while (!memory_manager.pool_thread_running) {
00480                 switch_yield(1000);
00481         }
00482 
00483         return memory_manager.memory_pool;
00484 }

Here is the call graph for this function:

void switch_core_memory_stop void   ) 
 

00428 {
00429         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Stopping memory pool queue.\n");
00430         memory_manager.pool_thread_running = -1;
00431         while(memory_manager.pool_thread_running) {
00432                 switch_yield(1000);
00433         }
00434 }

Here is the call graph for this function:

void* switch_core_perform_alloc switch_memory_pool_t pool,
switch_size_t  memory,
const char *  file,
const char *  func,
int  line
 

00330 {
00331         void *ptr = NULL;
00332 
00333         switch_assert(pool != NULL);
00334 
00335 #ifdef LOCK_MORE
00336         switch_mutex_lock(memory_manager.mem_lock);
00337 #endif
00338 
00339 #ifdef DEBUG_ALLOC
00340         if (memory > 500)
00341                 switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "Core Allocate %d\n", (int)memory);
00342         /*switch_assert(memory < 20000);*/
00343 #endif
00344 
00345         ptr = apr_palloc(pool, memory);
00346         switch_assert(ptr != NULL);
00347         memset(ptr, 0, memory);
00348 
00349 #ifdef LOCK_MORE
00350         switch_mutex_unlock(memory_manager.mem_lock);
00351 #endif
00352 
00353         return ptr;
00354 }

Here is the call graph for this function:

switch_status_t switch_core_perform_destroy_memory_pool switch_memory_pool_t **  pool,
const char *  file,
const char *  func,
int  line
 

00314 {
00315         switch_assert(pool != NULL);
00316 
00317 #ifdef DEBUG_ALLOC2
00318         switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "Free Pool\n");
00319 #endif
00320 
00321         if ((memory_manager.pool_thread_running != 1) || (switch_queue_push(memory_manager.pool_queue, *pool) != SWITCH_STATUS_SUCCESS)) {
00322                 apr_pool_destroy(*pool);
00323         }
00324         *pool = NULL;
00325         
00326         return SWITCH_STATUS_SUCCESS;
00327 }

Here is the call graph for this function:

switch_status_t switch_core_perform_new_memory_pool switch_memory_pool_t **  pool,
const char *  file,
const char *  func,
int  line
 

00262 {
00263         char *tmp;
00264 #ifdef PER_POOL_LOCK
00265                 apr_allocator_t *my_allocator = NULL;
00266         apr_thread_mutex_t *my_mutex;
00267 #else
00268                 void *pop = NULL;
00269 #endif
00270 
00271         switch_mutex_lock(memory_manager.mem_lock);
00272         switch_assert(pool != NULL);
00273 
00274 #ifndef PER_POOL_LOCK
00275         if (switch_queue_trypop(memory_manager.pool_recycle_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
00276                 *pool = (switch_memory_pool_t *) pop;
00277         } else {
00278 #endif
00279 
00280 #ifdef PER_POOL_LOCK
00281                 if ((apr_allocator_create(&my_allocator)) != APR_SUCCESS) {
00282                         return SWITCH_STATUS_MEMERR;
00283                 }
00284 
00285                 if ((apr_pool_create_ex(pool, NULL, NULL, my_allocator)) != APR_SUCCESS) {
00286                         apr_allocator_destroy(my_allocator);
00287                         my_allocator = NULL;
00288                         return SWITCH_STATUS_MEMERR;
00289                 }
00290 
00291                 if ((apr_thread_mutex_create(&my_mutex, APR_THREAD_MUTEX_DEFAULT, *pool)) != APR_SUCCESS) {
00292                         return SWITCH_STATUS_MEMERR;
00293                 }
00294 
00295                 apr_allocator_mutex_set(my_allocator, my_mutex);
00296                 apr_allocator_owner_set(my_allocator, *pool);
00297 #else
00298                 apr_pool_create(pool, NULL);
00299                 switch_assert(*pool != NULL);
00300         }
00301 #endif
00302 
00303 #ifdef DEBUG_ALLOC2
00304         switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "New Pool\n");
00305 #endif
00306         tmp = switch_core_sprintf(*pool, "%s:%d", func, line);
00307         apr_pool_tag(*pool, tmp);
00308         switch_mutex_unlock(memory_manager.mem_lock);
00309 
00310         return SWITCH_STATUS_SUCCESS;
00311 }

Here is the call graph for this function:

void* switch_core_perform_permanent_alloc switch_size_t  memory,
const char *  file,
const char *  func,
int  line
 

00092 {
00093         void *ptr = NULL;
00094         switch_assert(memory_manager.memory_pool != NULL);
00095 
00096 #ifdef LOCK_MORE
00097         switch_mutex_lock(memory_manager.mem_lock);
00098 #endif
00099 
00100 #ifdef DEBUG_ALLOC
00101         switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "Perm Allocate %d\n", (int)memory);
00102 #endif
00103 
00104         ptr = apr_palloc(memory_manager.memory_pool, memory);
00105 
00106         switch_assert(ptr != NULL);
00107         memset(ptr, 0, memory);
00108 
00109 #ifdef LOCK_MORE
00110         switch_mutex_unlock(memory_manager.mem_lock);
00111 #endif
00112 
00113         return ptr;
00114 }

Here is the call graph for this function:

char* switch_core_perform_permanent_strdup const char *  todup,
const char *  file,
const char *  func,
int  line
 

00117 {
00118         char *duped = NULL;
00119         switch_size_t len;
00120         switch_assert(memory_manager.memory_pool != NULL);
00121 
00122         if (!todup)
00123                 return NULL;
00124 
00125 #ifdef LOCK_MORE
00126         switch_mutex_lock(memory_manager.mem_lock);
00127 #endif
00128 
00129         len = strlen(todup) + 1;
00130         duped = apr_pstrmemdup(memory_manager.memory_pool, todup, len);
00131         switch_assert(duped != NULL);
00132 
00133 #ifdef DEBUG_ALLOC
00134         switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "Perm Allocate %d\n", (int)len);
00135 #endif
00136 
00137 #ifdef LOCK_MORE
00138         switch_mutex_unlock(memory_manager.mem_lock);
00139 #endif
00140 
00141         return duped;
00142 }

Here is the call graph for this function:

void* switch_core_perform_session_alloc switch_core_session_t session,
switch_size_t  memory,
const char *  file,
const char *  func,
int  line
 

00062 {
00063         void *ptr = NULL;
00064         switch_assert(session != NULL);
00065         switch_assert(session->pool != NULL);
00066 
00067 #ifdef LOCK_MORE
00068         switch_mutex_lock(memory_manager.mem_lock);
00069 #endif
00070 
00071 #ifdef DEBUG_ALLOC
00072         if (memory > 500)
00073                 switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "Session Allocate %d\n", (int)memory);
00074 #endif
00075 
00076         ptr = apr_palloc(session->pool, memory);
00077         switch_assert(ptr != NULL);
00078 
00079         memset(ptr, 0, memory);
00080         
00081 #ifdef LOCK_MORE
00082         switch_mutex_unlock(memory_manager.mem_lock);
00083 #endif
00084 
00085         return ptr;
00086 }

Here is the call graph for this function:

char* switch_core_perform_session_strdup switch_core_session_t session,
const char *  todup,
const char *  file,
const char *  func,
int  line
 

00193 {
00194         char *duped = NULL;
00195         switch_size_t len;
00196         switch_assert(session != NULL);
00197         switch_assert(session->pool != NULL);
00198 
00199         if (!todup) {
00200                 return NULL;
00201         }
00202 
00203 #ifdef LOCK_MORE
00204         switch_mutex_lock(memory_manager.mem_lock);
00205 #endif
00206 
00207         len = strlen(todup) + 1;
00208 
00209 #ifdef DEBUG_ALLOC
00210         if (len > 500)
00211                 switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "Sess Strdup Allocate %d\n", (int)len);
00212 #endif
00213 
00214         duped = apr_pstrmemdup(session->pool, todup, len);
00215         switch_assert(duped != NULL);
00216 
00217 
00218 #ifdef LOCK_MORE
00219         switch_mutex_unlock(memory_manager.mem_lock);
00220 #endif
00221 
00222         return duped;
00223 }

Here is the call graph for this function:

char* switch_core_perform_strdup switch_memory_pool_t pool,
const char *  todup,
const char *  file,
const char *  func,
int  line
 

00226 {
00227         char *duped = NULL;
00228         switch_size_t len;
00229         switch_assert(pool != NULL);
00230 
00231         if (!todup) {
00232                 return NULL;
00233         }
00234 
00235 #ifdef LOCK_MORE
00236         switch_mutex_lock(memory_manager.mem_lock);
00237 #endif
00238 
00239         len = strlen(todup) + 1;
00240 
00241 #ifdef DEBUG_ALLOC
00242         if (len > 500)
00243                 switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "core strdup Allocate %d\n", (int)len);
00244 #endif
00245 
00246         duped = apr_pstrmemdup(pool, todup, len);
00247         switch_assert(duped != NULL);
00248 
00249 #ifdef LOCK_MORE
00250         switch_mutex_unlock(memory_manager.mem_lock);
00251 #endif
00252 
00253         return duped;
00254 }

Here is the call graph for this function:

switch_memory_pool_t* switch_core_session_get_pool switch_core_session_t session  ) 
 

00053 {
00054         switch_assert(session != NULL);
00055         switch_assert(session->pool != NULL);
00056         return session->pool;
00057 }

char* switch_core_session_sprintf switch_core_session_t session,
const char *  fmt,
  ...
 

00145 {
00146         va_list ap;
00147         char *result = NULL;
00148 
00149 #ifdef LOCK_MORE
00150         switch_mutex_lock(memory_manager.mem_lock);
00151 #endif
00152 
00153         switch_assert(session != NULL);
00154         switch_assert(session->pool != NULL);
00155         va_start(ap, fmt);
00156         
00157         result = apr_pvsprintf(session->pool, fmt, ap);
00158         switch_assert(result != NULL);
00159         va_end(ap);
00160 
00161 #ifdef LOCK_MORE
00162         switch_mutex_unlock(memory_manager.mem_lock);
00163 #endif
00164 
00165         return result;
00166 }

Here is the call graph for this function:

char* switch_core_sprintf switch_memory_pool_t pool,
const char *  fmt,
  ...
 

00169 {
00170         va_list ap;
00171         char *result = NULL;
00172 
00173         switch_assert(pool != NULL);
00174 
00175 #ifdef LOCK_MORE
00176         switch_mutex_lock(memory_manager.mem_lock);
00177 #endif
00178 
00179         va_start(ap, fmt);
00180 
00181         result = apr_pvsprintf(pool, fmt, ap);
00182         switch_assert(result != NULL);
00183         va_end(ap);
00184 
00185 #ifdef LOCK_MORE
00186         switch_mutex_unlock(memory_manager.mem_lock);
00187 #endif
00188 
00189         return result;
00190 }

Here is the call graph for this function:


Variable Documentation

switch_mutex_t* mem_lock
 

switch_memory_pool_t* memory_pool
 

switch_queue_t* pool_queue
 

switch_queue_t* pool_recycle_queue
 

int pool_thread_running
 


Generated on Mon May 26 22:06:52 2008 for FreeSWITCH by  doxygen 1.3.9.1