/* append a newly received tag from a 200/INVITE to * transaction's set; (only safe if called from within * a REPLY_LOCK); it returns 1 if such a to tag already * exists */ inline static int update_totag_set(struct cell *t, struct sip_msg *ok) { struct totag_elem *i, *n; str *tag; char *s; if (!ok->to || !ok->to->parsed) { LM_ERR("to not parsed\n"); return 0; } tag=&get_to(ok)->tag_value; if (!tag->s) { LM_DBG("no tag in to\n"); return 0; } for (i=t->fwded_totags; i; i=i->next) { if (i->tag.len==tag->len && memcmp(i->tag.s, tag->s, tag->len) ==0 ){ /* to tag already recorded */ #ifdef XL_DEBUG LM_CRIT("totag retransmission\n"); #else LM_DBG("totag retransmission\n"); #endif return 1; } } /* that's a new to-tag -- record it */ #ifndef HP_MALLOC shm_lock(); n=(struct totag_elem*) shm_malloc_unsafe(sizeof(struct totag_elem)); s=(char *)shm_malloc_unsafe(tag->len); shm_unlock(); #else n=(struct totag_elem*) shm_malloc(sizeof(struct totag_elem)); s=(char *)shm_malloc(tag->len); #endif if (!s || !n) { LM_ERR("no more share memory \n"); if (n) shm_free(n); if (s) shm_free(s); return 0; } memset(n, 0, sizeof(struct totag_elem)); memcpy(s, tag->s, tag->len ); n->tag.s=s;n->tag.len=tag->len; n->next=t->fwded_totags; t->fwded_totags=n; LM_DBG("new totag \n"); return 0; }
inline static void* sh_realloc(void* p, unsigned int size) { void *r; shm_lock(); shm_free_unsafe(p); r=shm_malloc_unsafe(size); shm_unlock(); return r; }
/** * init the core lock */ int shm_core_lock_init(void) { if (_shm_lock) { LM_DBG("shared memory lock initialized\n"); return 0; } _shm_lock=shm_malloc_unsafe(sizeof(gen_lock_t)); /* skip lock_alloc, race cond*/ if (_shm_lock==0){ LOG(L_CRIT, "could not allocate lock\n"); shm_core_destroy(); return -1; } if (lock_init(_shm_lock)==0){ LOG(L_CRIT, "could not initialize lock\n"); shm_core_destroy(); return -1; } return 0; }
inline static void* sh_realloc(void* p, unsigned int size) { void *r; #ifndef HP_MALLOC shm_lock(); shm_free_unsafe(p); r = shm_malloc_unsafe(size); #else shm_free(p); r = shm_malloc(size); #endif shm_threshold_check(); #ifndef HP_MALLOC shm_unlock(); #endif return r; }
int shm_getmem(void) { #ifdef SHM_MMAP int fd; #else struct shmid_ds shm_info; #endif #ifdef SHM_MMAP if (shm_mempool && (shm_mempool!=(void*)-1)){ #else if ((shm_shmid!=-1)||(shm_mempool!=(void*)-1)){ #endif LM_CRIT("shm already initialized\n"); return -1; } #ifdef SHM_MMAP #ifdef USE_ANON_MMAP shm_mempool=mmap(0, shm_mem_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1 ,0); #else fd=open("/dev/zero", O_RDWR); if (fd==-1){ LM_CRIT("could not open /dev/zero: %s\n", strerror(errno)); return -1; } shm_mempool=mmap(0, shm_mem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd ,0); /* close /dev/zero */ close(fd); #endif /* USE_ANON_MMAP */ #else shm_shmid=shmget(IPC_PRIVATE, /* SHM_MEM_SIZE */ shm_mem_size , 0700); if (shm_shmid==-1){ LM_CRIT("could not allocate shared memory segment: %s\n", strerror(errno)); return -1; } shm_mempool=shmat(shm_shmid, 0, 0); #endif if (shm_mempool==(void*)-1){ LM_CRIT("could not attach shared memory segment: %s\n", strerror(errno)); /* destroy segment*/ shm_mem_destroy(); return -1; } return 0; } int shm_mem_init_mallocs(void* mempool, unsigned long pool_size) { /* init it for malloc*/ shm_block=shm_malloc_init(mempool, pool_size); if (shm_block==0){ LM_CRIT("could not initialize shared malloc\n"); shm_mem_destroy(); return -1; } mem_lock=shm_malloc_unsafe(sizeof(gen_lock_t)); /* skip lock_alloc, race cond*/ if (mem_lock==0){ LM_CRIT("could not allocate lock\n"); shm_mem_destroy(); return -1; } if (lock_init(mem_lock)==0){ LM_CRIT("could not initialize lock\n"); shm_mem_destroy(); return -1; } #ifdef STATISTICS if (event_shm_threshold) { event_shm_last=shm_malloc_unsafe(sizeof(long)); if (event_shm_last==0){ LM_CRIT("could not allocate shm last event indicator\n"); shm_mem_destroy(); return -1; } *event_shm_last=0; event_shm_pending=shm_malloc_unsafe(sizeof(int)); if (event_shm_pending==0){ LM_CRIT("could not allocate shm peinding flags\n"); shm_mem_destroy(); return -1; } *event_shm_pending=0; } #endif /* STATISTICS */ LM_DBG("success\n"); return 0; }
int shm_getmem(void) { #ifdef SHM_MMAP int fd; #else struct shmid_ds shm_info; #endif #ifdef SHM_MMAP if (shm_mempool && (shm_mempool!=(void*)-1)){ #else if ((shm_shmid!=-1)||(shm_mempool!=(void*)-1)){ #endif LM_CRIT("shm already initialized\n"); return -1; } #ifdef SHM_MMAP #ifdef USE_ANON_MMAP shm_mempool=mmap(0, shm_mem_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1 ,0); #else fd=open("/dev/zero", O_RDWR); if (fd==-1){ LM_CRIT("could not open /dev/zero: %s\n", strerror(errno)); return -1; } shm_mempool=mmap(0, shm_mem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd ,0); /* close /dev/zero */ close(fd); #endif /* USE_ANON_MMAP */ #else shm_shmid=shmget(IPC_PRIVATE, /* SHM_MEM_SIZE */ shm_mem_size , 0700); if (shm_shmid==-1){ LM_CRIT("could not allocate shared memory segment: %s\n", strerror(errno)); return -1; } shm_mempool=shmat(shm_shmid, 0, 0); #endif if (shm_mempool==(void*)-1){ LM_CRIT("could not attach shared memory segment: %s\n", strerror(errno)); /* destroy segment*/ shm_mem_destroy(); return -1; } return 0; } int shm_mem_init_mallocs(void* mempool, unsigned long pool_size) { #ifdef HP_MALLOC int i; #endif /* init it for malloc*/ shm_block = shm_malloc_init(mempool, pool_size, "shm"); if (!shm_block){ LM_CRIT("could not initialize shared malloc\n"); shm_mem_destroy(); return -1; } #ifdef SHM_EXTRA_STATS int size_prealoc, j, one_full_entry, groups; char *start; if(mem_free_idx != 1){ #ifndef SHM_SHOW_DEFAULT_GROUP groups = mem_free_idx - 1; #else groups = mem_free_idx; #endif one_full_entry = 3 * (sizeof(stat_var) + sizeof(stat_val)); size_prealoc = groups * sizeof(struct module_info) + groups * one_full_entry; #ifndef DBG_MALLOC memory_mods_stats = MY_MALLOC_UNSAFE(shm_block, size_prealoc); #else memory_mods_stats = MY_MALLOC_UNSAFE(shm_block, size_prealoc, __FILE__, __FUNCTION__, __LINE__ ); #endif if(!memory_mods_stats){ LM_CRIT("could not alloc shared memory"); return -1; } memset( (void*)memory_mods_stats, 0, size_prealoc); start = (char*)memory_mods_stats + groups * sizeof(struct module_info); for(j = 0; j < groups; j++){ memory_mods_stats[j].fragments = (stat_var *)(start + j * one_full_entry); memory_mods_stats[j].memory_used = (stat_var *)(start + j * one_full_entry + sizeof(stat_var)); memory_mods_stats[j].real_used = (stat_var *)(start + j * one_full_entry + 2 * sizeof(stat_var)); memory_mods_stats[j].fragments->u.val = (stat_val*)(start + j * one_full_entry + 3 * sizeof(stat_var)); memory_mods_stats[j].memory_used->u.val = (stat_val*)(start + j * one_full_entry + 3 * sizeof(stat_var) + sizeof(stat_val)); memory_mods_stats[j].real_used->u.val = (stat_val*)(start + j * one_full_entry + 3 * sizeof(stat_var) + 2 * sizeof(stat_val)); } #ifndef SHM_SHOW_DEFAULT_GROUP if(core_index != 0){ update_stat(memory_mods_stats[core_index - 1].fragments, 1); update_stat(memory_mods_stats[core_index - 1].memory_used, size_prealoc); update_stat(memory_mods_stats[core_index - 1].real_used, size_prealoc + FRAG_OVERHEAD); } #else update_stat(memory_mods_stats[core_index].fragments, 1); update_stat(memory_mods_stats[core_index].memory_used, size_prealoc); update_stat(memory_mods_stats[core_index].real_used, size_prealoc + FRAG_OVERHEAD); #endif } #endif #ifdef HP_MALLOC /* lock_alloc cannot be used yet! */ mem_lock = shm_malloc_unsafe(HP_TOTAL_HASH_SIZE * sizeof *mem_lock); if (!mem_lock) { LM_CRIT("could not allocate the shm lock array\n"); shm_mem_destroy(); return -1; } for (i = 0; i < HP_TOTAL_HASH_SIZE; i++) if (!lock_init(&mem_lock[i])) { LM_CRIT("could not initialize lock\n"); shm_mem_destroy(); return -1; } mem_hash_usage = shm_malloc_unsafe(HP_TOTAL_HASH_SIZE * sizeof *mem_hash_usage); if (!mem_hash_usage) { LM_ERR("failed to allocate statistics array\n"); return -1; } memset(mem_hash_usage, 0, HP_TOTAL_HASH_SIZE * sizeof *mem_hash_usage); #else mem_lock = shm_malloc_unsafe(sizeof *mem_lock); if (!mem_lock) { LM_CRIT("could not allocate the shm lock\n"); shm_mem_destroy(); return -1; } if (!lock_init(mem_lock)) { LM_CRIT("could not initialize lock\n"); shm_mem_destroy(); return -1; } #endif #ifdef STATISTICS if (event_shm_threshold) { event_shm_last=shm_malloc_unsafe(sizeof(long)); if (event_shm_last==0){ LM_CRIT("could not allocate shm last event indicator\n"); shm_mem_destroy(); return -1; } *event_shm_last=0; event_shm_pending=shm_malloc_unsafe(sizeof(int)); if (event_shm_pending==0){ LM_CRIT("could not allocate shm pending flags\n"); shm_mem_destroy(); return -1; } *event_shm_pending=0; } #endif /* STATISTICS */ LM_DBG("success\n"); return 0; }
int shm_getmem() { #ifdef SHM_MMAP #ifndef USE_ANON_MMAP int fd; #endif #else struct shmid_ds shm_info; #endif #ifdef SHM_MMAP if (shm_mempool && (shm_mempool!=(void*)-1)){ #else if ((shm_shmid!=-1)||(shm_mempool!=(void*)-1)){ #endif LOG(L_CRIT, "BUG: shm_mem_init: shm already initialized\n"); return -1; } #ifdef SHM_MMAP #ifdef USE_ANON_MMAP shm_mempool=mmap(0, shm_mem_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1 ,0); #else fd=open("/dev/zero", O_RDWR); if (fd==-1){ LOG(L_CRIT, "ERROR: shm_mem_init: could not open /dev/zero: %s\n", strerror(errno)); return -1; } shm_mempool=mmap(0, shm_mem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd ,0); /* close /dev/zero */ close(fd); #endif /* USE_ANON_MMAP */ #else shm_shmid=shmget(IPC_PRIVATE, /* SHM_MEM_SIZE */ shm_mem_size , 0700); if (shm_shmid==-1){ LOG(L_CRIT, "ERROR: shm_mem_init: could not allocate shared memory" " segment: %s\n", strerror(errno)); return -1; } shm_mempool=shmat(shm_shmid, 0, 0); #endif if (shm_mempool==(void*)-1){ LOG(L_CRIT, "ERROR: shm_mem_init: could not attach shared memory" " segment: %s\n", strerror(errno)); /* destroy segment*/ shm_mem_destroy(); return -1; } return 0; } int shm_mem_init_mallocs(void* mempool, unsigned long pool_size) { /* init it for malloc*/ shm_block=shm_malloc_init(mempool, pool_size); if (shm_block==0){ LOG(L_CRIT, "ERROR: shm_mem_init: could not initialize shared" " malloc\n"); shm_mem_destroy(); return -1; } mem_lock=shm_malloc_unsafe(sizeof(gen_lock_t)); /* skip lock_alloc, race cond*/ if (mem_lock==0){ LOG(L_CRIT, "ERROR: shm_mem_init: could not allocate lock\n"); shm_mem_destroy(); return -1; } if (lock_init(mem_lock)==0){ LOG(L_CRIT, "ERROR: shm_mem_init: could not initialize lock\n"); shm_mem_destroy(); return -1; } DBG("shm_mem_init: success\n"); return 0; }