Multiple mostly-stylistic changes, and improved argument checking.
parse_int_arg no checks that -E is given nothing larger than a long. Simplified conditions in increment_result_counters. Added braces to conditional blocks without any. Added conditions to stop if calloc returns NULL.
This commit is contained in:
parent
df84d11ffb
commit
93801e248b
61
csim.c
61
csim.c
@ -11,9 +11,10 @@
|
|||||||
With the verbose option (-v), outputs details for each operation.
|
With the verbose option (-v), outputs details for each operation.
|
||||||
|
|
||||||
Exit statuses:
|
Exit statuses:
|
||||||
0 - success
|
0 - success
|
||||||
1 - usage error
|
1 - usage error
|
||||||
-1 - file error
|
-1 - file error
|
||||||
|
2 - memory error
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**** Headers ****/
|
/**** Headers ****/
|
||||||
@ -126,16 +127,16 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
/* FILE READING */
|
/* FILE READING */
|
||||||
FILE* f = fopen(filename, "r");
|
FILE* f = fopen(filename, "r");
|
||||||
if(!f)
|
if(!f) {
|
||||||
{
|
|
||||||
printf("Invalid file name: %s", filename);
|
printf("Invalid file name: %s", filename);
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
//char buffer[20];
|
//char buffer[20];
|
||||||
while (fgets(buffer, 20, f)) {
|
while (fgets(buffer, 20, f)) {
|
||||||
if (buffer[0] == 'I')
|
if (buffer[0] == 'I') {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
char* end;
|
char* end;
|
||||||
char op = buffer[1];
|
char op = buffer[1];
|
||||||
@ -257,12 +258,19 @@ long bits_to_size(int bits)
|
|||||||
*/
|
*/
|
||||||
void increment_result_counters(char result, results* score)
|
void increment_result_counters(char result, results* score)
|
||||||
{
|
{
|
||||||
score->hits += (result == 'H');
|
if (result == 'H') {
|
||||||
score->misses += (result == 'M' || result == 'E');
|
score->hits++;
|
||||||
score->evictions += (result == 'E');
|
mprintf(" hit");
|
||||||
if (result == 'H') mprintf(" hit");
|
}
|
||||||
if (result == 'M') mprintf(" miss");
|
else if (result == 'M') {
|
||||||
if (result == 'E') mprintf(" miss eviction");
|
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** make_cache(long num_sets, long num_lines)
|
||||||
{
|
{
|
||||||
linked_line** cache = (linked_line**)calloc(num_sets, sizeof(linked_line*));
|
linked_line** cache = (linked_line**)calloc(num_sets, sizeof(linked_line*));
|
||||||
|
if (! cache) {
|
||||||
|
printf("Error: calloc failed\n");
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
long ii;
|
long ii;
|
||||||
for (ii = 0; ii < num_sets; ii++) {
|
for (ii = 0; ii < num_sets; ii++) {
|
||||||
cache[ii] = make_set(NULL, num_lines);
|
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 newer A pointer to the previously-created element of the set.
|
||||||
@param num_lines The number of lines remaining to be added.
|
@param num_lines The number of lines remaining to be added.
|
||||||
@return A pointer to the created line.
|
@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* make_set(linked_line* newer, long num_lines) {
|
||||||
linked_line* current = (linked_line*)calloc(1, sizeof(linked_line));
|
linked_line* current = (linked_line*)calloc(1, sizeof(linked_line));
|
||||||
|
if (! current) {
|
||||||
|
printf("Error: calloc failed\n");
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
current->newer = newer;
|
current->newer = newer;
|
||||||
num_lines--;
|
num_lines--;
|
||||||
if (num_lines) { // If not on the last line
|
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* set_head = cache[set];
|
||||||
linked_line* line = find_line(set_head, tag);
|
linked_line* line = find_line(set_head, tag);
|
||||||
|
|
||||||
if (!line->validity) out = 'M';
|
if (!line->validity) {
|
||||||
else if (line->tag == tag) out = 'H';
|
out = 'M';
|
||||||
else out = 'E';
|
}
|
||||||
|
else if (line->tag == tag) {
|
||||||
|
out = 'H';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
out = 'E';
|
||||||
|
}
|
||||||
|
|
||||||
if(!(line == set_head)) {
|
if(!(line == set_head)) {
|
||||||
line->newer->older = line->older;
|
line->newer->older = line->older;
|
||||||
if(line->older)
|
if(line->older) {
|
||||||
line->older->newer = line->newer;
|
line->older->newer = line->newer;
|
||||||
|
}
|
||||||
line->newer = NULL;
|
line->newer = NULL;
|
||||||
line->older = set_head;
|
line->older = set_head;
|
||||||
set_head->newer = line;
|
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)
|
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;
|
return line;
|
||||||
else
|
}
|
||||||
|
else {
|
||||||
return find_line(line->older, tag);
|
return find_line(line->older, tag);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user