Implemented linked_lines and modified cache creation/deletion.

Replaced struct line_s and typedef line with linked_line_s and linked_line.
Added make_set() and free_set().
Modified make_cache() and free_cache() to call make_set() and free_set().
This commit is contained in:
Jacob 2016-04-27 21:24:53 -04:00
parent 7f254fd50b
commit b556be1622
1 changed files with 55 additions and 18 deletions

73
csim.c
View File

@ -22,23 +22,28 @@ Exit statuses:
/* Typedefs and structs */
typedef unsigned char uint8_t;
/* UNSURE. As defined in conference. */
struct line_s {
/* Linked Line and related prototypes */
typedef struct linked_line_s linked_line;
struct linked_line_s {
uint8_t validity;
int tag;
long tag;
linked_line* newer;
linked_line* older;
};
typedef struct line_s line;
/* Macros */
#define mprintf(...) if (VERBOSE) printf(__VA_ARGS__)
/* Prototypes */
/* General Prototypes */
void print_usage(void);
void bad_usage_error(void);
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);
linked_line** make_cache(long set_indices, long num_lines);
linked_line* make_set(linked_line* newer, long num_lines);
void free_cache(linked_line** cache, long num_sets);
void free_set(linked_line* line);
/* Global variables */
uint8_t VERBOSE = 0; /**< If nonzero, mprintf will not print. Set in main() if -v flag is given. */
@ -86,7 +91,7 @@ int main(int argc, char* argv[])
/* End of argument parsing. */
//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);
linked_line** cache = make_cache(set_indices, num_lines);
/* FILE READING */
FILE* f = fopen(filename, "r");
@ -114,31 +119,63 @@ int main(int argc, char* argv[])
printSummary(0, 0, 0);
fclose(f);
free_cache(cache, num_lines);
free_cache(cache, set_index_bits);
return 0;
}
/** What do you think? */
line** make_cache(long set_indices, long lines)
linked_line** make_cache(long set_indices, long num_lines)
{
line** cache = calloc(set_indices, sizeof(line*));
int ii;
for (ii = 0; ii < lines; ii++) {
cache[ii] = calloc(lines, sizeof(line));
linked_line** cache = (linked_line**)calloc(set_indices, sizeof(linked_line*));
long ii;
for (ii = 0; ii < num_lines; ii++) {
cache[ii] = make_set(NULL, num_lines);
}
return cache;
}
void free_cache(line** cache, long lines)
/**
Creates a set by creating and linking a given number of 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.
*/
linked_line* make_set(linked_line* newer, long num_lines) {
linked_line* current = (linked_line*)calloc(1, sizeof(linked_line));
current->newer = newer;
if (num_lines) { // If not on the last line
current->older = make_set(current, num_lines-1);
}
else {
current->older = NULL;
}
return current;
}
void free_cache(linked_line** cache, long num_sets)
{
int ii;
for (ii = 0; ii < lines; ii++) {
free(cache[ii]);
for (ii = 0; ii < num_sets; ii++) {
free_set(cache[ii]);
}
free(cache);
}
/**
Frees a set by recursing through it, then freeing each element from oldest to newest.
@param line A pointer to the next line in the set.
@param num_lines The number
*/
void free_set(linked_line* line) {
if (line != NULL) {
free_set(line->older);
free(line);
}
}
void print_usage(void)
{
printf("Usage: ./csim [-hv] -s <number> -E <number> -b <number> -t <file>\n"