Browse Source

Fixed types.

size_t -> long
uint8_t -> uint8_t or int
masstesting
Jacob 6 years ago
parent
commit
7f254fd50b
  1. 46
      csim.c

46
csim.c

@ -21,6 +21,7 @@ Exit statuses: @@ -21,6 +21,7 @@ Exit statuses:
/* Typedefs and structs */
typedef unsigned char uint8_t;
/* UNSURE. As defined in conference. */
struct line_s {
uint8_t validity;
@ -34,22 +35,21 @@ typedef struct line_s line; @@ -34,22 +35,21 @@ typedef struct line_s line;
/* Prototypes */
void print_usage(void);
void bad_usage_error(void);
size_t parse_int_arg(char *arg, char opt);
size_t bits_to_size(uint8_t bits);
line** make_cache(size_t set_indices, size_t lines);
void free_cache(line** cache, size_t lines);
long parse_int_arg(char *arg, char opt);
long bits_to_size(int bits);
line** make_cache(long set_indices, long lines);
void free_cache(line** cache, long lines);
/* Global variables */
uint8_t VERBOSE = 0; /**< If nonzero, mprintf will not print. Set in main() if -v flag is given. */
int main(int argc, char* argv[])
{
int hits, misses, evictions;
uint8_t set_index_bits = 0, block_bits = 0; // Not-useful input variables.
size_t set_indices = 0, num_lines = 0, block_size = 0; //Useful variables
char buffer[20];
int set_index_bits = 0, block_bits = 0; // Not-useful input variables.
long set_indices = 0, num_lines = 0, block_size = 0; //Useful variables
char *filename = NULL;
int opt = 0;
while ((opt=getopt(argc, argv, "hvs:E:b:t:")) != -1) {
@ -84,13 +84,11 @@ int main(int argc, char* argv[]) @@ -84,13 +84,11 @@ int main(int argc, char* argv[])
bad_usage_error();
}
/* End of argument parsing. */
//printf("Arguments: s=%hhu, E=%zu, b=%hhu, t=%s, v=%hhu, 2^s=%zd, 2^b=%zu\n", set_index_bits, num_lines, block_bits, filename, VERBOSE, set_indices, block_size);
//probably broken: printf("Arguments: s=%hhu, E=%u, b=%hhu, t=%s, v=%hhu, 2^s=%d, 2^b=%u\n", set_index_bits, num_lines, block_bits, filename, VERBOSE, set_indices, block_size);
line** cache = make_cache(set_indices, num_lines);
/* FILE READING */
char buffer[20];
FILE* f = fopen(filename, "r");
if(!f)
{
@ -98,13 +96,14 @@ int main(int argc, char* argv[]) @@ -98,13 +96,14 @@ int main(int argc, char* argv[])
exit(-1);
}
//char buffer[20];
while (fgets(buffer, 20, f)) {
if (buffer[0] == 'I')
continue;
char* end;
char op = buffer[1];
void* address = strtol(buffer+3, &end, 16);
long address = strtol(buffer+3, &end, 16);
if (*end != ',' || !(op == 'S' || op == 'L' || op == 'M')) {
printf("Invalid input file, last line:\n%s\n", buffer);
return -1;
@ -120,20 +119,23 @@ int main(int argc, char* argv[]) @@ -120,20 +119,23 @@ int main(int argc, char* argv[])
return 0;
}
line** make_cache(size_t set_indices, size_t lines)
/** What do you think? */
line** make_cache(long set_indices, long lines)
{
line** cache = calloc(set_indices, sizeof(line*));
int ii;
for (ii = 0; ii < lines; ii++)
for (ii = 0; ii < lines; ii++) {
cache[ii] = calloc(lines, sizeof(line));
}
return cache;
}
void free_cache(line** cache, size_t lines)
void free_cache(line** cache, long lines)
{
int ii;
for (ii = 0; ii < lines; ii++)
for (ii = 0; ii < lines; ii++) {
free(cache[ii]);
}
free(cache);
}
@ -162,10 +164,10 @@ void bad_usage_error(void) @@ -162,10 +164,10 @@ void bad_usage_error(void)
Parses a string into a size_t.
@param arg The string to parse.
@param opt The option being parsed. If 'E', the output can not be 0.
@return The parsed integer if the argument was an integer.
@return The parsed long integer if the argument was an integer.
@note Exits if argument is invalid.
*/
size_t parse_int_arg(char* arg, char opt)
long parse_int_arg(char* arg, char opt)
{
char* end;
long i = strtol(arg, &end, 0);
@ -178,7 +180,7 @@ size_t parse_int_arg(char* arg, char opt) @@ -178,7 +180,7 @@ size_t parse_int_arg(char* arg, char opt)
printf("Argument %ld for option %c too small (must be greater than 0)", i, opt);
exit(1);
}
return (size_t)i;
return i;
}
/**
@ -186,11 +188,11 @@ size_t parse_int_arg(char* arg, char opt) @@ -186,11 +188,11 @@ size_t parse_int_arg(char* arg, char opt)
@param bits The power of 2 to use.
@note Exits if number of bits is larger than a size_t.
*/
size_t bits_to_size(uint8_t bits)
long bits_to_size(int bits)
{
size_t max_shift = 8*sizeof(size_t);
int max_shift = 8*sizeof(int);
if (bits > max_shift-1) {
printf("Argument %hhu too large (-s and -b must be less than %zu)", bits, max_shift);
printf("Argument %hhd too large (-s and -b must be less than %hhd)", bits, max_shift);
exit(1);
}
return (size_t)1 << bits;