antirez/redis — Issue #236: zrealloc function in the zmalloc.c file
Author: biechangjiang
Created: 2011-12-06 05:11:08+00:00
State: open

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 = redis_malloc_size(ptr);
newptr = realloc(ptr,size);
if (!newptr) zmalloc_oom(size);

update_zmalloc_stat_free(oldsize);
update_zmalloc_stat_alloc(redis_malloc_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_free(oldsize+PREFIX_SIZE);
update_zmalloc_stat_alloc(size,size); //update_zmalloc_stat_alloc(size+PREFIX_SIZE,size);
return (char*)newptr+PREFIX_SIZE;
#endif
}