From 2fc771d08e310b79d8942cf4e4d7adf6a34b2f0e Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Wed, 27 Apr 2016 16:13:27 -0400 Subject: [PATCH] Add cache allocation and free functions --- csim.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/csim.c b/csim.c index cb3f4bb..2819fa5 100644 --- a/csim.c +++ b/csim.c @@ -36,6 +36,8 @@ 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); /* Global variables */ uint8_t VERBOSE = 0; /**< If nonzero, mprintf will not print. Set in main() if -v flag is given. */ @@ -84,6 +86,8 @@ int main(int argc, char* argv[]) /* 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); + line** cache = make_cache(set_indices, num_lines); + /* FILE READING */ char buffer[20]; @@ -109,9 +113,27 @@ int main(int argc, char* argv[]) } printSummary(0, 0, 0); + free_cache(cache, num_lines); + return 0; } +line** make_cache(size_t set_indices, size_t lines) +{ + line** cache = calloc(set_indices, sizeof(line*)); + int ii; + for (ii = 0; ii < lines; ii++) + cache[ii] = calloc(lines, sizeof(line)); + return cache; +} + +void free_cache(line** cache, size_t lines) +{ + int ii; + for (ii = 0; ii < lines; ii++) + free(cache[ii]); + free(cache); +} void print_usage(void) {