struct hp_block *hp_shm_malloc_init(char *address, unsigned long size, char *name) { struct hp_block *hpb; hpb = hp_malloc_init(address, size, name); if (!hpb) { LM_ERR("failed to initialize shm block\n"); return NULL; } #ifdef HP_MALLOC_FAST_STATS hpb->free_hash[PEEK_HASH_RR(hpb, hpb->first_frag->size)].total_no++; #endif hp_frag_attach(hpb, hpb->first_frag); /* first fragment attach is the equivalent of a split */ if (stats_are_ready()) { #if defined(STATISTICS) && !defined(HP_MALLOC_FAST_STATS) update_stat(shm_rused, FRAG_OVERHEAD); update_stat(shm_frags, 1); #endif #if defined(DBG_MALLOC) || defined(STATISTICS) hpb->real_used += FRAG_OVERHEAD; hpb->total_fragments++; #endif } else { hpb->real_used += FRAG_OVERHEAD; hpb->total_fragments++; } #ifdef HP_MALLOC_FAST_STATS #ifdef DBG_MALLOC hp_stats_lock = hp_shm_malloc_unsafe(hpb, sizeof *hp_stats_lock, __FILE__, __FUNCTION__, __LINE__); #else hp_stats_lock = hp_shm_malloc_unsafe(hpb, sizeof *hp_stats_lock); #endif if (!hp_stats_lock) { LM_ERR("failed to alloc hp statistics lock\n"); return NULL; } if (!lock_init(hp_stats_lock)) { LM_CRIT("could not initialize hp statistics lock\n"); return NULL; } #endif return hpb; }
void *hp_shm_realloc_unsafe(struct hp_block *hpb, void *p, unsigned long size) { struct hp_frag *f; unsigned long orig_size; void *ptr; if (size == 0) { if (p) hp_shm_free_unsafe(hpb, p); return NULL; } if (!p) return hp_shm_malloc_unsafe(hpb, size); f = FRAG_OF(p); size = ROUNDUP(size); orig_size = f->size; /* shrink operation? */ if (orig_size > size) shm_frag_split_unsafe(hpb, f, size); else if (orig_size < size) { ptr = hp_shm_malloc_unsafe(hpb, size); if (ptr) { /* copy, need by libssl */ memcpy(ptr, p, orig_size); hp_shm_free_unsafe(hpb, p); } p = ptr; } return p; }
struct hp_block *hp_shm_malloc_init(char *address, unsigned long size) { struct hp_block *hpb; hpb = hp_malloc_init(address, size); if (!hpb) { LM_ERR("failed to initialize shm block\n"); return NULL; } #ifdef HP_MALLOC_FAST_STATS hpb->free_hash[PEEK_HASH_RR(hpb, hpb->first_frag->size)].total_no++; #endif hp_frag_attach(hpb, hpb->first_frag); /* first fragment attach is the equivalent of a split */ if (stats_are_ready()) update_stats_shm_frag_split(hpb, hpb->first_frag); else { hpb->real_used += FRAG_OVERHEAD; hpb->total_fragments++; } /* if memory warming is on, pre-populate the hash with free fragments */ if (mem_warming_enabled) { if (!mem_warming_pattern_file) mem_warming_pattern_file = MEM_WARMING_DEFAULT_PATTERN_FILE; if (mem_warming_percentage == -1) mem_warming_percentage = MEM_WARMING_DEFAULT_PERCENTAGE; if (hp_mem_warming(hpb) != 0) LM_INFO("skipped memory warming\n"); } hp_stats_lock = hp_shm_malloc_unsafe(hpb, sizeof *hp_stats_lock); if (!hp_stats_lock) { LM_ERR("failed to alloc hp statistics lock\n"); return NULL; } if (!lock_init(hp_stats_lock)) { LM_CRIT("could not initialize hp statistics lock\n"); return NULL; } return hpb; }