diff --git a/csim.c b/csim.c index fb00293..0662f18 100644 --- a/csim.c +++ b/csim.c @@ -11,9 +11,10 @@ With the verbose option (-v), outputs details for each operation. Exit statuses: - 0 - success - 1 - usage error + 0 - success + 1 - usage error -1 - file error + 2 - memory error */ /**** Headers ****/ @@ -126,16 +127,16 @@ int main(int argc, char* argv[]) /* FILE READING */ FILE* f = fopen(filename, "r"); - if(!f) - { + if(!f) { printf("Invalid file name: %s", filename); exit(-1); } //char buffer[20]; while (fgets(buffer, 20, f)) { - if (buffer[0] == 'I') + if (buffer[0] == 'I') { continue; + } char* end; char op = buffer[1]; @@ -257,12 +258,19 @@ long bits_to_size(int bits) */ void increment_result_counters(char result, results* score) { - score->hits += (result == 'H'); - score->misses += (result == 'M' || result == 'E'); - score->evictions += (result == 'E'); - if (result == 'H') mprintf(" hit"); - if (result == 'M') mprintf(" miss"); - if (result == 'E') mprintf(" miss eviction"); + if (result == 'H') { + score->hits++; + mprintf(" hit"); + } + else if (result == 'M') { + score->misses++; + mprintf(" miss"); + } + else if (result == 'E') { + score->misses++; + score->evictions++; + mprintf(" miss eviction"); + } } @@ -278,6 +286,10 @@ void increment_result_counters(char result, results* score) linked_line** make_cache(long num_sets, long num_lines) { linked_line** cache = (linked_line**)calloc(num_sets, sizeof(linked_line*)); + if (! cache) { + printf("Error: calloc failed\n"); + exit(2); + } long ii; for (ii = 0; ii < num_sets; ii++) { cache[ii] = make_set(NULL, num_lines); @@ -290,9 +302,15 @@ linked_line** make_cache(long num_sets, long num_lines) @param newer A pointer to the previously-created element of the set. @param num_lines The number of lines remaining to be added. @return A pointer to the created line. + @bug Large values for num_lines result in a segfault in the call to calloc. + @TODO Free already-allocated space in event of allocation failure. */ linked_line* make_set(linked_line* newer, long num_lines) { linked_line* current = (linked_line*)calloc(1, sizeof(linked_line)); + if (! current) { + printf("Error: calloc failed\n"); + exit(2); + } current->newer = newer; num_lines--; if (num_lines) { // If not on the last line @@ -384,14 +402,21 @@ char cache_access(linked_line** cache, long address, linked_line* set_head = cache[set]; linked_line* line = find_line(set_head, tag); - if (!line->validity) out = 'M'; - else if (line->tag == tag) out = 'H'; - else out = 'E'; + if (!line->validity) { + out = 'M'; + } + else if (line->tag == tag) { + out = 'H'; + } + else { + out = 'E'; + } if(!(line == set_head)) { line->newer->older = line->older; - if(line->older) + if(line->older) { line->older->newer = line->newer; + } line->newer = NULL; line->older = set_head; set_head->newer = line; @@ -415,10 +440,12 @@ char cache_access(linked_line** cache, long address, */ linked_line* find_line(linked_line* line, long tag) { - if(line->tag == tag || !line->validity || !line->older) + if (line->tag == tag || !line->validity || !line->older) { return line; - else + } + else { return find_line(line->older, tag); + } }