/* * A basic header of information wrapper allocator. Simply stores * information as a header, returns the memory + 1 past it, can be * retrieved again with - 1. Where type is stat_mem_block_t*. */ void *stat_mem_allocate(size_t size, size_t line, const char *file) { stat_mem_block_t *info = (stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size); void *data = (void*)(info + 1); if(GMQCC_UNLIKELY(!info)) return NULL; info->line = line; info->size = size; info->file = file; info->prev = NULL; info->next = stat_mem_block_root; /* likely since it only happens once */ if (GMQCC_LIKELY(stat_mem_block_root != NULL)) { VALGRIND_MAKE_MEM_DEFINED(stat_mem_block_root, sizeof(stat_mem_block_t)); stat_mem_block_root->prev = info; VALGRIND_MAKE_MEM_NOACCESS(stat_mem_block_root, sizeof(stat_mem_block_t)); } stat_mem_block_root = info; stat_mem_allocated += size; stat_mem_high += size; stat_mem_allocated_total ++; if (stat_mem_high > stat_mem_peak) stat_mem_peak = stat_mem_high; VALGRIND_MALLOCLIKE_BLOCK(data, size, sizeof(stat_mem_block_t), 0); return data; }
/* * A basic header of information wrapper allocator. Simply stores * information as a header, returns the memory + 1 past it, can be * retrieved again with - 1. Where type is stat_mem_block_t*. */ void *stat_mem_allocate(size_t size, size_t line, const char *file, const char *expr) { stat_mem_block_t *info = (stat_mem_block_t*)malloc(size + IDENT_MEM_TOP); void *data = (void *)((char*)info + IDENT_MEM_TOP); if(GMQCC_UNLIKELY(!info)) return NULL; info->line = line; info->size = size; info->file = file; info->expr = expr; info->prev = NULL; info->next = stat_mem_block_root; /* Write identifier */ memcpy(info + 1, IDENT_MEM, IDENT_SIZE); /* likely since it only happens once */ if (GMQCC_LIKELY(stat_mem_block_root != NULL)) { VALGRIND_MAKE_MEM_DEFINED(stat_mem_block_root, IDENT_MEM_TOP); stat_mem_block_root->prev = info; VALGRIND_MAKE_MEM_NOACCESS(stat_mem_block_root, IDENT_MEM_TOP); } stat_mem_block_root = info; stat_mem_allocated += size; stat_mem_high += size; stat_mem_allocated_total ++; if (stat_mem_high > stat_mem_peak) stat_mem_peak = stat_mem_high; VALGRIND_MALLOCLIKE_BLOCK(data, size, IDENT_MEM_TOP, 0); return data; }
void *stat_mem_reallocate(void *ptr, size_t size, size_t line, const char *file) { stat_mem_block_t *oldinfo = NULL; stat_mem_block_t *newinfo; if (GMQCC_UNLIKELY(!ptr)) return stat_mem_allocate(size, line, file); /* stay consistent with glibc */ if (GMQCC_UNLIKELY(!size)) { stat_mem_deallocate(ptr); return NULL; } oldinfo = ((stat_mem_block_t*)ptr - 1); newinfo = ((stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size)); if (GMQCC_UNLIKELY(!newinfo)) { stat_mem_deallocate(ptr); return NULL; } VALGRIND_MALLOCLIKE_BLOCK(newinfo + 1, size, sizeof(stat_mem_block_t), 0); /* we need access to the old info redzone */ VALGRIND_MAKE_MEM_DEFINED(oldinfo, sizeof(stat_mem_block_t)); memcpy(newinfo+1, oldinfo+1, oldinfo->size); if (oldinfo->prev) { /* just need access for a short period */ VALGRIND_MAKE_MEM_DEFINED(oldinfo->prev, sizeof(stat_mem_block_t)); oldinfo->prev->next = oldinfo->next; /* don't need access anymore */ VALGRIND_MAKE_MEM_NOACCESS(oldinfo->prev, sizeof(stat_mem_block_t)); } if (oldinfo->next) { /* just need access for a short period */ VALGRIND_MAKE_MEM_DEFINED(oldinfo->next, sizeof(stat_mem_block_t)); oldinfo->next->prev = oldinfo->prev; /* don't need access anymore */ VALGRIND_MAKE_MEM_NOACCESS(oldinfo->next, sizeof(stat_mem_block_t)); } /* move ahead */ if (oldinfo == stat_mem_block_root) stat_mem_block_root = oldinfo->next; /* we need access to the redzone for the newinfo block */ VALGRIND_MAKE_MEM_DEFINED(newinfo, sizeof(stat_mem_block_t)); newinfo->line = line; newinfo->size = size; newinfo->file = file; newinfo->prev = NULL; newinfo->next = stat_mem_block_root; /* * likely since the only time there is no root is when it's * being initialized first. */ if (GMQCC_LIKELY(stat_mem_block_root != NULL)) { /* we need access to the root */ VALGRIND_MAKE_MEM_DEFINED(stat_mem_block_root, sizeof(stat_mem_block_t)); stat_mem_block_root->prev = newinfo; /* kill access */ VALGRIND_MAKE_MEM_NOACCESS(stat_mem_block_root, sizeof(stat_mem_block_t)); } stat_mem_block_root = newinfo; stat_mem_allocated -= oldinfo->size; stat_mem_high -= oldinfo->size; stat_mem_allocated += newinfo->size; stat_mem_high += newinfo->size; /* * we're finished with the redzones, lets kill the access * to them. */ VALGRIND_MAKE_MEM_NOACCESS(newinfo, sizeof(stat_mem_block_t)); VALGRIND_MAKE_MEM_NOACCESS(oldinfo, sizeof(stat_mem_block_t)); if (stat_mem_high > stat_mem_peak) stat_mem_peak = stat_mem_high; free(oldinfo); VALGRIND_FREELIKE_BLOCK(ptr, sizeof(stat_mem_block_t)); return newinfo + 1; }