Skip to content

Commit

Permalink
memgrind
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Zhuang authored and Eric Zhuang committed Oct 14, 2016
1 parent 8fa0ad1 commit 18b44e9
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 0 deletions.
49 changes: 49 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,52 @@ int main(int argc, char** argv) {
return 0;
}

// int main( int argc, char** argv) {

// printf("1\n");
// block_init();
// printf("2\n");
// int x;
// printf("3\n");
// free( &x );
// printf("4\n");

// char * p = (char *)malloc( 200 );
// printf("5\n");
// free( p + 10 );
// printf("6\n");

// int * y;
// printf("7\n");
// free( y );
// printf("8\n");
// char * p2 = (char*)malloc(100);
// printf("9\n");
// free( p2 );
// printf("10\n");
// free( p2 );
// printf("11\n");
// char* p4 = (char*)malloc(5001);
// printf("12\n");
// char* p5 = (char*)malloc(5000);
// printf("13\n");
// char* p6 = (char*)malloc(1);
// printf("14\n");

// int i, n;
// time_t t;

// n = 5;

// /* Intializes random number generator */
// srand((unsigned) time(&t));

// /* Print 5 random numbers from 0 to 49 */
// for( i = 0 ; i < n ; i++ )
// {
// printf("%d\n", rand() % 2);
// }

// return 0;

// }
93 changes: 93 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
COMPILER = gcc
CFLAGS = -Wall -g
CFLAGS2 = -g

all: mymalloc Test1 Test2 Test3 Test4 Test5 Test6 Test7 Test8
.PHONY: all
.PHONY: clean

mymalloc.o: mymalloc.c mymalloc.h
$(COMPILER) $(CFLAGS) -c mymalloc.c

main.o: main.c mymalloc.h
$(COMPILER) $(CFLAGS2) -c main.c

mymalloc: mymalloc.o main.o
$(COMPILER) $(CFLAGS) -o mymalloc main.o mymalloc.o


Test1.o: Test1.c
$(COMPILER) $(CFLAGS2) -c Test1.c

Test1: mymalloc.o Test1.o
$(COMPILER) $(CFLAGS) -o Test1 Test1.o mymalloc.o


Test2.o: Test2.c
$(COMPILER) $(CFLAGS2) -c Test2.c

Test2: mymalloc.o Test2.o
$(COMPILER) $(CFLAGS) -o Test2 Test2.o mymalloc.o


Test3.o: Test3.c
$(COMPILER) $(CFLAGS2) -c Test3.c

Test3: mymalloc.o Test3.o
$(COMPILER) $(CFLAGS) -o Test3 Test3.o mymalloc.o


Test4.o: Test4.c
$(COMPILER) $(CFLAGS2) -c Test4.c

Test4: mymalloc.o Test4.o
$(COMPILER) $(CFLAGS) -o Test4 Test4.o mymalloc.o


Test5.o: Test5.c
$(COMPILER) $(CFLAGS2) -c Test5.c

Test5: mymalloc.o Test5.o
$(COMPILER) $(CFLAGS) -o Test5 Test5.o mymalloc.o


Test6.o: Test6.c
$(COMPILER) $(CFLAGS2) -c Test6.c

Test6: mymalloc.o Test6.o
$(COMPILER) $(CFLAGS) -o Test6 Test6.o mymalloc.o


Test7.o: Test7.c
$(COMPILER) $(CFLAGS2) -c Test7.c

Test7: mymalloc.o Test7.o
$(COMPILER) $(CFLAGS) -o Test7 Test7.o mymalloc.o


Test8.o: Test8.c
$(COMPILER) $(CFLAGS2) -c Test8.c

Test8: mymalloc.o Test7.o
$(COMPILER) $(CFLAGS) -o Test8 Test8.o mymalloc.o


clean:
rm -f *.o mymalloc Test1 Test2 Test3 Test4 Test5 Test6 Test7 Test8

CC = gcc
CCFLAGS = -Wall -g

all: runfile

runfile: mymalloc.o memgrind.o
$(CC) $(CCFLAGS) -o runfile mymalloc.o memgrind.o

mymalloc.o: mymalloc.c mymalloc.h
$(CC) $(CCFLAGS) -c mymalloc.c

memgrind.o: memgrind.c
$(CC) $(CCFLAGS) -c memgrind.c

clean:
rm -f runfile mymalloc.o memgrind.o
109 changes: 109 additions & 0 deletions memgrind.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include <stdio.h>
#include <sys/time.h>
#include "mymalloc.h"


void testA(){
char* malloc_pointers[3000];
int pointer_counter = 0;
int workload_counter = 0;
long int time_total = 0;

while(workload_counter < 100) {
struct timeval start, end;
gettimeofday(&start, NULL);


while(pointer_counter < 3000) {

malloc_pointers[pointer_counter] = malloc(1);
pointer_counter++;

}
pointer_counter = 0;
while(pointer_counter < 3000) {

free(malloc_pointers[pointer_counter]);
pointer_counter++;

}
gettimeofday(&end, NULL);
time_total = time_total + ((end.tv_sec * 1000000 + end.tv_usec)
- (start.tv_sec * 1000000 + start.tv_usec));
workload_counter++;
}
long int average_time = time_total/100;
printf("Average Time for Test A was %ld microseconds\n", average_time);
}

void testB(){
int pointer_counter = 0;
int workload_counter = 0;
long int time_total = 0;

while(workload_counter < 100) {
struct timeval start, end;
gettimeofday(&start, NULL);


while(pointer_counter < 3000) {

char* test_pointer = (char*)malloc(1);
free(test_pointer);
pointer_counter++;
}

gettimeofday(&end, NULL);
time_total = time_total + ((end.tv_sec * 1000000 + end.tv_usec)
- (start.tv_sec * 1000000 + start.tv_usec));
workload_counter++;
}
long int average_time = time_total/100;
printf("Average Time for Test B was %ld microseconds\n", average_time);

}

void testC(){

int malloc_counter = 0;
int free_counter = 0;
int total_counter = 0;
time_t t;

/* Intializes random number generator */
srand((unsigned) time(&t));

while(total_counter < 6000) {

int random_number = rand() % 2;
if(random_number == 0 && malloc_counter < 3000) {
malloc_counter++;
total_counter++;
}else if (random_number == 1 && free_counter < 3000) {
free_counter++;
total_counter++;
}

}
printf("total_counter is: %d\nmalloc_counter is: %d\nfree_counter is: %d\n",total_counter,malloc_counter,free_counter);

}
void testD(){

}

void testEF(){

}


int main( int argc, char** argv ) {
block_init();
testA();
testB();
testC();

return 0;
}
Contact GitHub API Training Shop Blog About
© 2016 GitHub, Inc. Terms Privacy Security Status Help

0 comments on commit 18b44e9

Please sign in to comment.