void *zrealloc(void *ptr, size_t size) { #ifndef HAVE_MALLOC_SIZE void *realptr; #endif size_t oldsize; void *newptr; if (ptr == NULL) return zmalloc(size); #ifdef HAVE_MALLOC_SIZE oldsize = zmalloc_size(ptr); newptr = realloc(ptr,size); if (!newptr) zmalloc_oom(size); update_zmalloc_stat_free(oldsize); update_zmalloc_stat_alloc(zmalloc_size(newptr),size); return newptr; #else realptr = (char*)ptr-PREFIX_SIZE; oldsize = *((size_t*)realptr); newptr = realloc(realptr,size+PREFIX_SIZE); if (!newptr) zmalloc_oom(size); *((size_t*)newptr) = size; update_zmalloc_stat_free(oldsize); update_zmalloc_stat_alloc(size,size); return (char*)newptr+PREFIX_SIZE; #endif }
void *zcalloc(size_t size) { void *ptr = calloc(1, size+PREFIX_SIZE); if (!ptr) zmalloc_oom(size); #ifdef HAVE_MALLOC_SIZE update_zmalloc_stat_alloc(zmalloc_size(ptr),size); return ptr; #else *((size_t*)ptr) = size; update_zmalloc_stat_alloc(size+PREFIX_SIZE,size); return (char*)ptr+PREFIX_SIZE; #endif }
void *zcalloc(size_t size) { void *ptr = calloc(1, size+PREFIX_SIZE); if (!ptr) zmalloc_oom_handler(size);//输出错误 #ifdef HAVE_MALLOC_SIZE update_zmalloc_stat_alloc(zmalloc_size(ptr));//更新内存使用统计 return ptr; #else *((size_t*)ptr) = size; update_zmalloc_stat_alloc(size+PREFIX_SIZE);//更新内存使用统计 return (char*)ptr+PREFIX_SIZE; #endif }
void *zmalloc(size_t size) { void *ptr = malloc(size+PREFIX_SIZE); if (!ptr) { zmalloc_oom_handler(size); } #ifdef HAVE_MALLOC_SIZE update_zmalloc_stat_alloc(zmalloc_size(ptr)); return ptr; #else *((size_t*)ptr) = size; update_zmalloc_stat_alloc(size+PREFIX_SIZE); return (char*)ptr+PREFIX_SIZE; #endif }
/* 调用系统函数calloc函数申请空间 */ void *zcalloc(size_t size) { //calloc与malloc的意思一样,不过参数不一样 //void *calloc(size_t numElements,size_t sizeOfElement),;numElements * sizeOfElement才是最终的内存的大小 //所在这里就是申请一块大小为size+PREFIX_SIZE的空间 void *ptr = calloc(1, size+PREFIX_SIZE); if (!ptr) zmalloc_oom_handler(size); #ifdef HAVE_MALLOC_SIZE update_zmalloc_stat_alloc(zmalloc_size(ptr)); return ptr; #else *((size_t*)ptr) = size; update_zmalloc_stat_alloc(size+PREFIX_SIZE); return (char*)ptr+PREFIX_SIZE; #endif }
/* 调用zmalloc申请size个大小的空间 */ void *zmalloc(size_t size) { //实际调用的还是malloc函数 void *ptr = malloc(size+PREFIX_SIZE); //如果申请的结果为null,说明发生了oom,调用oom的处理方法 if (!ptr) zmalloc_oom_handler(size); #ifdef HAVE_MALLOC_SIZE //更新used_memory的大小 update_zmalloc_stat_alloc(zmalloc_size(ptr)); return ptr; #else *((size_t*)ptr) = size; update_zmalloc_stat_alloc(size+PREFIX_SIZE); return (char*)ptr+PREFIX_SIZE; #endif }
void *zcalloc(size_t size) { void *ptr = calloc(1, size+PREFIX_SIZE); #ifdef WIN32 if ( mutex_initialized == 0 ) { pthread_mutex_init(&used_memory_mutex, NULL ); mutex_initialized = 1 ; } #endif if (!ptr) zmalloc_oom_handler(size); #ifdef HAVE_MALLOC_SIZE update_zmalloc_stat_alloc(zmalloc_size(ptr),size); return ptr; #else *((size_t*)ptr) = size; update_zmalloc_stat_alloc(size+PREFIX_SIZE,size); return (char*)ptr+PREFIX_SIZE; #endif }
void *zmalloc(size_t size) { void *ptr = malloc(size + PREFIX_SIZE); if (!ptr) return NULL; *((size_t*) ptr) = size; update_zmalloc_stat_alloc(size+PREFIX_SIZE); return (char*) ptr + PREFIX_SIZE; }
void *zcalloc(size_t count, size_t size) { size_t nSize = count * size; void *ptr = __calloc(count, size); if (!ptr) zmalloc_oom_handler(nSize); update_zmalloc_stat_alloc(__malloc_size(ptr)); return ptr; }
void *zmalloc(size_t size) { void *ptr = __malloc(size); if (!ptr) zmalloc_oom_handler(size); update_zmalloc_stat_alloc(__malloc_size(ptr)); // 分配后将其清零. memset(ptr, 0, size); return ptr; }
void *zrealloc(void *ptr, size_t size) { #ifndef HAVE_MALLOC_SIZE void *realptr; #endif size_t oldsize; void *newptr; #ifdef WIN32 if ( mutex_initialized == 0 ) { pthread_mutex_init(&used_memory_mutex, NULL ); mutex_initialized = 1 ; } #endif if (ptr == NULL) return zmalloc(size); #ifdef HAVE_MALLOC_SIZE oldsize = zmalloc_size(ptr); newptr = realloc(ptr,size); if (!newptr) zmalloc_oom_handler(size); update_zmalloc_stat_free(oldsize); update_zmalloc_stat_alloc(zmalloc_size(newptr),size); return newptr; #else realptr = (char*)ptr-PREFIX_SIZE; oldsize = *((size_t*)realptr); newptr = realloc(realptr,size+PREFIX_SIZE); if (!newptr) zmalloc_oom_handler(size); *((size_t*)newptr) = size; update_zmalloc_stat_free(oldsize); update_zmalloc_stat_alloc(size,size); return (char*)newptr+PREFIX_SIZE; #endif }
void *zrealloc(void *ptr, size_t size) { size_t oldsize; void *newptr; if (ptr == NULL) return zmalloc(size); oldsize = __malloc_size(ptr); newptr = __realloc(ptr, size); if (!newptr) zmalloc_oom_handler(size); update_zmalloc_stat_free(oldsize); update_zmalloc_stat_alloc(__malloc_size(newptr)); return newptr; }
void *zmalloc_no_tcache(size_t size) { void *ptr = mallocx(size+PREFIX_SIZE, MALLOCX_TCACHE_NONE); if (!ptr) zmalloc_oom_handler(size); update_zmalloc_stat_alloc(zmalloc_size(ptr)); return ptr; }