📄 memry.cpp
字号:
if (count < 1 || count > MAX_CHUNK) { tprintf ("Invalid size %d requested of free_struct", count); return; } // tprintf("Freeing structure of size %d\n",count); //no of MEMUNIONS-1 struct_count = (count - 1) / sizeof (MEMUNION); if (deadstruct == NULL) { //not really legal check_struct(MEMCHECKS, count); } else { if (struct_count < MAX_STRUCTS) { //can do fixed sizes #ifdef COUNTING_CLASS_STRUCTURES if (name != NULL) { index = identify_struct_owner (struct_count, name); if (index < MAX_CLASSES) { owner_counts[struct_count][index]--; ASSERT_HOST (owner_counts[struct_count][index] >= 0); } } #endif element = (MEMUNION *) deadstruct; //add to freelist element->ptr = free_structs[struct_count]; free_structs[struct_count] = element; //one less in use structs_in_use[struct_count]--; if (structs_in_use[struct_count] == 0) { index = 0; for (element = struct_blocks[struct_count]; element != NULL; element = nextblock) { //traverse and destroy nextblock = element->ptr; //free all the blocks old_struct_block(element); index++; } //none left any more struct_blocks[struct_count] = NULL; //no free structs free_structs[struct_count] = NULL; blocks_in_use[struct_count] = 0; } else if (structs_in_use[struct_count] < 0) { tprintf ("Negative number of structs of size %d in use", (int) count); } else if (structs_in_use[struct_count] < blocks_in_use[struct_count]) { prev_block = NULL; for (block = struct_blocks[struct_count]; block != NULL; block = nextblock) { nextblock = block; index = STRUCT_BLOCK_SIZE / (struct_count + 1) - 1; end_element = block + STRUCT_BLOCK_SIZE; for (element = free_structs[struct_count]; element != NULL; element = element->ptr) { if (element > nextblock && element < end_element) { index--; if (index == 0) break; } } if (index == 0) { index = STRUCT_BLOCK_SIZE / (struct_count + 1) - 1; for (element = free_structs[struct_count], prev_element = NULL; element != NULL; element = element->ptr) { if (element > nextblock && element < end_element) { index--; if (prev_element != NULL) prev_element->ptr = element->ptr; else free_structs[struct_count] = element->ptr; if (index == 0) break; } else prev_element = element; } if (prev_block != NULL) prev_block->ptr = block->ptr; else struct_blocks[struct_count] = block->ptr; nextblock = block->ptr; blocks_in_use[struct_count]--; //free all the blocks old_struct_block(block); } else { prev_block = block; //traverse and destroy nextblock = block->ptr; } } } } else free_mem(deadstruct); //free directly }#else free(deadstruct);#endif}/********************************************************************** * alloc_mem_p * * Allocate permanent space which will never be returned. * This space is allocated from the top end of a memory block to * avoid the fragmentation which would result from alternate use * of alloc_mem for permanent and temporary blocks. **********************************************************************///#ifdef __UNIX__//#pragma OPT_LEVEL 0//#endifDLLSYM void *alloc_mem_p( //allocate permanent space INT32 count //block size to allocate ) { #ifdef RAYS_MALLOC #ifdef TESTING_BIGSTUFF if (main_mem.biggestblock == 0) main_mem.init (alloc_big_mem, free_big_mem, FIRSTSIZE, LASTSIZE, MAX_CHUNK); #else if (main_mem.biggestblock == 0) main_mem.init ((void *(*)(INT32)) malloc, free, FIRSTSIZE, LASTSIZE, MAX_CHUNK); #endif if (mem_mallocdepth > 0) return main_mem.alloc_p (count, trace_caller (mem_mallocdepth)); else return main_mem.alloc_p (count, NULL); #else return malloc ((size_t) count); #endif}/********************************************************************** * alloc_mem * * Return a pointer to a buffer of count bytes aligned for any type. **********************************************************************/DLLSYM void *alloc_mem( //get some memory INT32 count //no of bytes to get ) { #ifdef RAYS_MALLOC #ifdef TESTING_BIGSTUFF if (main_mem.biggestblock == 0) main_mem.init (alloc_big_mem, free_big_mem, FIRSTSIZE, LASTSIZE, MAX_CHUNK); #else if (main_mem.biggestblock == 0) main_mem.init ((void *(*)(INT32)) malloc, free, FIRSTSIZE, LASTSIZE, MAX_CHUNK); #endif if (mem_mallocdepth > 0) return main_mem.alloc (count, trace_caller (mem_mallocdepth)); else return main_mem.alloc (count, NULL); #else return malloc ((size_t) count); #endif}/********************************************************************** * alloc_big_mem * * Return a pointer to a buffer of count bytes aligned for any type. **********************************************************************/DLLSYM void *alloc_big_mem( //get some memory INT32 count //no of bytes to get ) { #ifdef TESTING_BIGSTUFF if (big_mem.biggestblock == 0) big_mem.init ((void *(*)(INT32)) malloc, free, BIGSIZE, BIGSIZE, MAX_BIGCHUNK); if (mem_mallocdepth > 0) return big_mem.alloc (count, trace_caller (mem_mallocdepth)); else return big_mem.alloc (count, NULL); #else return malloc ((size_t) count); #endif}/********************************************************************** * alloc_big_zeros * * Return a pointer to a buffer of count bytes aligned for any type. **********************************************************************/DLLSYM void *alloc_big_zeros( //get some memory INT32 count //no of bytes to get ) { #ifdef TESTING_BIGSTUFF if (big_mem.biggestblock == 0) big_mem.init ((void *(*)(INT32)) malloc, free, BIGSIZE, BIGSIZE, MAX_BIGCHUNK); void *buf; //return value if (mem_mallocdepth > 0) buf = big_mem.alloc (count, trace_caller (mem_mallocdepth)); else buf = big_mem.alloc (count, NULL); memset (buf, 0, count); return buf; #else return calloc ((size_t) count, 1); #endif}/********************************************************************** * free_mem * * Free a block allocated by alloc_mem (or alloc_mem_p). * It checks that the pointer is legal and maintains counts of the * amount of free memory above and below the current free pointer. **********************************************************************/DLLSYM void free_mem( //free mem from alloc_mem void *oldchunk //chunk to free ) { #ifdef RAYS_MALLOC if (mem_freedepth > 0 && main_mem.callers != NULL) main_mem.dealloc (oldchunk, trace_caller (mem_freedepth)); else main_mem.dealloc (oldchunk, NULL); #else free(oldchunk); #endif}/********************************************************************** * free_big_mem * * Free a block allocated by alloc_big_mem. * It checks that the pointer is legal and maintains counts of the * amount of free memory above and below the current free pointer. **********************************************************************/DLLSYM void free_big_mem( //free mem from alloc_mem void *oldchunk //chunk to free ) { #ifdef TESTING_BIGSTUFF if (mem_freedepth > 0 && main_mem.callers != NULL) big_mem.dealloc (oldchunk, trace_caller (mem_freedepth)); else big_mem.dealloc (oldchunk, NULL); #else free(oldchunk); #endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -