From 953e3065b81c68db9f7462bb790406fcd370c768 Mon Sep 17 00:00:00 2001
From: "csander@caltech.edu" <csander@caltech.edu>
Date: Sat, 21 Nov 2020 00:04:35 +0000
Subject: [PATCH] Fix #2

---
 .gitignore    |  1 +
 Makefile      | 27 +++++++++++----
 driver/fcyc.c | 95 +++++++++++++++++++++++++--------------------------
 3 files changed, 67 insertions(+), 56 deletions(-)

diff --git a/.gitignore b/.gitignore
index fd6a322..25ba33d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
 bin/*
 out/*
+.debug
diff --git a/Makefile b/Makefile
index c018d53..4b71fb1 100644
--- a/Makefile
+++ b/Makefile
@@ -1,15 +1,26 @@
 #
 # Makefile for the malloc lab driver
 #
-CC = clang
-CFLAGS = -Werror -Wall -Wextra -O3 -g
 
-all: bin/mdriver-implicit bin/mdriver-explicit
+CLEAN_COMMAND = rm -f out/* bin/*
+ifdef DEBUG
+  CC = clang-with-asan
+  CFLAGS = -Werror -Wall -Wextra
+  ifeq ($(wildcard .debug),)
+    $(shell $(CLEAN_COMMAND))
+    $(shell touch .debug)
+  endif
+else
+  CC = clang
+  CFLAGS = -Werror -Wall -Wextra -O3 -g
+  ifneq ($(wildcard .debug),)
+    $(shell $(CLEAN_COMMAND) .debug)
+  endif
+endif
 
-bin/mdriver-implicit: out/mdriver-implicit.o out/mm-implicit.o out/memlib.o out/fsecs.o out/fcyc.o out/clock.o out/ftimer.o
-	$(CC) $(CFLAGS) $^ -o $@
+all: bin/mdriver-implicit bin/mdriver-explicit
 
-bin/mdriver-explicit: out/mdriver-explicit.o out/mm-explicit.o out/memlib.o out/fsecs.o out/fcyc.o out/clock.o out/ftimer.o
+bin/mdriver-%: out/mdriver-%.o out/mm-%.o out/memlib.o out/fsecs.o out/fcyc.o out/clock.o
 	$(CC) $(CFLAGS) $^ -o $@
 
 out/mdriver-implicit.o: driver/mdriver.c
@@ -25,4 +36,6 @@ out/%.o: driver/%.c
 	$(CC) $(CFLAGS) -c $^ -o $@
 
 clean:
-	rm -f out/* bin/*
+	$(CLEAN_COMMAND)
+
+.PRECIOUS: bin/mdriver-% out/mdriver-implicit.o out/mdriver-explicit.o out/%.o
diff --git a/driver/fcyc.c b/driver/fcyc.c
index b8e4113..78559fd 100644
--- a/driver/fcyc.c
+++ b/driver/fcyc.c
@@ -43,11 +43,11 @@ static double *samples = NULL;
 static void init_sampler()
 {
     if (values)
-	free(values);
+        free(values);
     values = calloc(kbest, sizeof(double));
 #if KEEP_SAMPLES
     if (samples)
-	free(samples);
+        free(samples);
     /* Allocate extra for wraparound analysis */
     samples = calloc(maxsamples+kbest, sizeof(double));
 #endif
@@ -61,11 +61,11 @@ static void add_sample(double val)
 {
     int pos = 0;
     if (samplecount < kbest) {
-	pos = samplecount;
-	values[pos] = val;
+        pos = samplecount;
+        values[pos] = val;
     } else if (val < values[kbest-1]) {
-	pos = kbest-1;
-	values[pos] = val;
+        pos = kbest-1;
+        values[pos] = val;
     }
 #if KEEP_SAMPLES
     samples[samplecount] = val;
@@ -73,10 +73,10 @@ static void add_sample(double val)
     samplecount++;
     /* Insertion sort */
     while (pos > 0 && values[pos-1] > values[pos]) {
-	double temp = values[pos-1];
-	values[pos-1] = values[pos];
-	values[pos] = temp;
-	pos--;
+        double temp = values[pos-1];
+        values[pos-1] = values[pos];
+        values[pos] = temp;
+        pos--;
     }
 }
 
@@ -86,34 +86,31 @@ static void add_sample(double val)
 static int has_converged()
 {
     return
-	(samplecount >= kbest) &&
-	((1 + epsilon)*values[0] >= values[kbest-1]);
+        (samplecount >= kbest) &&
+        ((1 + epsilon)*values[0] >= values[kbest-1]);
 }
 
 /*
  * clear - Code to clear cache
  */
-static volatile int sink = 0;
 
 static void clear()
 {
-    int x = sink;
     int *cptr, *cend;
     int incr = cache_block/sizeof(int);
     if (!cache_buf) {
-	cache_buf = malloc(cache_bytes);
-	if (!cache_buf) {
-	    fprintf(stderr, "Fatal error.  Malloc returned null when trying to clear cache\n");
-	    exit(1);
-	}
+        cache_buf = malloc(cache_bytes);
+        if (!cache_buf) {
+            fprintf(stderr, "Fatal error.  Malloc returned null when trying to clear cache\n");
+            exit(1);
+        }
     }
     cptr = (int *) cache_buf;
     cend = cptr + cache_bytes/sizeof(int);
     while (cptr < cend) {
-	x += *cptr;
-	cptr += incr;
+        *(volatile int *) cptr;
+        cptr += incr;
     }
-    sink = x;
 }
 
 /*
@@ -136,32 +133,32 @@ double fcyc(test_funct f, void *argp)
     double result;
     init_sampler();
     if (compensate) {
-	do {
-	    double cyc;
-	    if (clear_cache)
-		clear();
-	    start_comp_counter();
-	    f(argp);
-	    cyc = get_comp_counter();
-	    add_sample(cyc);
-	} while (!has_converged() && samplecount < maxsamples);
+        do {
+            double cyc;
+            if (clear_cache)
+                clear();
+            start_comp_counter();
+            f(argp);
+            cyc = get_comp_counter();
+            add_sample(cyc);
+        } while (!has_converged() && samplecount < maxsamples);
     } else {
-	do {
-	    double cyc;
-	    if (clear_cache)
-		clear();
-	    start_counter();
-	    f(argp);
-	    cyc = get_counter();
-	    add_sample(cyc);
-	} while (!has_converged() && samplecount < maxsamples);
+        do {
+            double cyc;
+            if (clear_cache)
+                clear();
+            start_counter();
+            f(argp);
+            cyc = get_counter();
+            add_sample(cyc);
+        } while (!has_converged() && samplecount < maxsamples);
     }
 #ifdef DEBUG
     {
-	int i;
-	printf(" %d smallest values: [", kbest);
-	for (i = 0; i < kbest; i++)
-	    printf("%.0f%s", values[i], i==kbest-1 ? "]\n" : ", ");
+        int i;
+        printf(" %d smallest values: [", kbest);
+        for (i = 0; i < kbest; i++)
+            printf("%.0f%s", values[i], i==kbest-1 ? "]\n" : ", ");
     }
 #endif
     result = values[0];
@@ -194,11 +191,11 @@ void set_fcyc_clear_cache(int clear)
 void set_fcyc_cache_size(int bytes)
 {
     if (bytes != cache_bytes) {
-	cache_bytes = bytes;
-	if (cache_buf) {
-	    free(cache_buf);
-	    cache_buf = NULL;
-	}
+        cache_bytes = bytes;
+        if (cache_buf) {
+            free(cache_buf);
+            cache_buf = NULL;
+        }
     }
 }
 
-- 
GitLab