FreeSWITCH API Documentation  1.7.0
Macros | Functions | Variables
switch_core_memory.c File Reference
#include <switch.h>
#include "private/switch_core_pvt.h"
+ Include dependency graph for switch_core_memory.c:

Go to the source code of this file.

Macros

#define PER_POOL_LOCK   1
 

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_session_vsprintf (switch_core_session_t *session, const char *fmt, va_list ap)
 printf-style style printing routine. The data is output to a string allocated from the session More...
 
char * switch_core_vsprintf (switch_memory_pool_t *pool, const char *fmt, va_list ap)
 
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_set_data (switch_memory_pool_t *pool, const char *key, void *data)
 
void * switch_core_memory_pool_get_data (switch_memory_pool_t *pool, const char *key)
 
void switch_core_memory_pool_tag (switch_memory_pool_t *pool, const char *tag)
 
void switch_pool_clear (switch_memory_pool_t *p)
 
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)
 
static void *SWITCH_THREAD_FUNC pool_thread (switch_thread_t *thread, void *obj)
 
void switch_core_memory_stop (void)
 
switch_memory_pool_tswitch_core_memory_init (void)
 

Variables

struct {
   switch_queue_t *   pool_queue
 
   switch_queue_t *   pool_recycle_queue
 
   switch_memory_pool_t *   memory_pool
 
   int   pool_thread_running
 
memory_manager
 
static switch_thread_tpool_thread_p = NULL
 

Macro Definition Documentation

#define PER_POOL_LOCK   1

Definition at line 46 of file switch_core_memory.c.

Function Documentation

static void* SWITCH_THREAD_FUNC pool_thread ( switch_thread_t thread,
void *  obj 
)
static

Definition at line 504 of file switch_core_memory.c.

References memory_manager, SWITCH_CHANNEL_LOG, switch_core_memory_reclaim(), SWITCH_LOG_CONSOLE, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_queue_pop(), switch_queue_size(), switch_queue_trypop(), switch_queue_trypush(), SWITCH_STATUS_SUCCESS, and switch_yield.

Referenced by switch_core_memory_init().

505 {
506  memory_manager.pool_thread_running = 1;
507 
508  while (memory_manager.pool_thread_running == 1) {
509  int len = switch_queue_size(memory_manager.pool_queue);
510 
511  if (len) {
512  int x = len, done = 0;
513 
514  switch_yield(1000000);
515 #ifdef USE_MEM_LOCK
517 #endif
518  while (x > 0) {
519  void *pop = NULL;
520  if (switch_queue_pop(memory_manager.pool_queue, &pop) != SWITCH_STATUS_SUCCESS || !pop) {
521  done = 1;
522  break;
523  }
524 #if defined(PER_POOL_LOCK) || defined(DESTROY_POOLS)
525 #ifdef USE_MEM_LOCK
527 #endif
528 
529 #ifdef DEBUG_ALLOC
530  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "%p DESTROY POOL\n", (void *) pop);
531 #endif
532  apr_pool_destroy(pop);
533 #ifdef USE_MEM_LOCK
535 #endif
536 #else
537  apr_pool_mutex_set(pop, NULL);
538 #ifdef DEBUG_ALLOC
539  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "%p DESTROY POOL\n", (void *) pop);
540 #endif
541  apr_pool_clear(pop);
542  if (switch_queue_trypush(memory_manager.pool_recycle_queue, pop) != SWITCH_STATUS_SUCCESS) {
543 #ifdef USE_MEM_LOCK
545 #endif
546 #ifdef DEBUG_ALLOC
547  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "%p DESTROY POOL\n", (void *) pop);
548 #endif
549  apr_pool_destroy(pop);
550 #ifdef USE_MEM_LOCK
552 #endif
553 
554  }
555 #endif
556  x--;
557  }
558 #ifdef USE_MEM_LOCK
560 #endif
561  if (done) {
562  goto done;
563  }
564  } else {
565  switch_yield(1000000);
566  }
567  }
568 
569  done:
571 
572  {
573  void *pop = NULL;
574  while (switch_queue_trypop(memory_manager.pool_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
575 #ifdef USE_MEM_LOCK
577 #endif
578  apr_pool_destroy(pop);
579  pop = NULL;
580 #ifdef USE_MEM_LOCK
582 #endif
583 
584  }
585  }
586 
587  memory_manager.pool_thread_running = 0;
588 
589  return NULL;
590 }
unsigned int switch_queue_size(switch_queue_t *queue)
Definition: switch_apr.c:1114
#define SWITCH_CHANNEL_LOG
switch_status_t switch_queue_trypop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1140
switch_status_t switch_queue_pop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1119
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:908
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
static struct @2 memory_manager
switch_status_t switch_queue_trypush(switch_queue_t *queue, void *data)
Definition: switch_apr.c:1155
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
void switch_core_memory_reclaim(void)
switch_memory_pool_t* switch_core_memory_init ( void  )

Definition at line 614 of file switch_core_memory.c.

References memory_manager, memset(), pool_thread(), pool_thread_p, switch_assert, switch_cond_next(), switch_mutex_init(), SWITCH_MUTEX_NESTED, switch_queue_create(), switch_thread_create(), SWITCH_THREAD_STACKSIZE, switch_threadattr_create(), and switch_threadattr_stacksize_set().

Referenced by switch_core_init().

615 {
616 #ifndef INSTANTLY_DESTROY_POOLS
617  switch_threadattr_t *thd_attr;
618 #endif
619 #ifdef PER_POOL_LOCK
620  apr_allocator_t *my_allocator = NULL;
621  apr_thread_mutex_t *my_mutex;
622 #endif
623 
624  memset(&memory_manager, 0, sizeof(memory_manager));
625 
626 #ifdef PER_POOL_LOCK
627  if ((apr_allocator_create(&my_allocator)) != APR_SUCCESS) {
628  abort();
629  }
630 
631  if ((apr_pool_create_ex(&memory_manager.memory_pool, NULL, NULL, my_allocator)) != APR_SUCCESS) {
632  apr_allocator_destroy(my_allocator);
633  my_allocator = NULL;
634  abort();
635  }
636 
637  if ((apr_thread_mutex_create(&my_mutex, APR_THREAD_MUTEX_NESTED, memory_manager.memory_pool)) != APR_SUCCESS) {
638  abort();
639  }
640 
641  apr_allocator_mutex_set(my_allocator, my_mutex);
642  apr_pool_mutex_set(memory_manager.memory_pool, my_mutex);
643  apr_allocator_owner_set(my_allocator, memory_manager.memory_pool);
644  apr_pool_tag(memory_manager.memory_pool, "core_pool");
645 #else
646  apr_pool_create(&memory_manager.memory_pool, NULL);
647  switch_assert(memory_manager.memory_pool != NULL);
648 #endif
649 
650 #ifdef USE_MEM_LOCK
652 #endif
653 
654 #ifdef INSTANTLY_DESTROY_POOLS
655  {
656  void *foo;
657  foo = (void *) (intptr_t) pool_thread;
658  }
659 #else
660 
661  switch_queue_create(&memory_manager.pool_queue, 50000, memory_manager.memory_pool);
662  switch_queue_create(&memory_manager.pool_recycle_queue, 50000, memory_manager.memory_pool);
663 
664  switch_threadattr_create(&thd_attr, memory_manager.memory_pool);
665 
667  switch_thread_create(&pool_thread_p, thd_attr, pool_thread, NULL, memory_manager.memory_pool);
668 
669  while (!memory_manager.pool_thread_running) {
671  }
672 #endif
673 
674  return memory_manager.memory_pool;
675 }
switch_status_t switch_threadattr_stacksize_set(switch_threadattr_t *attr, switch_size_t stacksize)
Definition: switch_apr.c:660
static void *SWITCH_THREAD_FUNC pool_thread(switch_thread_t *thread, void *obj)
#define SWITCH_MUTEX_NESTED
Definition: switch_apr.h:318
#define SWITCH_THREAD_STACKSIZE
Definition: switch_types.h:551
static struct @2 memory_manager
switch_status_t switch_mutex_init(switch_mutex_t **lock, unsigned int flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:270
void switch_cond_next(void)
Definition: switch_time.c:638
static switch_thread_t * pool_thread_p
switch_status_t switch_threadattr_create(switch_threadattr_t **new_attr, switch_memory_pool_t *pool)
Definition: switch_apr.c:642
switch_status_t switch_thread_create(switch_thread_t **new_thread, switch_threadattr_t *attr, switch_thread_start_t func, void *data, switch_memory_pool_t *cont)
Definition: switch_apr.c:675
switch_status_t switch_queue_create(switch_queue_t **queue, unsigned int queue_capacity, switch_memory_pool_t *pool)
Definition: switch_apr.c:1109
#define switch_assert(expr)
memset(buf, 0, buflen)
void switch_core_memory_stop ( void  )

Definition at line 596 of file switch_core_memory.c.

References memory_manager, pool_thread_p, SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, switch_log_printf(), switch_queue_trypop(), SWITCH_STATUS_SUCCESS, and switch_thread_join().

Referenced by switch_core_destroy().

597 {
598 #ifndef INSTANTLY_DESTROY_POOLS
599  switch_status_t st;
600  void *pop = NULL;
601 
602  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Stopping memory pool queue.\n");
603 
604  memory_manager.pool_thread_running = 0;
606 
607 
608  while (switch_queue_trypop(memory_manager.pool_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
609  apr_pool_destroy(pop);
610  }
611 #endif
612 }
#define SWITCH_CHANNEL_LOG
switch_status_t switch_queue_trypop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1140
switch_status_t switch_thread_join(switch_status_t *retval, switch_thread_t *thd)
Definition: switch_apr.c:1255
static struct @2 memory_manager
static switch_thread_t * pool_thread_p
switch_status_t
Common return values.
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
void* switch_core_perform_alloc ( switch_memory_pool_t pool,
switch_size_t  memory,
const char *  file,
const char *  func,
int  line 
)

Definition at line 447 of file switch_core_memory.c.

References memory_manager, memset(), switch_assert, SWITCH_CHANNEL_ID_LOG, SWITCH_LOG_CONSOLE, switch_log_printf(), switch_mutex_lock(), and switch_mutex_unlock().

448 {
449  void *ptr = NULL;
450 
451  switch_assert(pool != NULL);
452 
453 #ifdef LOCK_MORE
454 #ifdef USE_MEM_LOCK
456 #endif
457 #endif
458 
459 #ifdef DEBUG_ALLOC
460  if (memory > 500)
461  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p Core Allocate %s %d\n",
462  (void *) pool, apr_pool_tag(pool, NULL), (int) memory);
463  /*switch_assert(memory < 20000); */
464 #endif
465 
466  ptr = apr_palloc(pool, memory);
467  switch_assert(ptr != NULL);
468  memset(ptr, 0, memory);
469 
470 #ifdef LOCK_MORE
471 #ifdef USE_MEM_LOCK
473 #endif
474 #endif
475 
476  return ptr;
477 }
switch_memory_pool_t * pool
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
static struct @2 memory_manager
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define switch_assert(expr)
memset(buf, 0, buflen)
switch_status_t switch_core_perform_destroy_memory_pool ( switch_memory_pool_t **  pool,
const char *  file,
const char *  func,
int  line 
)

Definition at line 414 of file switch_core_memory.c.

References memory_manager, switch_assert, SWITCH_CHANNEL_ID_LOG, SWITCH_LOG_CONSOLE, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_queue_push(), and SWITCH_STATUS_SUCCESS.

415 {
416  switch_assert(pool != NULL);
417 
418 #ifdef DEBUG_ALLOC2
419  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p Free Pool %s\n", (void *) *pool, apr_pool_tag(*pool, NULL));
420 #endif
421 
422 #ifdef INSTANTLY_DESTROY_POOLS
423 #ifdef USE_MEM_LOCK
425 #endif
426  apr_pool_destroy(*pool);
427 #ifdef USE_MEM_LOCK
429 #endif
430 #else
431  if ((memory_manager.pool_thread_running != 1) || (switch_queue_push(memory_manager.pool_queue, *pool) != SWITCH_STATUS_SUCCESS)) {
432 #ifdef USE_MEM_LOCK
434 #endif
435  apr_pool_destroy(*pool);
436 #ifdef USE_MEM_LOCK
438 #endif
439  }
440 #endif
441 
442  *pool = NULL;
443 
444  return SWITCH_STATUS_SUCCESS;
445 }
switch_memory_pool_t * pool
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
static struct @2 memory_manager
switch_status_t switch_queue_push(switch_queue_t *queue, void *data)
Definition: switch_apr.c:1129
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define switch_assert(expr)
switch_status_t switch_core_perform_new_memory_pool ( switch_memory_pool_t **  pool,
const char *  file,
const char *  func,
int  line 
)

Definition at line 348 of file switch_core_memory.c.

References memory_manager, switch_assert, SWITCH_CHANNEL_ID_LOG, switch_core_sprintf(), SWITCH_LOG_CONSOLE, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_queue_trypop(), and SWITCH_STATUS_SUCCESS.

349 {
350  char *tmp;
351 #ifdef INSTANTLY_DESTROY_POOLS
352  apr_pool_create(pool, NULL);
353  switch_assert(*pool != NULL);
354 #else
355 
356 #ifdef PER_POOL_LOCK
357  apr_allocator_t *my_allocator = NULL;
358  apr_thread_mutex_t *my_mutex;
359 #else
360  void *pop = NULL;
361 #endif
362 
363 #ifdef USE_MEM_LOCK
365 #endif
366  switch_assert(pool != NULL);
367 
368 #ifndef PER_POOL_LOCK
369  if (switch_queue_trypop(memory_manager.pool_recycle_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
370  *pool = (switch_memory_pool_t *) pop;
371  } else {
372 #endif
373 
374 #ifdef PER_POOL_LOCK
375  if ((apr_allocator_create(&my_allocator)) != APR_SUCCESS) {
376  abort();
377  }
378 
379  if ((apr_pool_create_ex(pool, NULL, NULL, my_allocator)) != APR_SUCCESS) {
380  abort();
381  }
382 
383  if ((apr_thread_mutex_create(&my_mutex, APR_THREAD_MUTEX_NESTED, *pool)) != APR_SUCCESS) {
384  abort();
385  }
386 
387  apr_allocator_mutex_set(my_allocator, my_mutex);
388  apr_allocator_owner_set(my_allocator, *pool);
389 
390  apr_pool_mutex_set(*pool, my_mutex);
391 
392 #else
393  apr_pool_create(pool, NULL);
394  switch_assert(*pool != NULL);
395  }
396 #endif
397 #endif
398 
399  tmp = switch_core_sprintf(*pool, "%s:%d", file, line);
400  apr_pool_tag(*pool, tmp);
401 
402 #ifdef DEBUG_ALLOC2
403  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p New Pool %s\n", (void *) *pool, apr_pool_tag(*pool, NULL));
404 #endif
405 
406 
407 #ifdef USE_MEM_LOCK
409 #endif
410 
411  return SWITCH_STATUS_SUCCESS;
412 }
switch_memory_pool_t * pool
char * switch_core_sprintf(switch_memory_pool_t *pool, const char *fmt,...)
switch_status_t switch_queue_trypop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1140
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
static struct @2 memory_manager
struct apr_pool_t switch_memory_pool_t
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define switch_assert(expr)
void* switch_core_perform_permanent_alloc ( switch_size_t  memory,
const char *  file,
const char *  func,
int  line 
)

Definition at line 104 of file switch_core_memory.c.

References memory_manager, memset(), switch_assert, SWITCH_CHANNEL_ID_LOG, SWITCH_LOG_CONSOLE, switch_log_printf(), switch_mutex_lock(), and switch_mutex_unlock().

105 {
106  void *ptr = NULL;
107  switch_assert(memory_manager.memory_pool != NULL);
108 
109 #ifdef LOCK_MORE
110 #ifdef USE_MEM_LOCK
112 #endif
113 #endif
114 
115 #ifdef DEBUG_ALLOC
116  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p Perm Allocate %s %d\n",
117  (void *)memory_manager.memory_pool, apr_pool_tag(memory_manager.memory_pool, NULL), (int) memory);
118 #endif
119 
120  ptr = apr_palloc(memory_manager.memory_pool, memory);
121 
122  switch_assert(ptr != NULL);
123  memset(ptr, 0, memory);
124 
125 #ifdef LOCK_MORE
126 #ifdef USE_MEM_LOCK
128 #endif
129 #endif
130 
131  return ptr;
132 }
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
static struct @2 memory_manager
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define switch_assert(expr)
memset(buf, 0, buflen)
char* switch_core_perform_permanent_strdup ( const char *  todup,
const char *  file,
const char *  func,
int  line 
)

Definition at line 134 of file switch_core_memory.c.

References memory_manager, switch_assert, SWITCH_BLANK_STRING, SWITCH_CHANNEL_ID_LOG, SWITCH_LOG_CONSOLE, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), and zstr.

135 {
136  char *duped = NULL;
137  switch_size_t len;
138  switch_assert(memory_manager.memory_pool != NULL);
139 
140  if (!todup) {
141  return NULL;
142  }
143 
144  if (zstr(todup)) {
145  return SWITCH_BLANK_STRING;
146  }
147 #ifdef LOCK_MORE
148 #ifdef USE_MEM_LOCK
150 #endif
151 #endif
152 
153  len = strlen(todup) + 1;
154  duped = apr_pstrmemdup(memory_manager.memory_pool, todup, len);
155  switch_assert(duped != NULL);
156 
157 #ifdef DEBUG_ALLOC
158  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p Perm Allocate %s %d\n",
159  (void *) memory_manager.memory_pool, apr_pool_tag(memory_manager.memory_pool, NULL), (int) len);
160 #endif
161 
162 #ifdef LOCK_MORE
163 #ifdef USE_MEM_LOCK
165 #endif
166 #endif
167 
168  return duped;
169 }
#define zstr(x)
Definition: switch_utils.h:281
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
static struct @2 memory_manager
#define SWITCH_BLANK_STRING
Definition: switch_types.h:47
uintptr_t switch_size_t
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define switch_assert(expr)
void* switch_core_perform_session_alloc ( switch_core_session_t session,
switch_size_t  memory,
const char *  file,
const char *  func,
int  line 
)

Definition at line 68 of file switch_core_memory.c.

References memory_manager, memset(), switch_assert, SWITCH_CHANNEL_ID_LOG, SWITCH_LOG_CONSOLE, switch_log_printf(), switch_mutex_lock(), and switch_mutex_unlock().

70 {
71  void *ptr = NULL;
72  switch_assert(session != NULL);
73  switch_assert(session->pool != NULL);
74 
75 #ifdef LOCK_MORE
76 #ifdef USE_MEM_LOCK
78 #endif
79 #endif
80 
81 #ifdef DEBUG_ALLOC
82  if (memory > 500)
83  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p %p Session Allocate %s %d\n",
84  (void *) session->pool, (void *) session, apr_pool_tag(session->pool, NULL), (int) memory);
85 #endif
86 
87  ptr = apr_palloc(session->pool, memory);
88  switch_assert(ptr != NULL);
89 
90  memset(ptr, 0, memory);
91 
92 #ifdef LOCK_MORE
93 #ifdef USE_MEM_LOCK
95 #endif
96 #endif
97 
98  return ptr;
99 }
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
static struct @2 memory_manager
switch_memory_pool_t * pool
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define switch_assert(expr)
memset(buf, 0, buflen)
char* switch_core_perform_session_strdup ( switch_core_session_t session,
const char *  todup,
const char *  file,
const char *  func,
int  line 
)

Definition at line 223 of file switch_core_memory.c.

References memory_manager, switch_assert, SWITCH_BLANK_STRING, SWITCH_CHANNEL_ID_LOG, SWITCH_LOG_CONSOLE, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), and zstr.

224 {
225  char *duped = NULL;
226 #ifdef DEBUG_ALLOC
227  switch_size_t len;
228 #endif
229 
230  switch_assert(session != NULL);
231  switch_assert(session->pool != NULL);
232 
233  if (!todup) {
234  return NULL;
235  }
236 
237  if (zstr(todup)) {
238  return SWITCH_BLANK_STRING;
239  }
240 #ifdef LOCK_MORE
241 #ifdef USE_MEM_LOCK
243 #endif
244 #endif
245 
246 
247 
248 #ifdef DEBUG_ALLOC
249  len = strlen(todup);
250  if (len > 500)
251  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p %p Sess Strdup Allocate %s %ld\n",
252  (void *) session->pool, (void *)session, apr_pool_tag(session->pool, NULL), strlen(todup));
253 #endif
254 
255  duped = apr_pstrdup(session->pool, todup);
256  switch_assert(duped != NULL);
257 
258 #ifdef LOCK_MORE
259 #ifdef USE_MEM_LOCK
261 #endif
262 #endif
263 
264  return duped;
265 }
#define zstr(x)
Definition: switch_utils.h:281
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
static struct @2 memory_manager
#define SWITCH_BLANK_STRING
Definition: switch_types.h:47
uintptr_t switch_size_t
switch_memory_pool_t * pool
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define switch_assert(expr)
char* switch_core_perform_strdup ( switch_memory_pool_t pool,
const char *  todup,
const char *  file,
const char *  func,
int  line 
)

Definition at line 267 of file switch_core_memory.c.

References memory_manager, switch_assert, SWITCH_BLANK_STRING, SWITCH_CHANNEL_ID_LOG, SWITCH_LOG_CONSOLE, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), and zstr.

268 {
269  char *duped = NULL;
270  switch_size_t len;
271  switch_assert(pool != NULL);
272 
273  if (!todup) {
274  return NULL;
275  }
276 
277  if (zstr(todup)) {
278  return SWITCH_BLANK_STRING;
279  }
280 #ifdef LOCK_MORE
281 #ifdef USE_MEM_LOCK
283 #endif
284 #endif
285 
286  len = strlen(todup) + 1;
287 
288 #ifdef DEBUG_ALLOC
289  if (len > 500)
290  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p Core Strdup Allocate %s %d\n",
291  (void *) pool, apr_pool_tag(pool, NULL), (int)len);
292 #endif
293 
294  duped = apr_pstrmemdup(pool, todup, len);
295  switch_assert(duped != NULL);
296 
297 #ifdef LOCK_MORE
298 #ifdef USE_MEM_LOCK
300 #endif
301 #endif
302 
303  return duped;
304 }
switch_memory_pool_t * pool
#define zstr(x)
Definition: switch_utils.h:281
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
static struct @2 memory_manager
#define SWITCH_BLANK_STRING
Definition: switch_types.h:47
uintptr_t switch_size_t
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define switch_assert(expr)
switch_memory_pool_t* switch_core_session_get_pool ( switch_core_session_t session)

Definition at line 59 of file switch_core_memory.c.

References switch_assert.

60 {
61  switch_assert(session != NULL);
62  switch_assert(session->pool != NULL);
63  return session->pool;
64 }
switch_memory_pool_t * pool
#define switch_assert(expr)
char* switch_core_session_sprintf ( switch_core_session_t session,
const char *  fmt,
  ... 
)

Definition at line 171 of file switch_core_memory.c.

References switch_core_vsprintf().

172 {
173  va_list ap;
174  char *result = NULL;
175 
176  va_start(ap, fmt);
177  result = switch_core_vsprintf(session->pool, fmt, ap);
178  va_end(ap);
179 
180  return result;
181 }
switch_memory_pool_t * pool
char * switch_core_vsprintf(switch_memory_pool_t *pool, const char *fmt, va_list ap)
char* switch_core_sprintf ( switch_memory_pool_t pool,
const char *  fmt,
  ... 
)

Definition at line 212 of file switch_core_memory.c.

References switch_core_vsprintf().

Referenced by switch_core_perform_new_memory_pool().

213 {
214  va_list ap;
215  char *result;
216  va_start(ap, fmt);
217  result = switch_core_vsprintf(pool, fmt, ap);
218  va_end(ap);
219 
220  return result;
221 }
switch_memory_pool_t * pool
char * switch_core_vsprintf(switch_memory_pool_t *pool, const char *fmt, va_list ap)
char* switch_core_vsprintf ( switch_memory_pool_t pool,
const char *  fmt,
va_list  ap 
)

Definition at line 188 of file switch_core_memory.c.

References memory_manager, switch_assert, switch_mutex_lock(), and switch_mutex_unlock().

Referenced by switch_core_session_sprintf(), switch_core_session_vsprintf(), and switch_core_sprintf().

189 {
190  char *result = NULL;
191 
192  switch_assert(pool != NULL);
193 
194 #ifdef LOCK_MORE
195 #ifdef USE_MEM_LOCK
197 #endif
198 #endif
199 
200  result = apr_pvsprintf(pool, fmt, ap);
201  switch_assert(result != NULL);
202 
203 #ifdef LOCK_MORE
204 #ifdef USE_MEM_LOCK
206 #endif
207 #endif
208 
209  return result;
210 }
switch_memory_pool_t * pool
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
static struct @2 memory_manager
#define switch_assert(expr)

Variable Documentation

struct { ... } memory_manager
switch_memory_pool_t* memory_pool

Definition at line 55 of file switch_core_memory.c.

switch_queue_t* pool_queue

Definition at line 53 of file switch_core_memory.c.

switch_queue_t* pool_recycle_queue

Definition at line 54 of file switch_core_memory.c.

switch_thread_t* pool_thread_p = NULL
static

Definition at line 593 of file switch_core_memory.c.

Referenced by switch_core_memory_init(), and switch_core_memory_stop().

int pool_thread_running

Definition at line 56 of file switch_core_memory.c.