mem_chunk *mem_AddChunk(void *ptr,unsigned long size) { mem_chunk *chunk = (mem_chunk*)malloc_ptr(sizeof(mem_chunk)); if(mem_verbosity>=1) printf("adding chunk:%x (size:%d)\n",ptr,size); //TODO add backtrace string chunk->ptr = ptr; chunk->size = size; chunk->is_freed = 0; chunk->prev = mem_last_chunk; chunk->next = NULL; mem_chunks_num++; mem_allocated_size+=size; if(mem_allocated_size>mem_max_allocated_size) mem_max_allocated_size = mem_allocated_size; if(mem_last_chunk) { //printf("updating prev last_chunk chunk->next\n"); mem_last_chunk->next = chunk; } else { //printf("first chunk in chain\n"); mem_first_chunk = chunk; } mem_last_chunk = chunk; /*if(!mem_first_chunk) mem_first_chunk = chunk; */ return(chunk); }
void *mem_malloc(unsigned long size) { void *ptr = malloc_ptr(size); if(size > mem_max_chunk_size) { mem_max_chunk_size = size; } mem_AddChunk(ptr,size); return(ptr); }
void operator()( int /*id*/ ) const { void* (*malloc_ptr)(size_t); void (*free_ptr)(void*); const char* actual_name; LIBRARY_HANDLE lib = LOAD_LIBRARY(actual_name = MALLOCLIB_NAME1); if (!lib) lib = LOAD_LIBRARY(actual_name = MALLOCLIB_NAME2); if (!lib) { REPORT("Can't load " MALLOCLIB_NAME1 " or " MALLOCLIB_NAME2 "\n"); exit(1); } #if _WIN32 || _WIN64 (void *&)malloc_ptr = GetProcAddress(lib, "scalable_malloc"); (void *&)free_ptr = GetProcAddress(lib, "scalable_free"); #else (void *&)malloc_ptr = dlsym(lib, "scalable_malloc"); (void *&)free_ptr = dlsym(lib, "scalable_free"); #endif if (!malloc_ptr || !free_ptr) { REPORT("Can't find scalable_(malloc|free) in %s \n", actual_name); exit(1); } void *p = malloc_ptr(100); memset(p, 1, 100); free_ptr(p); #if _WIN32 || _WIN64 BOOL ret = FreeLibrary(lib); ASSERT(ret, "FreeLibrary must be successful"); ASSERT(GetModuleHandle(actual_name), "allocator library must not be unloaded"); #else int ret = dlclose(lib); ASSERT(ret == 0, "dlclose must be successful"); ASSERT(dlsym(RTLD_DEFAULT, "scalable_malloc"), "allocator library must not be unloaded"); #endif }