WinDBG/Exts/CodeAnalyzer/gdbplus/gdb-7.7/gdb/heap.c
/*
 * block's index map is an array of indexes of sub blocks
 */
block's index map is an array of indexes of sub blocks
block ' s index map is an array of indexes of sub blocks
static CA_BOOL build_block_index_map(struct inuse_block* blk,
						struct inuse_block* inuse_blocks,
						unsigned long num_inuse_blocks)
{
	size_t ptr_sz = g_ptr_bit >> 3;
	if (!blk->reachable.index_map)
	{
		address_t start, end, cursor;
		unsigned int max_sub_blocks, total_sub_blocks;
		unsigned int* index_buf = NULL;
		unsigned int i, index;
		start = ALIGN(blk->addr, ptr_sz);
		end   = start + blk->size;
		cursor = start;
		max_sub_blocks = (end - start) / ptr_sz;
		total_sub_blocks = 0;
		index_buf = get_index_map_buffer(max_sub_blocks + 1);	
		while (cursor < end)
		{
			address_t ptr;
			struct inuse_block *sub_blk;
			if (read_memory_wrapper(NULL, cursor, (void*)&ptr, ptr_sz) && ptr)
			{
				sub_blk = find_inuse_block(ptr, inuse_blocks, num_inuse_blocks);
				if (sub_blk)
				{
					CA_BOOL found_dup = CA_FALSE;
					index = sub_blk - inuse_blocks;
					for (i = 0; i < total_sub_blocks; i++)
					{
						if (index_buf[i] == index)
						{
							found_dup = CA_TRUE;
							break;
						}
					}
					if (!found_dup)
					{
						index_buf[total_sub_blocks++] = index;
						if (total_sub_blocks == UINT_MAX)
						{
							CA_PRINT("Internal fatal error: number of sub blocks exceeds 4 billion\n");
							return CA_FALSE;
						}
					}
				}
			}
			cursor += ptr_sz;
		}
		index_buf[total_sub_blocks++] = UINT_MAX;	
		blk->reachable.index_map = (unsigned int*) malloc(total_sub_blocks * sizeof(unsigned int));
		if (!blk->reachable.index_map)
		{
			CA_PRINT("Out-of-memory\n");
			return CA_FALSE;
		}
		memcpy(blk->reachable.index_map, index_buf, total_sub_blocks * sizeof(unsigned int));
	}
	return CA_TRUE;
}
static CA_BOOL build_block_index_map ( struct inuse_block * blk , struct inuse_block * inuse_blocks , unsigned long num_inuse_blocks ) { size_t ptr_sz = g_ptr_bit >> 3 ; if ( ! blk -> reachable . index_map ) { address_t start , end , cursor ; unsigned int max_sub_blocks , total_sub_blocks ; unsigned int * index_buf = NULL ; unsigned int i , index ; start = ALIGN ( blk -> addr , ptr_sz ) ; end = start + blk -> size ; cursor = start ; max_sub_blocks = ( end - start ) / ptr_sz ; total_sub_blocks = 0 ; index_buf = get_index_map_buffer ( max_sub_blocks + 1 ) ; while ( cursor < end ) { address_t ptr ; struct inuse_block * sub_blk ; if ( read_memory_wrapper ( NULL , cursor , ( void * ) & ptr , ptr_sz ) && ptr ) { sub_blk = find_inuse_block ( ptr , inuse_blocks , num_inuse_blocks ) ; if ( sub_blk ) { CA_BOOL found_dup = CA_FALSE ; index = sub_blk - inuse_blocks ; for ( i = 0 ; i < total_sub_blocks ; i ++ ) { if ( index_buf [ i ] == index ) { found_dup = CA_TRUE ; break ; } } if ( ! found_dup ) { index_buf [ total_sub_blocks ++ ] = index ; if ( total_sub_blocks == UINT_MAX ) { CA_PRINT ( " \n " ) ; return CA_FALSE ; } } } } cursor += ptr_sz ; } index_buf [ total_sub_blocks ++ ] = UINT_MAX ; blk -> reachable . index_map = ( unsigned int * ) malloc ( total_sub_blocks * sizeof ( unsigned int ) ) ; if ( ! blk -> reachable . index_map ) { CA_PRINT ( " \n " ) ; return CA_FALSE ; } memcpy ( blk -> reachable . index_map , index_buf , total_sub_blocks * sizeof ( unsigned int ) ) ; } return CA_TRUE ; }
block's index map is an array of indexes of sub blocks
block ' s index map is an array of indexes of sub blocks
// Prepare this // Queue possible pointers to heap memory contained by this block // one for terminator // avoid duplicate, which is not uncommon // FIXME, consider non-linear search // allocate cache to hold the indexes of this block // this value serves as terminator