Question: This C code is not running which is related to cache memory operations.......Please find the bugs here /******************************** * 1. Includes * ********************************/ #include #include
This C code is not running which is related to cache memory operations.......Please find the bugs here
/******************************** * 1. Includes * ********************************/ #include
#include
}; struct Cache_ { int hits; int misses; int reads; int writes; int cache_size; int block_size; int numLines; Block* blocks;
}; calculates the corresponding tag, index, and offset. * * @param binary memory address * * @return void
void parseMemoryAddress(char *address) { unsigned int dec; char *bstring, *bformatted, *tag, *index, *offset; int i; bformatted = address; i = 0; tag = (char *) malloc( sizeof(char) * (TAG + 1) ); assert(tag != NULL); tag[TAG] = '\0'; for(i = 0; i < TAG; i++) { tag[i] = bformatted[i]; } index = (char *) malloc( sizeof(char) * (INDEX + 1) ); assert(index != NULL); index[INDEX] = '\0'; for(i = TAG + 1; i < INDEX + TAG + 1; i++) { index[i - TAG - 1] = bformatted[i]; } offset = (char *) malloc( sizeof(char) * (OFFSET + 1) ); assert(offset != NULL); offset[OFFSET] = '\0'; for(i = INDEX + TAG + 2; i < OFFSET + INDEX + TAG + 2; i++) { offset[i - INDEX - TAG - 2] = bformatted[i]; } printf("Tag: %s ", tag); printf("Index: %s ", index); printf("Offset: %s ", offset);
} int main(int argc, char **argv) { /* Local Variables */ int counter, i, j; Cache cache; FILE *file; char mode, address[100]; char buffer[LINELENGTH]; if(argc < 5 ) { fprintf(stderr,"insufficient no. of arguments "); return 0; } /* Open the file for reading. */ file = fopen( argv[0], "r" ); if( file == NULL ) { fprintf(stderr, "Error: Could not open file. "); return 0; } cache = createCache(CACHE_SIZE, BLOCK_SIZE); counter = 0; while( fgets(buffer, LINELENGTH, file) != NULL ) { if(buffer[0] != '#') { i = 0; while(buffer[i] != ' ') { i++; } mode = buffer[i+1]; i = i+2; j = 0; while(buffer[i] != '\0') { address[j] = buffer[i]; i++; j++; } address[j-1] = '\0'; if(DEBUG) printf("%i: %c %s ", counter, mode, address); if(mode == 'R') { readFromCache(cache, address); } else if(mode == 'W') { writeToCache(cache, address); } else { printf("%i: ERROR!!!! ", counter); fclose(file); destroyCache(cache); cache = NULL; return 0; } counter++; } } if(DEBUG) printf("Num Lines: %i ", counter); printf("CACHE HITS: %i CACHE MISSES: %i MEMORY READS: %i MEMORY WRITES: %i ", cache->hits, cache->misses, cache->reads, cache->writes); /* Close the file, destroy the cache. */ fclose(file); destroyCache(cache); cache = NULL; return 1;
} Cache createCache(int cache_size, int block_size) { /* Local Variables */ Cache cache; int i; /* Validate Inputs */ if(cache_size <= 0) { fprintf(stderr, "Cache size must be greater than 0 bytes... "); return NULL; } if(block_size <= 0) { fprintf(stderr, "Block size must be greater than 0 bytes... "); return NULL; } /* Lets make a cache! */ cache = (Cache) malloc( sizeof( struct Cache_ ) ); if(cache == NULL) { fprintf(stderr, "Could not allocate memory for cache. "); return NULL; } cache->hits = 0; cache->misses = 0; cache->reads = 0; cache->writes = 0; cache->cache_size = CACHE_SIZE; cache->block_size = BLOCK_SIZE; /* Calculate numLines */ cache->numLines = (int)(CACHE_SIZE / BLOCK_SIZE); cache->blocks = (Block*) malloc( sizeof(Block) * cache->numLines ); assert(cache->blocks != NULL); /* By default insert blocks where valid = 0 */ for(i = 0; i < cache->numLines; i++) { cache->blocks[i] = (Block) malloc( sizeof( struct Block_ ) ); assert(cache->blocks[i] != NULL); cache->blocks[i]->valid = 0; cache->blocks[i]->dirty = 0; cache->blocks[i]->tag = NULL; } return cache; } /* destroyCache * * Function that destroys a created cache. Frees all allocated memory. If * you pass in NULL, nothing happens. So make sure to set your cache = NULL * after you destroy it to prevent a double free. * * @param cache cache object to be destroyed * * @return void */ void destroyCache(Cache cache) { int i; if(cache != NULL) { free(cache); } return; } /* readFromCache * * Function that reads data from a cache. Returns 0 on failure * or 1 on success. * * @param cache target cache struct * @param address binaryaddress * * @return success 1 * @return failure 0 */ int readFromCache(Cache cache, char* address) { unsigned int dec; char *bstring, *bformatted, *tag, *index, *offset; int i; Block block; /* Validate inputs */ if(cache == NULL) { fprintf(stderr, "Error: Must supply a valid cache to write to. "); return 0; } if(address == NULL) { fprintf(stderr, "Error: Must supply a valid memory address. "); return 0; } /* Convert and parse necessary values */ bformatted = formatBinary(address); if(DEBUG) { printf("Formatted: %s ", bformatted); } i = 0; tag = (char *) malloc( sizeof(char) * (TAG + 1) ); assert(tag != NULL); tag[TAG] = '\0'; for(i = 0; i < TAG; i++) { tag[i] = bformatted[i]; } index = (char *) malloc( sizeof(char) * (INDEX + 1) ); assert(index != NULL); index[INDEX] = '\0'; for(i = TAG + 1; i < INDEX + TAG + 1; i++) { index[i - TAG - 1] = bformatted[i]; } offset = (char *) malloc( sizeof(char) * (OFFSET + 1) ); assert(offset != NULL); offset[OFFSET] = '\0'; for(i = INDEX + TAG + 2; i < OFFSET + INDEX + TAG + 2; i++) { offset[i - INDEX - TAG - 2] = bformatted[i]; } if(DEBUG) { printf("Tag: %s (%i) ", tag, btoi(tag)); printf("Index: %s (%i) ", index, btoi(index)); printf("Offset: %s (%i) ", offset, btoi(offset)); } /* Get the block */ block = cache->blocks[btoi(index)]; if(DEBUG) { printf("Attempting to read data from cache slot %i. ", btoi(index)); } if(block->valid == 1 && strcmp(block->tag, tag) == 0) { cache->hits++; free(tag); } else { cache->misses++; cache->reads++; block->valid = 1; if(block->tag != NULL) { free(block->tag); } block->tag = tag; } free(bformatted); free(offset); free(index); return 1; } /* writeToCache * * Function that writes data to the cache. Returns 0 on failure or * 1 on success. Frees any old tags that already existed in the * target slot. * * @param cache target cache struct * @param address binaryaddress * * @return success 1 * @return error 0 */ int writeToCache(Cache cache, char* address) { unsigned int dec; char *bstring, *bformatted, *tag, *index, *offset; int i; Block block; /* Validate inputs */ if(cache == NULL) { fprintf(stderr, "Error: Must supply a valid cache to write to. "); return 0; } if(address == NULL) { fprintf(stderr, "Error: Must supply a valid memory address. "); return 0; } /* Convert and parse necessary values */ bformatted = formatBinary(address); if(DEBUG) { printf("Formatted: %s ", bformatted); } i = 0; tag = (char *) malloc( sizeof(char) * (TAG + 1) ); assert(tag != NULL); tag[TAG] = '\0'; for(i = 0; i < TAG; i++) { tag[i] = bformatted[i]; } index = (char *) malloc( sizeof(char) * (INDEX + 1) ); assert(index != NULL); //checks for null values index[INDEX] = '\0'; for(i = TAG + 1; i < INDEX + TAG + 1; i++) { index[i - TAG - 1] = bformatted[i]; } offset = (char *) malloc( sizeof(char) * (OFFSET + 1) ); assert(offset != NULL); offset[OFFSET] = '\0'; for(i = INDEX + TAG + 2; i < OFFSET + INDEX + TAG + 2; i++) { offset[i - INDEX - TAG - 2] = bformatted[i]; } if(DEBUG) { printf("Tag: %s ", tag); printf("Index: %s ", index); printf("Offset: %s ", offset); } /* Get the block */ block = cache->blocks[btoi(index)]; if(DEBUG) { printf("Attempting to write data to cache slot %i. ", btoi(index)); } if(block->valid == 1 && strcmp(block->tag, tag) == 0) { cache->writes++; block->dirty = 1; cache->hits++; free(tag); } else { cache->misses++; cache->reads++; cache->writes++; } block->dirty = 1; block->valid = 1; if(block->tag != NULL) { free(block->tag); } block->tag = tag; } free(bstring); free(bformatted); free(offset); free(index); return 1; } /* printCache * * Prints out the values of each slot in the cache * as well as the hit, miss, read, write, and size * data. * * @param cache Cache struct * * @return void */ void printCache(Cache cache) { int i; char* tag; if(cache != NULL) { for(i = 0; i < cache->numLines; i++) { tag = "NULL"; if(cache->blocks[i]->tag != NULL) { tag = cache->blocks[i]->tag; } printf("[%i]: { valid: %i, tag: %s } ", i, cache->blocks[i]->valid, tag); } printf("Cache: \tCACHE HITS: %i \tCACHE MISSES: %i \tMEMORY READS: %i \tMEMORY WRITES: %i \tCACHE SIZE: %i Bytes \tBLOCK SIZE: %i Bytes \tNUM LINES: %i ", cache->hits, cache->misses, cache->reads, cache->writes, cache->cache_size, cache->block_size, cache->numLines); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
