RTS API Documentation  1.10.11
Macros | Functions | Variables
switch_core_memory.c File Reference
#include <switch.h>
#include "private/switch_apr_pvt.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
 
#define DEBUG_ALLOC_CUTOFF   500
 

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)
 
char * switch_core_perform_strndup (switch_memory_pool_t *pool, const char *todup, size_t len, 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)
 
void switch_core_pool_stats (switch_stream_handle_t *stream)
 
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

◆ DEBUG_ALLOC_CUTOFF

#define DEBUG_ALLOC_CUTOFF   500

◆ PER_POOL_LOCK

#define PER_POOL_LOCK   1

Definition at line 48 of file switch_core_memory.c.

Function Documentation

◆ pool_thread()

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

Definition at line 579 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().

580 {
581  memory_manager.pool_thread_running = 1;
582 
583  while (memory_manager.pool_thread_running == 1) {
584  int len = switch_queue_size(memory_manager.pool_queue);
585 
586  if (len) {
587  int x = len, done = 0;
588 
589  switch_yield(1000000);
590 #ifdef USE_MEM_LOCK
592 #endif
593  while (x > 0) {
594  void *pop = NULL;
595  if (switch_queue_pop(memory_manager.pool_queue, &pop) != SWITCH_STATUS_SUCCESS || !pop) {
596  done = 1;
597  break;
598  }
599 #if defined(PER_POOL_LOCK) || defined(DESTROY_POOLS)
600 #ifdef USE_MEM_LOCK
602 #endif
603 
604 #ifdef DEBUG_ALLOC
605  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "%p DESTROY POOL\n", (void *) pop);
606 #endif
607  fspr_pool_destroy(pop);
608 #ifdef USE_MEM_LOCK
610 #endif
611 #else
612  fspr_pool_mutex_set(pop, NULL);
613 #ifdef DEBUG_ALLOC
614  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "%p DESTROY POOL\n", (void *) pop);
615 #endif
616  fspr_pool_clear(pop);
617  if (switch_queue_trypush(memory_manager.pool_recycle_queue, pop) != SWITCH_STATUS_SUCCESS) {
618 #ifdef USE_MEM_LOCK
620 #endif
621 #ifdef DEBUG_ALLOC
622  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "%p DESTROY POOL\n", (void *) pop);
623 #endif
624  fspr_pool_destroy(pop);
625 #ifdef USE_MEM_LOCK
627 #endif
628 
629  }
630 #endif
631  x--;
632  }
633 #ifdef USE_MEM_LOCK
635 #endif
636  if (done) {
637  goto done;
638  }
639  } else {
640  switch_yield(1000000);
641  }
642  }
643 
644  done:
646 
647  {
648  void *pop = NULL;
649  while (switch_queue_trypop(memory_manager.pool_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
650 #ifdef USE_MEM_LOCK
652 #endif
653  fspr_pool_destroy(pop);
654  pop = NULL;
655 #ifdef USE_MEM_LOCK
657 #endif
658 
659  }
660  }
661 
662  memory_manager.pool_thread_running = 0;
663 
664  return NULL;
665 }
unsigned int switch_queue_size(switch_queue_t *queue)
Definition: switch_apr.c:1238
#define SWITCH_CHANNEL_LOG
switch_status_t switch_queue_trypop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1264
static struct @3 memory_manager
switch_status_t switch_queue_pop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1243
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:998
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
switch_status_t switch_queue_trypush(switch_queue_t *queue, void *data)
Definition: switch_apr.c:1279
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_core_memory_init()

switch_memory_pool_t* switch_core_memory_init ( void  )

Definition at line 689 of file switch_core_memory.c.

References memory_manager, memset(), pool_thread(), 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().

690 {
691 #ifndef INSTANTLY_DESTROY_POOLS
692  switch_threadattr_t *thd_attr;
693 #endif
694 #ifdef PER_POOL_LOCK
695  fspr_allocator_t *my_allocator = NULL;
696  fspr_thread_mutex_t *my_mutex;
697 #endif
698 
699  memset(&memory_manager, 0, sizeof(memory_manager));
700 
701 #ifdef PER_POOL_LOCK
702  if ((fspr_allocator_create(&my_allocator)) != APR_SUCCESS) {
703  abort();
704  }
705 
706  if ((fspr_pool_create_ex(&memory_manager.memory_pool, NULL, NULL, my_allocator)) != APR_SUCCESS) {
707  fspr_allocator_destroy(my_allocator);
708  my_allocator = NULL;
709  abort();
710  }
711 
712  if ((fspr_thread_mutex_create(&my_mutex, APR_THREAD_MUTEX_NESTED, memory_manager.memory_pool)) != APR_SUCCESS) {
713  abort();
714  }
715 
716  fspr_allocator_mutex_set(my_allocator, my_mutex);
717  fspr_pool_mutex_set(memory_manager.memory_pool, my_mutex);
718  fspr_allocator_owner_set(my_allocator, memory_manager.memory_pool);
719  fspr_pool_tag(memory_manager.memory_pool, "core_pool");
720 #else
721  fspr_pool_create(&memory_manager.memory_pool, NULL);
722  switch_assert(memory_manager.memory_pool != NULL);
723 #endif
724 
725 #ifdef USE_MEM_LOCK
727 #endif
728 
729 #ifdef INSTANTLY_DESTROY_POOLS
730  {
731  void *foo;
732  foo = (void *) (intptr_t) pool_thread;
733  }
734 #else
735 
736  switch_queue_create(&memory_manager.pool_queue, 50000, memory_manager.memory_pool);
737  switch_queue_create(&memory_manager.pool_recycle_queue, 50000, memory_manager.memory_pool);
738 
739  switch_threadattr_create(&thd_attr, memory_manager.memory_pool);
740 
742  switch_thread_create(&pool_thread_p, thd_attr, pool_thread, NULL, memory_manager.memory_pool);
743 
744  while (!memory_manager.pool_thread_running) {
746  }
747 #endif
748 
749  return memory_manager.memory_pool;
750 }
switch_status_t switch_threadattr_stacksize_set(switch_threadattr_t *attr, switch_size_t stacksize)
Definition: switch_apr.c:683
static void *SWITCH_THREAD_FUNC pool_thread(switch_thread_t *thread, void *obj)
static struct @3 memory_manager
#define SWITCH_MUTEX_NESTED
Definition: switch_apr.h:318
#define SWITCH_THREAD_STACKSIZE
Definition: switch_types.h:584
switch_status_t switch_mutex_init(switch_mutex_t **lock, unsigned int flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:293
void switch_cond_next(void)
Definition: switch_time.c:658
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:665
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:698
switch_status_t switch_queue_create(switch_queue_t **queue, unsigned int queue_capacity, switch_memory_pool_t *pool)
Definition: switch_apr.c:1233
#define switch_assert(expr)
memset(buf, 0, buflen)

◆ switch_core_memory_stop()

void switch_core_memory_stop ( void  )

Definition at line 671 of file switch_core_memory.c.

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

Referenced by switch_core_destroy().

672 {
673 #ifndef INSTANTLY_DESTROY_POOLS
674  switch_status_t st;
675  void *pop = NULL;
676 
677  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Stopping memory pool queue.\n");
678 
679  memory_manager.pool_thread_running = 0;
681 
682 
683  while (switch_queue_trypop(memory_manager.pool_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
684  fspr_pool_destroy(pop);
685  }
686 #endif
687 }
#define SWITCH_CHANNEL_LOG
switch_status_t switch_queue_trypop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1264
static struct @3 memory_manager
switch_status_t switch_thread_join(switch_status_t *retval, switch_thread_t *thd)
Definition: switch_apr.c:1379
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.

◆ switch_core_perform_alloc()

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 518 of file switch_core_memory.c.

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

519 {
520  void *ptr = NULL;
521 
522  switch_assert(pool != NULL);
523 
524 #ifdef LOCK_MORE
525 #ifdef USE_MEM_LOCK
527 #endif
528 #endif
529 
530 #ifdef DEBUG_ALLOC
531  if (memory > DEBUG_ALLOC_CUTOFF)
532  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p Core Allocate %s %d\n",
533  (void *) pool, fspr_pool_tag(pool, NULL), (int) memory);
534  /*switch_assert(memory < 20000); */
535 #endif
536 
537 #if APR_POOL_DEBUG
538  ptr = fspr_palloc_debug(pool, memory, func);
539 #else
540  ptr = fspr_palloc(pool, memory);
541 #endif
542  switch_assert(ptr != NULL);
543  memset(ptr, 0, memory);
544 
545 #ifdef LOCK_MORE
546 #ifdef USE_MEM_LOCK
548 #endif
549 #endif
550 
551  return ptr;
552 }
#define DEBUG_ALLOC_CUTOFF
switch_memory_pool_t * pool
static struct @3 memory_manager
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
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_core_perform_destroy_memory_pool()

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 465 of file switch_core_memory.c.

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

466 {
467  char *tmp;
468  const char *tag;
469  switch_memory_pool_t *tmp_pool = NULL;
470  switch_assert(pool != NULL);
471 
472  /* In tag we store who calls the pool creation.
473  Now we append it with who calls the pool destroy.
474  */
475  if (*pool) {
476  tmp_pool = *pool;
477  *pool = NULL;
478 
479  tag = fspr_pool_tag(tmp_pool, NULL);
480  tmp = switch_core_sprintf(tmp_pool, "%s,%s:%d", (tag ? tag : ""), file, line);
481  fspr_pool_tag(tmp_pool, tmp);
482  }
483 
484 #ifdef DEBUG_ALLOC2
485  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p Free Pool %s\n", (void *) tmp_pool, fspr_pool_tag(tmp_pool, NULL));
486 #endif
487 
488 #ifdef INSTANTLY_DESTROY_POOLS
489 #ifdef USE_MEM_LOCK
491 #endif
492  fspr_pool_destroy(tmp_pool);
493 #ifdef USE_MEM_LOCK
495 #endif
496 #else
497  if ((memory_manager.pool_thread_running != 1) || (switch_queue_push(memory_manager.pool_queue, tmp_pool) != SWITCH_STATUS_SUCCESS)) {
498 #ifdef USE_MEM_LOCK
500 #endif
501 #if APR_POOL_DEBUG
502  fspr_pool_destroy_debug(tmp_pool, func);
503 #else
504  if (tmp_pool) {
505  fspr_pool_destroy(tmp_pool);
506  }
507 #endif
508 #ifdef USE_MEM_LOCK
509 
511 #endif
512  }
513 #endif
514 
515  return SWITCH_STATUS_SUCCESS;
516 }
switch_memory_pool_t * pool
char * switch_core_sprintf(switch_memory_pool_t *pool, const char *fmt,...)
static struct @3 memory_manager
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
switch_status_t switch_queue_push(switch_queue_t *queue, void *data)
Definition: switch_apr.c:1253
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.
struct fspr_pool_t switch_memory_pool_t
#define switch_assert(expr)

◆ switch_core_perform_new_memory_pool()

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 391 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.

392 {
393  char *tmp;
394 #ifdef INSTANTLY_DESTROY_POOLS
395  fspr_pool_create(pool, NULL);
396  switch_assert(*pool != NULL);
397 #else
398 
399 #ifdef PER_POOL_LOCK
400  fspr_allocator_t *my_allocator = NULL;
401  fspr_thread_mutex_t *my_mutex;
402 #else
403  void *pop = NULL;
404 #endif
405 
406 #ifdef USE_MEM_LOCK
408 #endif
409  switch_assert(pool != NULL);
410 
411 #ifndef PER_POOL_LOCK
412  if (switch_queue_trypop(memory_manager.pool_recycle_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
413  *pool = (switch_memory_pool_t *) pop;
414  } else {
415 #endif
416 
417 #ifdef PER_POOL_LOCK
418  if ((fspr_allocator_create(&my_allocator)) != APR_SUCCESS) {
419  abort();
420  }
421 
422 #if APR_POOL_DEBUG
423  if ((fspr_pool_create_ex_debug(pool, memory_manager.memory_pool, NULL, my_allocator, func)) != APR_SUCCESS) {
424 #else
425  if ((fspr_pool_create_ex(pool, NULL, NULL, my_allocator)) != APR_SUCCESS) {
426 #endif
427  abort();
428  }
429 
430  if ((fspr_thread_mutex_create(&my_mutex, APR_THREAD_MUTEX_NESTED, *pool)) != APR_SUCCESS) {
431  abort();
432  }
433 
434  fspr_allocator_mutex_set(my_allocator, my_mutex);
435  fspr_allocator_owner_set(my_allocator, *pool);
436 
437  fspr_pool_mutex_set(*pool, my_mutex);
438 
439 #else
440  fspr_pool_create(pool, NULL);
441  switch_assert(*pool != NULL);
442  }
443 #endif
444 #endif
445 
446  tmp = switch_core_sprintf(*pool, "%s:%d", file, line);
447  fspr_pool_tag(*pool, tmp);
448 
449 #if APR_POOL_DEBUG
450  fspr_pool_userdata_set(tmp, "line", NULL, *pool);
451 #endif
452 
453 #ifdef DEBUG_ALLOC2
454  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p New Pool %s\n", (void *) *pool, fspr_pool_tag(*pool, NULL));
455 #endif
456 
457 
458 #ifdef USE_MEM_LOCK
460 #endif
461 
462  return SWITCH_STATUS_SUCCESS;
463 }
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:1264
static struct @3 memory_manager
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
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.
struct fspr_pool_t switch_memory_pool_t
#define switch_assert(expr)

◆ switch_core_perform_permanent_alloc()

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

Definition at line 113 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().

114 {
115  void *ptr = NULL;
116  switch_assert(memory_manager.memory_pool != NULL);
117 
118 #ifdef LOCK_MORE
119 #ifdef USE_MEM_LOCK
121 #endif
122 #endif
123 
124 #ifdef DEBUG_ALLOC
125  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p Perm Allocate %s %d\n",
126  (void *)memory_manager.memory_pool, fspr_pool_tag(memory_manager.memory_pool, NULL), (int) memory);
127 #endif
128 
129  ptr = fspr_palloc(memory_manager.memory_pool, memory);
130 
131  switch_assert(ptr != NULL);
132  memset(ptr, 0, memory);
133 
134 #ifdef LOCK_MORE
135 #ifdef USE_MEM_LOCK
137 #endif
138 #endif
139 
140  return ptr;
141 }
static struct @3 memory_manager
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
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_core_perform_permanent_strdup()

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

Definition at line 143 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.

144 {
145  char *duped = NULL;
146  switch_size_t len;
147  switch_assert(memory_manager.memory_pool != NULL);
148 
149  if (!todup) {
150  return NULL;
151  }
152 
153  if (zstr(todup)) {
154  return SWITCH_BLANK_STRING;
155  }
156 #ifdef LOCK_MORE
157 #ifdef USE_MEM_LOCK
159 #endif
160 #endif
161 
162  len = strlen(todup) + 1;
163  duped = fspr_pstrmemdup(memory_manager.memory_pool, todup, len);
164  switch_assert(duped != NULL);
165 
166 #ifdef DEBUG_ALLOC
167  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p Perm Allocate %s %d\n",
168  (void *) memory_manager.memory_pool, fspr_pool_tag(memory_manager.memory_pool, NULL), (int) len);
169 #endif
170 
171 #ifdef LOCK_MORE
172 #ifdef USE_MEM_LOCK
174 #endif
175 #endif
176 
177  return duped;
178 }
static struct @3 memory_manager
#define zstr(x)
Definition: switch_utils.h:314
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
#define SWITCH_BLANK_STRING
Definition: switch_types.h:48
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_core_perform_session_alloc()

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 73 of file switch_core_memory.c.

References DEBUG_ALLOC_CUTOFF, memory_manager, memset(), switch_core_session::pool, switch_assert, SWITCH_CHANNEL_ID_LOG, SWITCH_LOG_CONSOLE, switch_log_printf(), switch_mutex_lock(), and switch_mutex_unlock().

75 {
76  void *ptr = NULL;
77  switch_assert(session != NULL);
78  switch_assert(session->pool != NULL);
79 
80 #ifdef LOCK_MORE
81 #ifdef USE_MEM_LOCK
83 #endif
84 #endif
85 
86 #ifdef DEBUG_ALLOC
87  if (memory > DEBUG_ALLOC_CUTOFF)
88  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p %p Session Allocate %s %d\n",
89  (void *) session->pool, (void *) session, fspr_pool_tag(session->pool, NULL), (int) memory);
90 #endif
91 
92 #if APR_POOL_DEBUG
93  ptr = fspr_palloc_debug(session->pool, memory, func);
94 #else
95  ptr = fspr_palloc(session->pool, memory);
96 #endif
97  switch_assert(ptr != NULL);
98 
99  memset(ptr, 0, memory);
100 
101 #ifdef LOCK_MORE
102 #ifdef USE_MEM_LOCK
104 #endif
105 #endif
106 
107  return ptr;
108 }
#define DEBUG_ALLOC_CUTOFF
static struct @3 memory_manager
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
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)

◆ switch_core_perform_session_strdup()

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 232 of file switch_core_memory.c.

References DEBUG_ALLOC_CUTOFF, memory_manager, switch_core_session::pool, switch_assert, SWITCH_BLANK_STRING, SWITCH_CHANNEL_ID_LOG, SWITCH_LOG_CONSOLE, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), and zstr.

233 {
234  char *duped = NULL;
235 #ifdef DEBUG_ALLOC
236  switch_size_t len;
237 #endif
238 
239  switch_assert(session != NULL);
240  switch_assert(session->pool != NULL);
241 
242  if (!todup) {
243  return NULL;
244  }
245 
246  if (zstr(todup)) {
247  return SWITCH_BLANK_STRING;
248  }
249 #ifdef LOCK_MORE
250 #ifdef USE_MEM_LOCK
252 #endif
253 #endif
254 
255 
256 
257 #ifdef DEBUG_ALLOC
258  len = strlen(todup);
259  if (len > DEBUG_ALLOC_CUTOFF)
260  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p %p Sess Strdup Allocate %s %ld\n",
261  (void *) session->pool, (void *)session, fspr_pool_tag(session->pool, NULL), strlen(todup));
262 #endif
263 
264  duped = fspr_pstrdup(session->pool, todup);
265  switch_assert(duped != NULL);
266 
267 #ifdef LOCK_MORE
268 #ifdef USE_MEM_LOCK
270 #endif
271 #endif
272 
273  return duped;
274 }
#define DEBUG_ALLOC_CUTOFF
static struct @3 memory_manager
#define zstr(x)
Definition: switch_utils.h:314
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
#define SWITCH_BLANK_STRING
Definition: switch_types.h:48
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)

◆ switch_core_perform_strdup()

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

Definition at line 276 of file switch_core_memory.c.

References switch_core_perform_strndup().

277 {
278  return switch_core_perform_strndup(pool, todup, todup ? strlen(todup) + 1 : 0, file, func, line);
279 }
switch_memory_pool_t * pool
char * switch_core_perform_strndup(switch_memory_pool_t *pool, const char *todup, size_t len, const char *file, const char *func, int line)

◆ switch_core_perform_strndup()

char* switch_core_perform_strndup ( switch_memory_pool_t pool,
const char *  todup,
size_t  len,
const char *  file,
const char *  func,
int  line 
)

Definition at line 281 of file switch_core_memory.c.

References DEBUG_ALLOC_CUTOFF, 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.

Referenced by switch_core_perform_strdup().

282 {
283  char *duped = NULL;
284  switch_assert(pool != NULL);
285 
286  if (!todup) {
287  return NULL;
288  }
289 
290  if (zstr(todup)) {
291  return SWITCH_BLANK_STRING;
292  }
293 #ifdef LOCK_MORE
294 #ifdef USE_MEM_LOCK
296 #endif
297 #endif
298 
299 #ifdef DEBUG_ALLOC
300  if (len > DEBUG_ALLOC_CUTOFF)
301  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p Core Strdup Allocate %s %d\n",
302  (void *) pool, fspr_pool_tag(pool, NULL), (int)len);
303 #endif
304 
305  duped = fspr_pstrmemdup(pool, todup, len);
306  switch_assert(duped != NULL);
307 
308 #ifdef LOCK_MORE
309 #ifdef USE_MEM_LOCK
311 #endif
312 #endif
313 
314  return duped;
315 }
#define DEBUG_ALLOC_CUTOFF
switch_memory_pool_t * pool
static struct @3 memory_manager
#define zstr(x)
Definition: switch_utils.h:314
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
#define SWITCH_BLANK_STRING
Definition: switch_types.h:48
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_core_session_get_pool()

switch_memory_pool_t* switch_core_session_get_pool ( switch_core_session_t session)

Definition at line 64 of file switch_core_memory.c.

References switch_core_session::pool, and switch_assert.

65 {
66  switch_assert(session != NULL);
67  switch_assert(session->pool != NULL);
68  return session->pool;
69 }
switch_memory_pool_t * pool
#define switch_assert(expr)

◆ switch_core_session_sprintf()

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

Definition at line 180 of file switch_core_memory.c.

References switch_core_session::pool, and switch_core_vsprintf().

181 {
182  va_list ap;
183  char *result = NULL;
184 
185  va_start(ap, fmt);
186  result = switch_core_vsprintf(session->pool, fmt, ap);
187  va_end(ap);
188 
189  return result;
190 }
int cJSON_bool fmt
Definition: switch_cJSON.h:150
switch_memory_pool_t * pool
char * switch_core_vsprintf(switch_memory_pool_t *pool, const char *fmt, va_list ap)

◆ switch_core_sprintf()

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

Definition at line 221 of file switch_core_memory.c.

References switch_core_vsprintf().

Referenced by switch_core_perform_destroy_memory_pool(), and switch_core_perform_new_memory_pool().

222 {
223  va_list ap;
224  char *result;
225  va_start(ap, fmt);
226  result = switch_core_vsprintf(pool, fmt, ap);
227  va_end(ap);
228 
229  return result;
230 }
switch_memory_pool_t * pool
int cJSON_bool fmt
Definition: switch_cJSON.h:150
char * switch_core_vsprintf(switch_memory_pool_t *pool, const char *fmt, va_list ap)

◆ switch_core_vsprintf()

char* switch_core_vsprintf ( switch_memory_pool_t pool,
const char *  fmt,
va_list  ap 
)

Definition at line 197 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().

198 {
199  char *result = NULL;
200 
201  switch_assert(pool != NULL);
202 
203 #ifdef LOCK_MORE
204 #ifdef USE_MEM_LOCK
206 #endif
207 #endif
208 
209  result = fspr_pvsprintf(pool, fmt, ap);
210  switch_assert(result != NULL);
211 
212 #ifdef LOCK_MORE
213 #ifdef USE_MEM_LOCK
215 #endif
216 #endif
217 
218  return result;
219 }
switch_memory_pool_t * pool
static struct @3 memory_manager
int cJSON_bool fmt
Definition: switch_cJSON.h:150
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
#define switch_assert(expr)

Variable Documentation

◆ memory_manager

struct { ... } memory_manager

◆ memory_pool

switch_memory_pool_t* memory_pool

Definition at line 60 of file switch_core_memory.c.

◆ pool_queue

switch_queue_t* pool_queue

Definition at line 58 of file switch_core_memory.c.

◆ pool_recycle_queue

switch_queue_t* pool_recycle_queue

Definition at line 59 of file switch_core_memory.c.

◆ pool_thread_p

switch_thread_t* pool_thread_p = NULL
static

Definition at line 668 of file switch_core_memory.c.

◆ pool_thread_running

int pool_thread_running

Definition at line 61 of file switch_core_memory.c.