Hi - I am quite new to this and not sure if I can do the below. Below is one file in a multi-file program... basically the main program calls initialise() and then calls process_access().
The arguments passed to process_access are fixed as is - but i need to be able to access the structures from all the fuctions without passing them. The members set correctly on initialisation but if i try to call them through the other functions they are all zero....
This probably doesnt make much sense but if somebosy could help me out i would be really grateful... thanks.
Code:
typedef struct{
int mem_blk;
int cache_blk;
int bank_num;
int dirty;
int valid;
} Block_Struct;
typedef struct {
IndexFunc indexfunc_ptr;
Block_Struct *blocks;
} Bank_Struct;
typedef struct {
int size_blks;
int blks_per_bank;
int num_banks;
Bank_Struct *banks;
} Cache_Struct;
Cache_Struct one_data;
Cache_Struct one_inst;
Cache_Struct two;
void initialise_cache(CacheParams params, Cache_Struct cache);
Block_Struct * find(Cache_Struct cache, int memory_address);
void
initialize(CacheParams l1_inst_params, CacheParams l1_data_params,
CacheParams l2_params)
{
initialise_cache(l1_inst_params, one_inst);
initialise_cache(l1_data_params, one_data);
initialise_cache(l2_params, two);
}
void
initialise_cache(CacheParams params, Cache_Struct cache){
int i = 0;
cache.num_banks = params.num_banks;
cache.size_blks = params.block_size;
cache.blks_per_bank = params.num_blocks_per_bank;
cache.banks = malloc(sizeof(Bank_Struct)*(cache.num_banks));
/* loops for each bank in the cache to initalise the blocks in the bank */
for( i = 0; i < (cache.num_banks); i++ ){
int j;
cache.banks[i].indexfunc_ptr = params.index_func[i];
cache.banks[i].blocks =
malloc(sizeof(Block_Struct)*(cache.blks_per_bank));
for ( j = 0; j < cache.blks_per_bank; j++ ){
cache.banks[i].blocks[j].cache_blk = j;
cache.banks[i].blocks[j].bank_num = i;
cache.banks[i].blocks[j].dirty = 0;
cache.banks[i].blocks[j].valid = 0;
}
}
}
Block_Struct *
find(Cache_Struct cache, int memory_address){
int i;
int num_banks, blk_size;
int blk_offset, mem_blk_number;
int cache_blk;
Block_Struct * blk_ptr;
blk_size = cache.size_blks;
blk_offset = memory_address % blk_size;
mem_blk_number = memory_address / blk_size;
for( i = 0; i < num_banks; i++ ){
cache_blk = cache.banks[i].indexfunc_ptr(mem_blk_number, cache.blks_per_bank);
blk_ptr = &(cache.banks[i].blocks[cache_blk]);
if ( (blk_ptr->mem_blk == mem_blk_number) && blk_ptr->valid )
return blk_ptr;
}
return NULL;
}
void
process_access(AccessType access_type, int address, int access_size)
{
if( access_type == ACC_DATA_READ )
find(one_data, address);
}